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…
- 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…
- 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…
- 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…
- 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…
- 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…
- Ubuntu 16.04 Nginx Ipv6 Ubuntu 16.04 Nginx Ipv6 What is Ubuntu 16.04? Ubuntu 16.04 is a version of the Ubuntu Linux operating system for computers and servers. Ubuntu has been around since 2004, and…
- 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…
- Stop Nginx Ubuntu 16.04 Stop Nginx Ubuntu 16.04 What is Nginx? Nginx is a web server and reverse proxy software. It is open source and widely used on the web. It is used to…
- 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…
- Deploy Laravel Nginx Ubuntu 18 Deploy Laravel Nginx Ubuntu 18 Intro to Laravel Laravel is a free, open-source, Model-View-Controller (MVC) web framework written in PHP. It has become one of the most popular web development…
- Create Wordpress With Nginx Mariadb Php-Fpm 16.04 Create WordPress With Nginx Mariadb Php-Fpm 16.04 What Is WordPress? WordPress is a content management system (CMS) and a blogging platform. It allows you to easily create and manage websites…
- How To Ubuntu 16.04 Multiple Php Nginx How to Ubuntu 16.04 Multiple PHP Nginx Ubuntu 16.04 is a robust operating system that is commonly used for web application development. This operating system uses Nginx as the web…
- Ubuntu 18 Enable Php Mysql Nginx Ubuntu 18 Enable Php Mysql Nginx Introduction: What Is Ubuntu? Ubuntu is an open source operating system based on the Debian GNU/Linux distribution. Built around the Linux kernel and released…
- Nginx Php-Fpm Php Stack Overflow Articel Nginx Php-Fpm Php Stack Overflow Articel What Is Nginx? Nginx (pronounced engine-x) is an open source web server and reverse proxy software that is popular for its high performance and…
- Ubuntu Server18 How To Enable Nginx Pdo Mysql Ubuntu Server18: How to Enable Nginx Pdo Mysql As a developer, you may have heard of Nginx, PDO, and MySQL – all are essential components of web applications. Nginx is…
- Digital Ocean Ubuntu Server Nginx Docker Digital Ocean Ubuntu Server Nginx Docker Understanding Digital Ocean Digital Ocean is a cloud service provider that focuses on simplifying web infrastructure for cloud developers. They offer a platform where…
- 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…
- 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…
- Default Webserver Nginx Ubuntu 18.04 Default Webserver Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source web server that is used to host websites or act as a reverse proxy for other web…
- Reverse Proxy Nginx Ubuntu 18.04 Reverse Proxy Nginx Ubuntu 18.04 Introduction Reverse Proxy is a type of proxy server that forwards requests to another server. It is mainly used by web servers, such as Nginx,…
- Google Cloud Ubuntu 16 Install Nginx Php Google Cloud Ubuntu 16 Install Nginx Php 1. What is Nginx? Nginx is a powerful open source web server. It is used to serve web pages to the internet and…
- 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…
- Install Varnish Ubuntu 16.04 Nginx Install Varnish Ubuntu 16.04 Nginx What is Varnish? Varnish is a web application accelerator which is also known as a caching HTTP reverse proxy. It's much faster than traditional web…
- How To Install Nginx Ubuntu How To Install Nginx Ubuntu What is Nginx? Nginx is an open-source web server, reverse proxy, load balancer, and HTTP cache solution with a strong focus on speed and performance.…
- 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…
- 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…
- Php Run On Server Apache Nginx PHP Run on Server Apache Nginx What is PHP? PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source scripting language that can be used to create dynamic…
- 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…
- Step By Step Install Nginx And Phpmyadmin On Ubuntu Step By Step Install Nginx And Phpmyadmin On Ubuntu Introduction Setting up a web server on Ubuntu is easy and straightforward. Nginx is a popular web server for Ubuntu, and…