
PHP is one of the most popular server-side scripting languages used for web development. If you’re using Ubuntu as your operating system, installing PHP is a straightforward process. This guide will walk you through the steps needed to install PHP on Ubuntu.
Step 1: Update Your System
Before installing any software, it’s always a good idea to update your system to ensure that you have the latest packages and security updates. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
Step 2: Install Apache or Nginx
PHP works with web servers such as Apache or Nginx. If you don’t already have a web server installed, you’ll need to install one. For this guide, we’ll use Apache as an example.
To install Apache, run:
sudo apt install apache2
After installation, start and enable Apache:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 3: Install PHP
To install PHP, use the following command:
sudo apt install php libapache2-mod-php
This will install PHP along with the Apache module to handle PHP files.
Step 4: Verify PHP Installation
To confirm that PHP has been installed correctly, check the version by running:
php -v
You should see the installed PHP version in the output.
Step 5: Test PHP with Apache
To ensure that PHP is working with your web server, create a simple PHP file to test. Create a file called info.php
in the /var/www/html
directory:
sudo nano /var/www/html/info.php
Add the following code to the file:
<?php
phpinfo();
?>
Save and close the file. Then, open your web browser and navigate to:
http://your_server_ip/info.php
You should see the PHP information page, which confirms that PHP is working correctly.
Step 6: Install Additional PHP Modules (Optional)
Depending on your application, you may need additional PHP modules. You can search for available modules using:
apt search php-
To install a specific module, use:
sudo apt install php-MODULE_NAME
For example, to install the php-mysql
module, run:
sudo apt install php-mysql
Step 7: Restart Apache
After making changes or installing new modules, restart Apache to apply the changes:
sudo systemctl restart apache2
Conclusion
You’ve successfully installed PHP on Ubuntu! With PHP, Apache, and any necessary modules installed, your server is ready to host PHP-based applications. Whether you’re building a dynamic website or running a CMS like WordPress, your Ubuntu server is now equipped to handle PHP scripts.