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.
Related Posts:
- Failed To Start Nginx Service In Ubuntu Failed to Start Nginx Service in Ubuntu What is Nginx? Nginx is an open-source web server written in C, designed for high performance and stability. Nginx is one of the…
- Install Phpmyadmin Nginx Ubuntu 16.04 Install Phpmyadmin Nginx Ubuntu 16.04 What is PhpMyAdmin? PhpMyAdmin is an open-source software for managing MySQL databases. It can be used to add, modify, and delete databases, tables, and columns.…
- Check Nginx Status Ubuntu 16.04 Check Nginx Status Ubuntu 16.04 What is Nginx? Nginx (pronounced "engine x") is an open source web server that is used for hosting websites and applications on a wide range…
- How Use Nginx Mysql Ubuntu How to Use Nginx, MySQL and Ubuntu What is Nginx? Nginx is an open source web server and reverse proxy software written by Igor Sysoev. It is a popular choice…
- How To Install Phpmyadmin In Ubuntu 18.04 Nginx How To Install Phpmyadmin In Ubuntu 18.04 Nginx Introduction The MySQL database management system is one of the most popular and powerful open source database systems available today. To make…
- Ubuntu 18.04 Letsencrypt Nginx Ubuntu 18.04 Letsencrypt Nginx What is Ubuntu and Why is it Used for Nginx? Ubuntu is a Linux-based operating system designed for open-source use. It is regularly updated, secure, and…
- Certbot Ubuntu 18.04 Nginx Certbot Ubuntu 18.04 Nginx Introduction to Certbot in Ubuntu 18.04 Certbot, formerly known as Let’s Encrypt, is an open-source certificate authority (CA) managed by the Internet Security Research Group (ISRG).…
- Install Nginx Ubuntu 16.04 For Odoo Install Nginx Ubuntu 16.04 For Odoo Install Ubuntu Server 16.04 Before we can install Nginx for Odoo we need to install Ubuntu 16.04 on our server. Ubunutu 16.04 is still…
- Nginx Ssl Configuration Ubuntu 18.04 Nginx Ssl Configuration Ubuntu 18.04 What is Nginx? Nginx is an open source web server and reverse proxy software. It is used to manage web traffic on the internet, like…
- Setting Up Passenger And Nginx Ubuntu 18.04 Setting Up Passenger And Nginx Ubuntu 18.04 Installing the Passenger Gem Passenger is an open source framework created by Phusion which allows easy integration of Nginx with Ruby applications such…
- Virtual Host Nginx Ubuntu 16.04 Virtual Host Nginx Ubuntu 16.04 Introduction A virtual host (also known as Virtual Private Server or VPS) is a service that allows a single physical server to host multiple websites.…
- How To Set Rails On Nginx Ubuntu 18.04 How To Set Rails On Nginx Ubuntu 18.04 Step 1: Install Ruby Using RVM The first step for setting up Ruby on Rails on an Ubuntu 18.04 server with Nginx…
- Install Nginx Ubuntu 18.08 Local Install Nginx on Ubuntu 18.04 Local Installing Nginx on Ubuntu 18.04 can be accomplished quickly and in several different ways. Depending on your needs, some methods may be better suited…
- Install Nginx Ubuntu Server 16.04 Install Nginx Ubuntu Server 16.04 Introduction to Nginx Nginx is a high performance web server and reverse proxy. It is normally used for serving static content such as images, static…
- Install Ssl Certificate Ubuntu 18.04 Nginx Install Ssl Certificate Ubuntu 18.04 Nginx Introduction To SSL And Why We Need It SSL (Secure Sockets Layer) is a security technology commonly used on the Internet to securely transmit…
- Nginx Php Fpm 7.2 Nginx Php Fpm 7.2 Introduction to Nginx and PHP-FPM Nginx is an open-source web server, reverse proxy server, and load balancer. It is known for being lightweight and fast, and…
- Ubuntu Install Nginx Php7.2 Mysql Ubuntu Install Nginx Php7.2 Mysql What is Nginx? Nginx is an open source web server that is very popular for powering web applications such as WordPress and Drupal. It is…
- Remove Apache2 Ubuntu And Change To Nginx Remove Apache2 Ubuntu And Change To Nginx What Is Apache2? Apache2 is an open-source web server software created and maintained by the Apache Software Foundation. It is one of the…
- Setting Php Nginx Ubuntu Vps Setting Up a PHP, Nginx, and Ubuntu VPS What is a VPS? A Virtual Private Server (VPS) is a type of virtualized hosting. It works in the same way as…
- Install Php5 Nginx Ubuntu 18.04 Install PHP5 Nginx Ubuntu 18.04 Understanding The Basics of Nginx and PHP Before we learn how to install Nginx and PHP on Ubuntu 18.04, it’s important to understand the basic…
- Remove Nginx And Install Apche Ubuntu 18.04 Remove Nginx And Install Apche Ubuntu 18.04 Introduction The world of web servers is quite vast and different web servers have different applications and preferences. If you are new to…
- Install Nginx In Ubuntu 16.04 Terminal Install Nginx On Ubuntu 16.04 Terminal Nginx (pronounced "engine x") is a lightweight web server that is becoming the most popular way to serve content on the web. It is…
- Install Nginx Php Mysql Phpmyadmin Ubuntu 18.04 Install Nginx Php Mysql Phpmyadmin Ubuntu 18.04 Introduction In this article, we will demonstrate how to install Nginx, PHP, MySQL, and phpMyAdmin on an Ubuntu 18.04 server. Nginx is a…
- Ubuntu Install Web Server Nginx Ubuntu Install Web Server Nginx Step 1: Install the Nginx Package The first step when installing Nginx on Ubuntu is to install the Nginx package from the Ubuntu repository. This…
- Available Application Apache And Nginx Available Application Apache And Nginx What is Apache? Apache is a web server software that allows users to store and deliver web content quickly and efficiently. Apache was created in…
- Ubuntu Set Ip For Nginx Ubuntu Set IP For Nginx Introduction to Setting IP For Nginx Nginx is one of the most popular web servers used today. It is open source, fast and reliable. Nginx…
- Digitalocean Letsencrypt Nginx Ubuntu 18.04 Digitalocean Letsencrypt Nginx Ubuntu 18.04 Introduction Ubuntu 18.04 is the latest version of the popular Linux operating system. Digitalocean is a cloud hosting provider that specializes in hosting and managing…
- Nginx Ubuntu Access Virtual Host From Another… Nginx Ubuntu Access Virtual Host From Another Machine Over LAN Are you trying to access a virtual host set up using Nginx on Ubuntu from another machine over a LAN?…
- Install Nginx Php Java On Centos 7 Install Nginx Php Java On Centos 7 What is Nginx? Nginx is an open source Web server created to serve Web traffic efficiently, reliably, and quickly. Nginx has become the…
- Tutorial Start Nginx Ubuntu Webserver Tutorial Start Nginx Ubuntu Webserver Step 1: Prerequisites Before we start configuring Nginx as a web server on Ubuntu, we need to make sure we have the following prerequisites: A…