PHP is a powerful and commonly utilized server-side scripting language used to develop modern websites and apps. If you’re a developer who has recently switched to Ubuntu, you may need to install PHP for your web development projects.
In this article, we’ll explore various methods to install PHP on Ubuntu, discuss its modules, and cover its uninstallation process.
How to Install PHP on Ubuntu
PHP (stands for Hypertext Preprocessor) is designed to handle flexible content, allowing you to interact with databases, process forms, and generate adaptive web pages. Moreover, it integrates with web servers such as Apache and Nginx to manage dynamic content. You can use PHP for web development in Ubuntu.
You can install PHP in Ubuntu using numerous ways. Let’s discuss three straightforward methods.
1. via Official Repository
First, navigate to the terminal and execute the apt update command to update the system respiratory packages:
$ sudo apt update
Next, install PHP and its commonly used extensions from Ubuntu’s official repository:
$ sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
Finally, you’ve successfully installed the PHP on your system.
After installing PHP, you can check its installed version number:
$ php -v
The output indicates that the current PHP version is 8.1.2.
Next, you can test PHP by creating a simple PHP file. To accomplish it, utilize any text editor like Nano, to create and save a PHP file:
$ nano simple.php
Then, copy and paste the below code into your simple.php file:
<?php
echo 'Welcome to Linuxfellas!';
?>
Afterward, run the following PHP command to display the content of your simple.php file:
$ php simple.php;
Welcome to Linuxfellas!
Finally, you’ve successfully executed the PHP file in Ubuntu.
2. via Apache
First, you’ll need to install the system-properties-common package, which is used to manage the software resources. Additionally, it has tools that allow users to add, remove, and manage Personal Package Archives (PPAs) and third-party repositories. To do so, execute the mentioned apt install command:
$ sudo apt install software-properties-common
Next, add the PHP PPA(ppa:ondrej/php) repository, which offers various versions of PHP for the Ubuntu system:
$ sudo add-apt-repository ppa:ondrej/php
Note: The PPA includes PHP versions ranging from 5.6 to 8.2.
Thus, your system is now ready to install any of the available PHP versions.
Moving forward, let’s execute this apt install command to install Apache and PHP:
$ sudo apt install apache2 php8.1 libapache2-mod-php8.1
Finally, you’ve successfully installed Apache and PHP version 8.1 on your Ubuntu system.
Now, you can start and enable the Apache by running the systemctl start command:
$ sudo systemctl start apache2 && sudo systemctl enable apache
Next, execute the systemctl status command to verify the status of Apache:
$ sudo systemctl status apache2
As shown in the above output, Apache is running. Therefore, you can proceed to configure PHP on Ubuntu.
Navigate to the Apache document directory through the cd command:
$ cd /var/www/html
Next, you’re required to create and open a PHP file (apache.php) using any text editor. Here, we’re using the Nano editor. Moreover, you can also name your file as per your preference:
$ sudo nano apache.php
Next, add the beneath code in the apache.php and save the file:
<?php
echo 'welcome to LinuxFellas';
?>
After closing the file, you need to restart the Apache using the systemctl restart command:
$ sudo systemctl restart apache2
Next, open your preferred web browser and enter your system’s IP address followed by the PHP file name in the URL:
http:IPaddress:/apache.php
Finally, you’ve finished configuring PHP with Apache.
3. via Nginx
Head to the terminal and install the system-properties-common package and the PPA (ppa:ondrej/php). Then, use the apt install command to install PHP 8.1 and the essential extensions for Nginx, including php-fpm, php-mysql, and php-xml:
$ sudo apt install php8.1-fpm php8.1-mysql php8.1-xml
Afterward, start and enable the php-fpm as Nginx uses it to process the PHP files:
$ sudo systemctl start php8.1-fpm && sudo systemctl enable php8.1-fpm
Next, check the status of the Nginx server:
$ sudo systemctl status nginx
Evidently, you can observe that Nginx is working.
Next, open the Nginx configuration file to use the PHP, which is located in the Nginx default directory:
$ sudo nano /etc/nginx/sites-available/default
Afterward, you’ve to locate and uncomment the following code from the file:
location \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
After editing and saving the file. Next, you’ll be required to restart your Nginx. To accomplish this, execute the systemctl restart command:
Continuing onward, run the nginx -t command to ensure your Nginx configuration is accurate:
$ sudo nginx -t
Next, reload the PHP through the systemctl reload command:
Now, create and open a PHP file (nginx.php) to verify that PHP is working with Nginx:
Then, add the following code to the nginx.php file:
<?php
echo 'Welcome to LinuxFellas and you are Using PHP for Nginx!';
?>
Next, navigate to your favorite browser and write the system IP address and PHP file name (nginx.php) in the URL:
How To Install PHP Modules on Ubuntu
After installing PHP, you can start using additional modules as needed. There are countless PHP modules, some are listed below as well as their objectives. You can install any of them:
- php-bcmath: for random accuracy mathematics
- php-curl: it’s used to construct HTTP requests from PHP
- php-gd: used for image processing
- php-intl: for internationalization functions
- php-mysql: support the MySQL database
- php-mbstring: this module is useful for non-ASCII characters
- php-soap: this is employed for SOAP web services
- php-xml: utilized for functioning with XML data
- php-zip: it manages the ZIP file compression
Moving forward, let’s discuss the syntax for installing PHP modules on Ubuntu:
$ sudo apt install php<version><module name>
For instance, you can install the php8.1-bcmath by executing the following command:
$ sudo apt install php8.1-bcmath
Additionally, if you desire to obtain the list of installed PHP modules, run the following PHP command:
$ php -m
How To Remove PHP From Ubuntu
To uninstall PHP on Ubuntu, you should first run the apt purge command. It will entirely terminate PHP as well as any configuration files generated during the installation process:
$ sudo apt purge php8.1-fpm
Then, remove its dependencies by executing the apt autoremove command:
$ sudo apt autoremove
Finally, you’ve successfully removed PHP from your Ubuntu system.
Conclusion
In this article, we’ve learned how to install PHP on Ubuntu using various methods, including through the official repository, Apache, and Nginx. We also covered testing and configuring PHP with both Apache and Nginx servers to ensure it’s functioning properly. Additionally, we discussed numerous PHP modules and syntax to install them. Lastly, we covered the uninstallation method for PHP when it’s no longer needed.
Do visit Linux Fellas for more informational content. We’d love to hear your ideas on topics you’d like us to explore in depth.
I’m a technical writer with a Bachelor’s in Computer Science. Through my research and writing, I aim to provide readers with comprehensive, informative articles that can assist them make informed decisions about their technological needs.