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:
- Nginx Wordpress Ubuntu 18.04 Nginx Wordpress Ubuntu 18.04 Introduction to Nginx Nginx is a web server software for hosting websites and applications. It is open-source and highly configurable, making it a popular choice for…
- 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…
- 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…
- Install Phpmyadmin On Nginx Ubuntu Install Phpmyadmin On Nginx Ubuntu What Is Phpmyadmin? Phpmyadmin is a free, open source web application written in PHP specifically designed to help manage MySQL databases. It is a powerful…
- 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…
- Cara Install Nginx Ubuntu 14.04 Cara Install Nginx Ubuntu 14.04 Step 1: Update the Software Packages The first step in installing Nginx is to update the software packages in Ubuntu 14.04. This can be done…
- 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…
- 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…
- 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…
- 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…
- 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…
- Bash Install Nginx On Ubuntu How To Install Nginx On Ubuntu What is Nginx? Nginx is a web server that is gaining popularity in the world of web hosting. Nginx is an open source web…
- Install Rails On Ubuntu Nginx Rbenv Install Rails On Ubuntu Nginx Rbenv Introduction Rails is an open-source web application framework written in Ruby. It is designed to make programming web applications easier by providing a full…
- 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…
- 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 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…
- 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…
- Instal Nginx Ubuntu 18.04 Installing Nginx on Ubuntu 18.04 What is Nginx? Nginx is a lightweight, open source, high-performance web server designed for serving dynamic and static web content. It is capable of handling…
- Nginx 1.10 3 Ubuntu Nginx 1.10 3 Ubuntu Overview of Nginx Nginx (“engine x”) is an open source web server created by Russian software engineer Igor Sysoev and launched in 2004. It is used…
- 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…
- 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 16.04 Nginx Letsencrypt Ubuntu 16.04: A Comprehensive Guide to Nginx and Letsencrypt What is Nginx? Nginx is a web server and an open-source reverse proxy server for HTTP, HTTPS, and other protocols. It…
- 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…
- Install Php Nginx Mysql Ubuntu 18.04 Install Php Nginx Mysql Ubuntu 18.04 What is PHP, Nginx, and MySQL PHP is a popular and powerful scripting language that can be used to create dynamic web pages, web…
- Webdav Nginx Ubuntu 16.04 Webdav Nginx Ubuntu 16.04 Introduction to Webdav with Nginx in Ubuntu 16.04 The ever-evolving world of the internet has given us a powerful platform to share information, media, and files…
- Install Nginx Phpmyadmin Ubuntu 18.04 Install Nginx Phpmyadmin Ubuntu 18.04 Introduction to Nginx, PHP, and Ubuntu Nginx is an open-source, high-performance web server written in C and used to serve static and dynamic webpages. It…
- 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…
- 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…
- 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…
- 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…