Replace Apache To Nginx Ubuntu 18
Installing Nginx
Ubuntu 18 uses Nginx as its default web server, and it is pre-installed. However, if you want to use a fresh version of Nginx, you can upgrade by using the following commands:
First, update the apt repository with the following command:
sudo apt update
Once the update is completed, install nginx using the following command:
sudo apt -y install nginx
Verify if the installation of nginx is successful by executing the following command:
sudo nginx –v
The output of the command should be similar to the following:
Nginx version: nginx/1.14.0 (Ubuntu)
Starting Nginx
After the installation of Nginx is complete, start Nginx by executing the following command:
sudo systemctl start nginx
You can check the status of Nginx web server using the following command:
sudo systemctl status nginx
The output of the command should be similar to the following:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-09-22 04:50:45 EDT; 3s ago
Docs: man:nginx(8)
Configuring Nginx
Once the Nginx web server is up and running, you have to configure it to serve the web content. Nginx configuration files are located in the /etc/nginx directory. In this directory there are two configuration files that are of particular interest: nginx.conf and sites-available.
If you open the nginx.conf file, you will see the following configuration:
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;
}
}
This basically means that when web server receives a request, it will look for the relevant file in the /var/www/html directory. The files should have one of the following extensions: .html, .htm or .nginx-debian.html. If the file is not found, an error 404 will be returned.
Configure Virtual Hosts
In order to configure the web server to serve more than one domain, you need to create the relevant virtual hosts. To create the virtual host, first create a directory in /var/www with the name of the domain:
sudo mkdir -p /var/www/example.com/public_html
Then set the permissions of the directory to allow the web server to access it:
sudo chown -R $USER:$USER /var/www/example.com/public_html
Next, create a configuration file for the virtual host in /etc/nginx/sites-available directory. The configuration file should be named after the domain name:
sudo nano /etc/nginx/sites-available/example.com
And paste the following configuration in the file:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
Finally, enable the virtual host:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Removing Apache Web Server
Once the Nginx web server is properly configured and working, it is time to remove the old Apache web server. To remove the Apache web server, execute the following command:
sudo apt purge apache2
This command will remove all packages related to Apache web server. After installation is completed, remove the old configuration files:
sudo rm -fr /etc/apache2
Testing Nginx
To test if the Nginx web server is working, open your web browser and go to http://localhost. You should see the default Nginx web page. To test a domain name, you have to add an entry in the /etc/hosts file. For example, if you want to test the domain example.com, add an entry like this:
127.0.0.1 example.com www.example.com
Then open http://example.com in a web browser, and you should see the web page that you created in the /var/www/example.com/public_html directory.
Conclusion
In this tutorial, we learned how to replace Apache web server with Nginx web server on Ubuntu 18. We have learned how to install Nginx, configure it, create virtual hosts and how to test the web server. It is recommended that you go through the documentation of Nginx to learn more about the configuration options.
Thank you for reading this article. Please read other articles to learn more about web servers and their configurations.
Related Posts:
- How To Install Cachet Nginx How To Install Cachet Nginx Overview Cachet is an open-source monitoring platform that is widely used by web developers, DevOps engineers, and system administrators for monitoring the performance of a…
- Ubuntu Nginx Fastcgi 7.2 Ubuntu Nginx Fastcgi 7.2 What is Ubuntu Nginx Fastcgi? Ubuntu Nginx Fastcgi is an open-source web server that is used to provide web hosting services on Ubuntu Linux. It provides…
- Error Nginx When Installing Certbot Ubuntu Error Nginx When Installing Certbot Ubuntu What is Nginx? Nginx is a web server software often used to serve web pages. It is known as a high-performance web server and…
- Install Mariadb Nginx Debian 9.7 Install Mariadb and Nginx on Debian 9.7 Overview of Installing MariaDB and Nginx on Debian 9.7 Installing MariaDB and Nginx on Debian 9.7 can be an essential step when setting…
- 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…
- 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…
- 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…
- Digitalocean Install Nginx Ubuntu 18.04 Digitalocean Install Nginx Ubuntu 18.04 Introducing DigitalOcean and Nginx DigitalOcean is a virtual cloud server provider, allowing users to spin up virtual servers located in various geographical locations throughout the…
- How To Install Nginx And Mariadb 10 How To Install Nginx And Mariadb 10 What is Nginx and MariaDB 10 Nginx is a free, open-source web server that is known for its scalability and performance. It is…
- Reconfigured Nginx To Use Php7.2 Reconfigured Nginx To Use Php7.2 Introduction Nginx is one of the most popular and powerful web servers available today. It is often used as the primary web server for large…
- 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…
- Install Postgis Ubuntu 18.04 Nginx Install Postgis Ubuntu 18.04 Nginx What is Postgis? Postgis is a Postgres-based open source geographic information system (GIS) that enables users to store, query, and analyze spatial data stored in…
- Deploy Laravel Nginx Ubuntu 17 Deploy Laravel Nginx Ubuntu 17 Requirements for Installing Laravel 5.4 on Ubuntu 17 This article will guide you through the process of installing Laravel 5.4 on Ubuntu 17. Before we…
- Install Nginx Postgres Centos 7 Install Nginx Postgres Centos 7 Introduction to Nginx and Postgres Nginx is an open source web server that has been gaining in popularity in recent years due to its reliability,…
- Restart Nginx Ubuntu 16.04 Restart Nginx Ubuntu 16.04 Why do you Need to Restart Nginx? When you deploy your web application, sometimes you need to restart your web server (Nginx) to ensure that all…
- Install Nginx 1.16 Ubuntu Install Nginx 1.16 Ubuntu Introduction to Nginx 1.16 Nginx 1.16 is a web server that helps you to serve web content quickly, efficiently and securely. It is especially popular with…
- 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.…
- Install Nginx Mysql Phpmyadmin Ubuntu Install Nginx Mysql Phpmyadmin Ubuntu 1. Introduction In today's tutorial, we will show you how to install Nginx, MySQL, and phpMyAdmin on Ubuntu 20.04. Nginx is an open source web…
- 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…
- 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…
- How To Setup Nginx On Ubuntu How To Setup Nginx On Ubuntu Introduction Nginx is a powerful web server that is very popular among Linux users. It is open-source and comes with great features such as…
- 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…
- 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…
- 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.…
- 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 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…
- Upgrade Nginx Ubuntu 18.04 Upgrade Nginx Ubuntu 18.04 Getting Started with Nginx Ubuntu 18.04 Installation Nginx is a high performance web server and reverse proxy. It is written in C and has ways to…
- Install Mysql Pada Nginx Pada Ubuntu Install Mysql Pada Nginx Pada Ubuntu Overview of Nginx and Its Benefits Nginx is a web server, reverse proxy server, and mail proxy server application that is open-source and available…
- Install Nginx Latest Ubuntu 16.04 Install Nginx Latest Ubuntu 16.04 What is Nginx? Nginx is a web server. It is also an open source and free software. It was first developed by Igor Sysoev in…
- How To Install Nginx Maridb 10 On Ubuntu 16.04 Lts How To Install Nginx Maridb 10 On Ubuntu 16.04 Lts Step 1 — Installing Nginx The first step in installing Nginx and MariaDB 10 on Ubuntu 16.04 is installing Nginx.…