Install Nginx And Php On Ubuntu 18.04


Install Nginx And Php On Ubuntu 18.04

Introduction

In this article, we will guide you on how to install Nginx and php on Ubuntu 18.04. Nginx is a popular web server that is designed to serve large amounts of static content quickly and efficiently. Nginx is also known for its stability, reliability, and low resource consumption. It is often used as a reverse proxy for Apache web servers. PHP is an open source scripting language commonly used in web development and can be used with Nginx.

Prerequisites

Before you begin, you will need to have an Ubuntu 18.04 server with a non-root user with sudo privileges configured. If you don’t have one, you can follow our Initial Server Setup with Ubuntu 18.04 guide to learn how to setup a user with the correct permissions.

Install Nginx

Before installing Nginx, you should make sure that all packages on your system are up to date. To do this, you can run the following command:

sudo apt update
sudo apt upgrade

Once all packages are up to date, you can install Nginx using apt. You can do this with the following command:

sudo apt install nginx

Once the installation is complete, you can start the Nginx service and enable it to automatically start when the server boots with the following commands:

sudo systemctl start nginx
sudo systemctl enable nginx

After Nginx has been installed, you should check if it is running correctly. To do this, you can enter the following command:

sudo systemctl status nginx

If everything is working correctly, 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: en
Active: active (running) since Fri 2018-12-14 11:00:00 UTC; 45s ago
Main PID: 32844 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─32844 nginx: master process /usr/sbin/nginx -g daemon on; master_p
└─32845 nginx: worker process

Install PHP

Now that Nginx is up and running, you can install PHP on your server. To do this, you will first need to install some additional packages. These packages are required for PHP to work correctly.

sudo apt install php-fpm php-mysql

Once the installation is complete, you can start the PHP-FPM service and enable it to automatically start when the server boots with the following commands:

sudo systemctl start php7.2-fpm
sudo systemctl enable php7.2-fpm

Now that the PHP-FPM service is running, you can configure Nginx to use it.

Configure Nginx To Use PHP-FPM

Now that Nginx and PHP-FPM are installed, you will need to configure Nginx to use the PHP processor to handle PHP files. To do this, you will need to edit the default configuration file located in the /etc/nginx/sites-enabled/ directory.

The default configuration file should look something like this:


server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

To configure Nginx to use PHP-FPM, you will need to add a few lines to the configuration file. The first line you need to add is the location directive:

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

Once you have added the location directive to the configuration file, you will need to save and close it. After that, you can test the configuration file for errors with the following command:

sudo nginx -t

If the configuration file is valid, you should see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the configuration test is successful, you can restart the Nginx service with the following command to apply the changes:

sudo systemctl restart nginx

Test the Configuration

Now that Nginx and PHP-FPM are configured, you can test the configuration. To do this, you will need to create a PHP file in the /var/www/html directory. You can create a file with the following command:

sudo nano /var/www/html/info.php

Once the file is opened, you can add the following code to the file:


phpinfo();
?>

Once you have added the code to the file, you can save and close it. After that, you can access the file in your web browser using the server’s IP address and the filename:

http://server_ip_address/info.php

If everything is working correctly, you should see a page similar to the one below:

Conclusion

In this article, you have learned how to install Nginx and PHP-FPM on Ubuntu 18.04. You have also learned how to configure Nginx to use PHP-FPM and how to test the configuration. If you have any questions or comments, please leave them below.

Thank You for Reading this Article

If you enjoyed this article or found it helpful, please consider sharing it. We also encourage you to read some of our other articles about Linux and Nginx.

Leave a Reply

Your email address will not be published. Required fields are marked *