Install Nginx And Php On Ubuntu

Install Nginx And Php On Ubuntu

A Comprehensive Tutorial to Install Nginx And Php On Ubuntu

Nginx and PHP are two of the most popular web server and scripting language platforms in the world. While PHP is a server-side scripting language, Nginx is a robust webserver with a wide range of features that allow it to serve webpages with speed and reliability. This tutorial will help you install Nginx and PHP successfully on Ubuntu in a few easy steps.

Overview of Nginx and PHP

Nginx is an open-source web server, reverse proxy, and mail proxy platform available for Linux and other Unix-like operating systems. Nginx is well known for its high performance and low resource utilization which makes it an ideal choice for large-scale web applications. Nginx can be used to deliver static content such as HTML, CSS, and JavaScript as well as dynamic content such as PHP Applications.

PHP is a scripting language used for developing server-side applications. It is one of the oldest and most popular web scripting languages available and is frequently used to develop web applications and websites. PHP comes in two main versions: PHP 7 and PHP 5. For this tutorial, we will be installing PHP 7.

How to Install Nginx and PHP on Ubuntu

To install Nginx and PHP on Ubuntu, you will need to install the other required packages first. To do this, open up a terminal window and run the following command:

sudo apt-get install nginx php7.2 php7.2-fpm php7.2-opcache php7.2-xml php7.2-curl php7.2-imap php7.2-mysql php7.2-cli php7.2-dev

This command will install all of the necessary packages for running Nginx and PHP 7 on Ubuntu. After the packages have been installed, you will need to configure Nginx’s virtual hosts. Virtual hosts will allow you to serve multiple different websites on the same server by using different domain names. To do this, open up the /etc/nginx/sites-enabled/default file and add the following to the file:

server {

server_name domain_name;

root /path_to_website/;

index index.php index.html index.htm;

location ~ .php$ {

try_files $uri =404;

fastcgi_pass unix:/var/run/php7.2-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;



Save the file and then restart the Nginx service with the following command:

sudo service nginx restart

This will restart the Nginx service and the new virtual host configuration will be active. Finally, we need to ensure that PHP is properly configured. To do this, open up the /etc/php/7.2/fpm/php.ini file and set the error reporting, display errors, and log errors to be On and save the file. Then restart the PHP-FPM service with the following command:

sudo service php7.2-fpm restart

That’s it! You now have a fully configured web server running both Nginx and PHP on Ubuntu.

Testing the Installation

To test your installation, create a new file called index.php in the root of your website and add the following code to the file:


Save the file and then open your website in a web browser. You should see a page containing information about your PHP installation. If you see this page, then your installation was successful.


If you run into any problems while installing Nginx and PHP on Ubuntu, the following steps may help you identify and fix the issue.

  • Check for any errors in the /var/log/nginx/error.log file. This file will contain detailed information about any errors that may have occurred during the installation process.
  • Ensure that the Nginx configuration files are properly set up. Errors in the Nginx configuration files can cause the web server to not start properly.
  • Check the system logs for any errors related to PHP. If PHP is not properly configured or installed, you will see errors related to PHP in the system logs.
  • Ensure that all of the necessary packages have been installed. If any of the necessary packages are not installed, PHP may not function properly.
  • Check your firewall settings. Firewalls can cause webpages to not load properly or slow down web server performance.


This tutorial has shown you how to install Nginx and PHP on Ubuntu. You should now have a functioning web server running both Nginx and PHP. If you encounter any issues during the installation process, keep these troubleshooting tips in mind. Thank you for reading this article. Please read other articles on our website for more great tips on using Linux.