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:
- 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…
- 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…
- 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…
- Nginx Always Displaying Default Page In Ubuntu Nginx Always Displaying Default Page In Ubuntu Introduction Ubuntu is one of the most popular Linux distributions and an immensely powerful and versatile operating system. It has a great package…
- 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…
- 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…
- 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 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 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 Pdo_Mysql Ubuntu Nginx Install Pdo_Mysql Ubuntu Nginx What is Pdo_Mysql? PDO_Mysql is a driver for the PHP Data Objects (PDO) extension that provides a database abstraction layer for working with MySQL databases.PDO_Mysql provides…
- 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…
- How To Install Nginx On Debian 10 How To Install Nginx On Debian 10 Overview In this guide, we will show how to install Nginx on a Debian 10 server. Nginx (pronounced “engine-x”) is an open source…
- 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…
- 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…
- Install Phpmyadmin On Nginx Ubuntu 18.04 Install Phpmyadmin On Nginx Ubuntu 18.04 Introduction PhpMyAdmin is one of the most popular and widely used web-based database management tools available. It is used for administering, managing and maintaining…
- How To Install Flask Nginx On Ubuntu 1604 How To Install Flask Nginx On Ubuntu 1604 Introduction Flask is a web application framework based on Python. It is highly useful for web developers due to its flexibility and…
- 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…
- 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…
- 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…
- 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 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…
- Reinstall Nginx Ubuntu 18.04 Reinstall Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source, high-performance web server application designed to serve web traffic with lightning-fast speed and robust stability. Nginx is one…
- 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 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…
- 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…
- 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…
- 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…
- 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 Ufw Allow Nginx Http Digitalocean How To Ufw Allow Nginx Http Digitalocean What is UFW for Nginx on DigitalOcean? UFW (Uncomplicated Firewall) is a firewall application package for use with the Ubuntu Linux operating system.…
- 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.…