Install PHP 8.1 with Apache2 on Ubuntu 24.04
PHP is one of the most widely used server-side programming languages, powering millions of websites and applications around the world. Combined with Apache2, PHP makes it easy to run dynamic websites, content management systems like WordPress, and custom web applications. In this tutorial, you will learn how to install PHP 8.1 on Ubuntu 24.04 and configure it to work with Apache2.
Prerequisite: Apache2 Installation
Before installing PHP 8.1, make sure you already have Apache2 installed and configured on your system. You can follow this step-by-step guide: Install Apache2 on Ubuntu 24.04.
Add the PHP Repository
Ubuntu 24.04 does not come with PHP 8.1 by default. You need to add the ondrej/php
repository to install the correct version:
sudo add-apt-repository ppa:ondrej/php
Then update the package list:
sudo apt update
Install PHP 8.1 and Extensions
Now install PHP 8.1 along with the most commonly used extensions required by modern applications:
sudo apt install -y php8.1 php8.1-mcrypt php8.1-gd php8.1-curl php8.1-mysql php8.1-zip php8.1-xml php8.1-soap php8.1-intl php8.1-mbstring php8.1-bcmath
This command installs PHP 8.1 and modules for database support, XML, SOAP, internationalization, string handling, encryption, and more.
Edit the PHP Configuration File
After installing PHP, edit the php.ini
file for Apache to adjust performance and error reporting settings:
sudo nano /etc/php/8.1/apache2/php.ini
Modify or add the following lines inside the file:
memory_limit = 2048M
max_input_vars = 10000
max_execution_time = 3600
max_input_time = 3600
display_errors = On
error_reporting = E_ALL
These settings increase memory limits, extend execution times, and enable detailed error reporting for development.
Restart Apache2
Once PHP is installed and configured, restart Apache to apply the changes:
sudo systemctl restart apache2
Verify PHP Installation
You can verify PHP 8.1 is correctly installed by running:
php -v
You should see output confirming the PHP 8.1 version.
Additionally, you can create a PHP info file inside your web root to check if Apache2 is loading PHP correctly:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Now open your browser and visit:
http://localhost/info.php
You should see a detailed PHP configuration page.
Conclusion
You have successfully installed and configured PHP 8.1 with Apache2 on Ubuntu 24.04. With PHP enabled, your server is now ready to run dynamic websites, frameworks, or CMS platforms like WordPress, Joomla, and Laravel. By adjusting the php.ini
file, you have also optimized your environment for better performance and error handling.
Comments
Post a Comment