In this guide we will cover steps to install PHP 7.4 in Ubuntu 20.04 and set up php-fpm
to serve that traffic with the help of Nginx.
PHP is a general-purpose scripting language. It is mostly popular for web development. It can be used to develop dynamic and interactive websites. It was among the first server-side languages that could be embedded into HTML, making it easier to add functionality to web pages without needing to call external files for data. PHP is among the easier programming languages to learn.
It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.
At the time of writing this article, the default Ubuntu 20.04 repositories include PHP 7.4 version.
Prerequisites
To follow along this guide, ensure that you have the following:
- Updated Ubuntu 20.04/22.04 server that is connected to the internet
- Root access to the server or user with sudo access
- You should comfortable with using Linux terminal to execute commands.
Ensure that the server is up to date
Before proceeding, let us ensure that our server is updated. Use these commands to 1. Update the server repositories, 2. Upgrade server packages to the latest releases:
|
|
Install php 7.4 on the server
Since the default repositories contain php version 7.4, let’s install it with this command:
|
|
Confirm the installed version with the command php -v
:
|
|
In this set up, we intend to serve php files with Nginx. Nginx doesn’t have built-in support for processing PHP files. We’ll use PHP-FPM (“fastCGI process manager”) to handle the PHP files.
Nginx will not work if Apache is installed and running. Since we will be using Nginx, let’s use this command to remove apache if it is installed in our sytstem:
|
|
Let’s now install php-fpm
with this command:
|
|
Once the installation is completed, the FPM service will start automatically. The service is added to systemd as php7.4-fpm
. To check the status of the service, run:
|
|
You should see output like above.
Installing Nginx
Now that PHP and php-fpm are installed in the system, we need to install nginx. Nginx will allow us to set up a virtual host that we can use to serve traffic from a domain.
Install nginx using this command:
|
|
Once the installation is done, Nginx will be started by default. Confirm that it is working as expected by checking its status. You should see output like this:
|
|
To confirm that Nginx is actually running and is reachable, use this command:
|
|
Where server_ip
is the ip of your server. If by any reason you do not know the public ip of your server use this to check:
|
|
You should see the Nginx default page.
Configure Nginx to serve php file
Now that we have Nginx set up and running in our server, let’s make it serve php files. In this example we will configure Nginx to serve the php info page.
To achieve this, we need to set up a virtual host in Nginx serving traffic for a specific domain (we will use example.citizix.com
). For this to work you need to update the DNS records for that domain in your dns host to have A
records point to that domain or subdomain. Consult your DNS providers documentation for the specifics of how to do that.
Confirm that the domain can resolve correctly to the server using the dig command:
|
|
Now we need to follow these steps to have our simple script working:
- Create the directory to store our static content (we will use
/var/www/example.citizix.com
)
|
|
- Ensure the created directory is accessible by the Nginx user
|
|
- Add an index file
/var/www/example.citizix.com/index.php
with content to show php info to the directory (/var/www/example.citizix.com
). Edit the file:
|
|
Add this content:
|
|
Save and close the file.
- Create nginx configuration in the nginx default directory
/etc/nginx/conf.d
. 1. We will create the file/etc/nginx/conf.d/example.citizix.com.conf
with the required content.
|
|
Add this content:
|
|
Confirm that nginx config is correct with this command:
|
|
Now you need to restart nginx and php-fpm using these commands:
|
|
Confirm that the services came up:
|
|
You should see that the services restarted successfully!
If all goes well, you should see the php info page when you load the site url.
That’s all! Up to this point we have been able to install php 7.4 in Ubuntu 20.04 and serve simple file with Nginx.