If you prefer, you can also check this post in video available on the Hcode channel on YouTube.
We have talked in some articles about installing PHP 8 on Ubuntu. However, this article is a little different, as we will show how to perform its installation and also create an example using this language, but running it through Nginx, one of the most well-known and fastest Web Servers in the world. This will be especially useful when we need to deploy our PHP projects and require a reliable Web Server.
We will perform the installation on a Linux Ubuntu server, hosted on DigitalOcean. This article will focus on PHP 8, but the step-by-step can be used for previous versions as well.
Our article will be divided into the following topics:
1 - Installing Nginx
2 - Installing and Configuring PHP 8
3 - What is PHP-FPM?
4 - Configuring PHP-FPM
5 - Configuring Nginx
6 - Creating the First Code
Installing Nginx
We will update the packages installed on our System using apt-get with the following command:
sudo apt-get update
After that, we will install Nginx with the following command:
sudo apt-get install nginx -y
We will start its service with the command below:
sudo service nginx start
With this command, when accessing the server, we will see the following result in the browser:
Great! Nginx was successfully installed!
Installing and Configuring PHP 8
We will download PHP without using apt-get, but rather through its source on the Internet. We can download it to whichever folder we prefer. In my case, I will download it to the root folder of the System.
The download can be done with the command below:
sudo wget -c https://downloads.php.net/~carusogabriel/php-8.0.0beta1.tar.xz -o -
After the download is complete, we will extract the PHP 8 folder and also rename it to a more pleasant name. We will do this with the following command:
sudo tar -Jxxvf php-8.0.0beta1.tar.xz && sudo mv php-8.0.0beta1 php8-beta1
Now we will access the folder for configuration with the command cd php8-beta1.
Before performing the configuration, we need to install some packages that PHP requires to function well. We will install these packages with the command below:
sudo apt-get install -y build-essential pkg-config libxml2-dev sqlite3 libsqlite3-dev zlib1g libz-dev
Now we are ready to install PHP on Ubuntu. We will do this with the two commands below:
sudo ./configure --enable-fpm --with-pdo-mysql
sudo make && sudo make install
Note that in the first command we added the flag --enable-fpm. This will be essential for the connection between Nginx and PHP. We will talk a little more about FPM shortly.
The next configuration we will perform is to add the php.ini file to our system. We will do this with the command below:
cp php.ini-development /usr/local/php/php.ini
We are doing very well! But before continuing with the configuration, we need to talk about PHP-FPM.
What is PHP-FPM?
When PHP is executed through a Web Server, it needs to communicate with that Server so that the code can actually run and its result reaches the end user. There are many ways to make this connection. One of them is CGI (Common Gateway Interface). It serves as a “middleman” between the PHP language and Nginx.
To facilitate this connection, PHP has a process manager called FPM (FastCGI Process Manager). We need to perform its configuration so that our PHP code works together with Nginx. This is what we will do now.
Configuring PHP-FPM
The first thing we need to do is edit the php.ini file. In this file, there is a configuration called cgi.fix_pathinfo. By default, this configuration has a value of 1. This setting allows Nginx to inform PHP-FPM about requests even if the files do not exist, which can be a security vulnerability. To address this issue, we will set this configuration to 0.
Let's access the php.ini file:
sudo nano /usr/local/php/php.ini
Look for the line we want and set it to 0:
cgi.fix_pathinfo=0
Now we can start configuring FPM. We will copy some default configuration files to our System’s folders. We will use the commands below:
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp sapi/fpm/php-fpm /usr/local/bin
cp sapi/fpm/php-fpm.conf /usr/local/etc/php-fpm.conf
To explain: The first file copied is the FPM configuration file for connection with the servers. The other two files were copied from the folder where we downloaded PHP. They represent the binary of FPM and its general configuration file.
Let's make a change to the www.conf file. To do this, we will access it using the command below:
sudo nano /usr/local/etc/php-fpm.d/www.conf
We will look for the user and group settings and set them with the values below:
user = www-data
group = www-data
Next, we will edit the general configuration file of FPM. We will access it with the command below:
sudo nano /usr/local/etc/php-fpm.conf
In this file, we will edit the last line, making it as follows:
include=/usr/local/etc/php-fpm.d/*.conf
We have made many configurations, and now we are ready to start the PHP-FPM service. For that, just specify the path to its binary with the following command:
/usr/local/bin/php-fpm
With that, FPM will be started ?.
Configuring Nginx
With FPM configured, we just need to configure Nginx.
We will access the configuration file for Nginx's Virtual Hosts with the following command:
sudo nano /etc/nginx/sites-available/default
In this file, we will look for a line that contains all the files that are executed by default in Nginx. This line should be below the root configuration line. We will set it as follows:
index index.php index.html index.htm index.nginx-debian.html;
Note that we added the index.php file so that Nginx actually executes the files of this language.
The next configuration we will perform is in the location ~ \.php$ directive. We will uncomment this configuration block and leave it as follows:
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
After saving this file, we will be ready to start the Nginx service with the new configurations. We will use the following command:
sudo service nginx restart
Creating the First Code
To test if everything went well, how about we create a small example? So let's go!
We will create a file named index.php in the /var/www/html folder with the command below:
sudo nano /var/www/html/index.php
The content of this new file will be as follows:
<?php
echo "<h1>PHP 8 on Nginx</h1>";
phpinfo();
?>
Now, accessing the local address of our server, we will see the following result:
Excellent! Our code worked, and now PHP is being executed together with Nginx on our server ?.
Conclusion
In this article, we learned how to install PHP 8 alongside Nginx. We hope this knowledge can help you in your studies about PHP or in your work as well ?.
We'll see you in the next article, until then.