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:
- Install Php Nginx Ubuntu 12.04 Install Php Nginx Ubuntu 12.04 Introduction Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS…
- How To Install Nginx On Ubuntu 18.04 How To Install Nginx on Ubuntu 18.04 Introduction Nginx is a popular open-source web server and reverse proxy software that’s used by millions of websites. It’s the most widely used…
- Hhvm Nginx Ubuntu 16.4 HHVM Nginx Ubuntu 16.4 What is HHVM? HHVM, also known as HipHop Virtual Machine, is a virtual machine developed by Facebook to speed up the execution of PHP code. It…
- How To Login Phpmyadmin In Nginx Server Ubuntu How To Login Phpmyadmin In Nginx Server Ubuntu What is Nginx & phpMyAdmin Nginx is a web server software that is commonly used on Linux-based systems, though it works on…
- Install Laravel 5.8 Nginx Php7.3 Install Laravel 5.8 Nginx Php7.3 Overview Installing Laravel 5.8 on a Nginx server running PHP 7.3 can be a tricky task. This tutorial explains how to install the popular open…
- Install Odoo 11 Nginx Ubuntu 16 Install Odoo 11 Nginx Ubuntu 16 What is Odoo 11 Nginx? Odoo 11 Nginx is an open source software package designed to facilitate secure and reliable web development. It is…
- 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…
- Install Nginx Php Oracle Ubuntu Install Nginx, Php, Oracle, Ubuntu Installing Nginx on Ubuntu Nginx is a popular web server software based on the LEMP stack that is widely used to host web applications, especially…
- Install Laravel Nginx Ubuntu 18.04 Install Laravel Nginx on Ubuntu 18.04 Introduction In this tutorial, we will look at how to install Laravel with Nginx on an Ubuntu 18.04 server. Laravel is a powerful PHP…
- How To Install Nginx On Linux Ubuntu How To Install Nginx On Linux Ubuntu What is Nginx? Nginx is a lightweight web server that is widely used for its ability to handle requests efficiently and quickly. It…
- Nginx Command Not Found Ubuntu Nginx Command Not Found Ubuntu What is Nginx? Nginx (pronounced as Engine X) is an open source web server created to handle high traffic network applications, replacing traditional web servers…
- How To Install Nginx In Ubuntu How To Install Nginx In Ubuntu Introduction to Nginx Nginx is a very powerful web server for hosting websites and applications. It is a fast and reliable server, and is…
- Install Nginx Passenger Ubuntu 16.04 Install Nginx Passenger Ubuntu 16.04 Installing Nginx On Ubuntu 16.04 Nginx is an open source web server that can be used to create web and application servers. It is a…
- Ubuntu Nginx Php-Fpm Short Cut Ubuntu Nginx Php-Fpm Short Cut What Is Nginx? Nginx is a web server software package developed by Igor Sysoev for use with the Linux operating system. It is open source…
- Install Laravel Ubuntu 16.04 With Nginx Web Server Install Laravel Ubuntu 16.04 With Nginx Web Server Introduction Laravel is one of the most popular expressive, elegant, and robust PHP frameworks available today. It is an open source framework…
- 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…
- 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 Deactive Nginx Ubuntu How To Deactivate Nginx Ubuntu Introduction Nginx is a web server that’s popular in the Linux world because of its simple configuration, scalability, and performance. The Apache httpd web server…
- Ubuntu Server Postgresql Nginx Php Ubuntu Server Postgresql Nginx Php Introduction Optimizing your server is one of the most important steps you can take when it comes to running a successful website or application. It…
- 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…
- Php-Fpm Nginx Ubuntu 18.04 PHP-FPM & Nginx on Ubuntu 18.04 Introduction to PHP-FPM PHP-FPM (FastCGI Process Manager) is an implementation of FastCGI, which is a standard protocol for interfacing external applications with web servers.…
- Linux Nginx Mysql Php Lemp Linux Nginx Mysql Php Lemp What is Linux, Nginx, MySQL, PHP (LEMP)? Linux, Nginx, MySQL, PHP (LEMP) is a software stack combination of Linux, Nginx, MySQL, and PHP, commonly referred…
- Install Nginx In Ubuntu 16.04 Install Nginx In Ubuntu 16.04 Overview of Nginx Nginx (pronounced "engine-x") is an open-source Web server that is designed to provide a balance of flexibility, performance, and scalability. It is…
- 403 Forbidden Nginx Ubuntu 14.04 403 Forbidden Nginx Ubuntu 14.04 What Is a 403 Forbidden Error? A 403 Forbidden error is an HTTP status code that denotes that a server, upon receiving a request from…
- Site-Available Nginx Whas Delete Ubuntu Site-Available Nginx What Delete Ubuntu What is Nginx and how does it work with Ubuntu? Nginx is a web server designed for high performance, scalability, and reliable internet connectivity. It…
- Ubuntu 18.04 Nginx Php Mariadb Ubuntu 18.04 Nginx, Php, and Mariadb Understanding Ubuntu 18.04 Ubuntu 18.04 is the latest version of the popular open-source Linux distribution. It is based on Debian and is known for…
- Nginx Php 5.6 Module Ubuntu 16.04 Nginx Php 5.6 Module Ubuntu 16.04 Overview of Nginx Nginx is a powerful web server that is used to serve both static and dynamic web content. It has become increasingly…
- Install Nginx Php Mysql Ssl & Wordpress On Ubuntu 18.04 Install Nginx Php Mysql Ssl & Wordpress On Ubuntu 18.04 What is Nginx, Php, Mysql, SSL and Wordpress? Nginx is a high-performance web server that is widely used to serve…
- Laravel Nginx Config Ubuntu 18 Laravel Nginx Config Ubuntu 18 Introduction Laravel is a powerful web-based MVC (Model-View-Controller) framework used by developers to create web applications, websites and APIs. It is based on the popular…
- 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…