Nginx is a free, open-source and powerful web server that was developed with speed and scalability in mind. It is capable of serving hundreds of thousands of clients simultaneously. Nginx uses the PHP Fast Process Manager (PHP-FPM) to run PHP scripts and applications. PHP-FPM is much faster than traditional CGI-based multi-user PHP environments and also allows hosting multiple applications with different PHP versions.
In this article, we will show you how to install Nginx with PHP-FPM on an Ubuntu 22.04 server.
Requirements
- A server running Ubuntu 22.04.
- A valid domain name is directed to the IP of your server.
- A root password is set up on your server.
Installing the Nginx web server
The Nginx web server package is included in the standard Ubuntu repository. You can therefore easily install it with the following command:
apt install nginx -y
After you have installed the Nginx web server package, start the Nginx service with the following command:
systemctl start nginx
You can check the status of Nginx with the following command:
systemctl status nginx
You should see the following output:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-11-04 13:58:34 UTC; 16s ago Docs: man:nginx(8) Main PID: 46938 (nginx) Tasks: 3 (limit: 4579) Memory: 8.2M CPU: 63ms CGroup: /system.slice/nginx.service ??46938 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??46940 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ??46941 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Nov 04 13:58:34 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server... Nov 04 13:58:34 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
To check the Nginx version, run the following command:
nginx -V
You should see the Nginx version in the following output:
nginx version: nginx/1.18.0 (Ubuntu) built with OpenSSL 3.0.2 15 Mar 2022
Now Nginx is installed and running. Now open your web browser and check the Nginx test page with the URL http://your-server-ip. You should see the Nginx test page on the following screen:
Install PHP-FPM
Next, you’ll need to install PHP and PHP-FPM to run the PHP code over the internet. You can install both packages with the following command:
apt install php php-fpm -y
Once the installation is complete, check the PHP version with the following command:
php --version
You should see the following output:
PHP 8.1.2-1ubuntu2.6 (cli) (built: Sep 15 2022 11:30:49) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.6, Copyright (c), by Zend Technologies
Next, start the PHP-FPM service with the following command
systemctl start php8.1-fpm
You can check the status of PHP-FPM with the following command:
systemctl status php8.1-fpm
You should see the following output:
? php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-11-04 14:00:10 UTC; 38s ago Docs: man:php-fpm8.1(8) Process: 56127 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=e> Main PID: 56123 (php-fpm8.1) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 4579) Memory: 7.2M CPU: 73ms CGroup: /system.slice/php8.1-fpm.service ??56123 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""> ??56125 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ??56126 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> Nov 04 14:00:10 ubuntu2204 systemd[1]: Starting The PHP 8.1 FastCGI Process Manager... Nov 04 14:00:10 ubuntu2204 systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
Configure Nginx for PHP-FPM
Next, create a configuration file for an Nginx virtual host to host a PHP website.
nano /etc/nginx/conf.d/test.conf
Add the following configurations:
server { listen 80; server_name test.example.com; root /var/www/html/; access_log /var/log/nginx/example.com-access.log; error_log /var/log/nginx/example.com-error.log error; index info.php index.htm index.php; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { fastcgi_split_path_info ^(. .php)(/. )$; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
Save and close the file, then create a simple PHP site with the following command:
nano /var/www/html/info.php
Add the following code:
Save and close the file and check the Nginx configuration with the following command:
nginx -t
You should get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Then restart the Nginx service to apply the changes:
systemctl restart nginx
Check PHP page
Now Nginx is configured with PHP-FPM to execute PHP code. You can now test it with the URL http://your-server-ip . You should see the PHP test page on the following screen:
Conclusion
Congratulations! You have successfully installed Nginx with PHP-FPM on Ubuntu 22.04. You can now use PHP-FPM to host a PHP-based website on the Nginx web server. You can also use PHP-FPM to run multiple PHP versions. If you have any questions, feel free to contact me.