PHP is a popular server-side scripting language for developing dynamic web applications. This guide will walk you through the process of installing PHP on an Ubuntu server or desktop.
Step 1: Update Your System
Before installing PHP, updating your package list and system packages is always a good practice. Open a terminal and run the following command:
sudo apt update && sudo apt upgrade -y
This ensures that you have the latest package information and security updates.
Step 2: Install PHP
Ubuntu’s default repositories contain PHP packages, making installation straightforward. To install PHP, run:
sudo apt install php -y
To verify the installation, check the installed PHP version:
php -v
You should see output similar to:
PHP 8.x.x (cli) (built: YYYY-MM-DD)
Copyright (c) The PHP Group
Step 3: Install PHP Extensions
Many web applications require additional PHP extensions. You can install them using the following command:
sudo apt install php-cli php-mbstring php-xml php-curl php-zip php-bcmath php-tokenizer php-mysql -y
To check enabled PHP modules, use:
php -m
Step 4: Configure PHP (Optional)
Depending on your application, you may need to modify PHP settings. The main configuration file is located at:
sudo nano /etc/php/*/apache2/php.ini
Adjust necessary settings such as memory_limit, upload_max_filesize, and max_execution_time Then save and exit.
Restart the Apache or Nginx server to apply changes:
For Apache:
sudo systemctl restart apache2
For Nginx (if using PHP-FPM):
sudo systemctl restart php*-fpm
Step 5: Test PHP Installation
To ensure PHP is working correctly with your web server, create a test file:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Save and exit. Open a browser and visit:
http://your-server-ip/info.php
You should see the PHP info page displaying details about your PHP installation.
Conclusion
Congratulations! You have successfully installed PHP on Ubuntu. You can now proceed with setting up your web applications. If you need specific PHP versions, consider using third-party repositories like ondrej/php to install different versions of PHP.
For further assistance, feel free to leave a comment below. Happy coding!