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:
- 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…
- Install Nginx Di Kali Linux Install Nginx Di Kali Linux Introduction Kali Linux is a well-known operating system specially designed for Penetration Testing and Security Auditing tasks. The operating system is loaded with all the…
- Install Php5 Nginx Ubuntu 18.04 Install PHP5 Nginx Ubuntu 18.04 Understanding The Basics of Nginx and PHP Before we learn how to install Nginx and PHP on Ubuntu 18.04, it’s important to understand the basic…
- 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…
- How To Set Up Nginx Loadbalancer Ubuntu How To Set Up Nginx Loadbalancer Ubuntu Introduction to Nginx Loadbalancer Nginx Loadbalancer is a web application that lets you easily set up load balancing for your website. It is…
- Raspbian Install Nginx And Php7 Raspbian Install Nginx And Php7 Introduction to Nginx and PHP Nginx is a powerful open source webserver and reverse proxy solution. Nginx is lightweight and fast, enabling webmasters to serve…
- 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 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 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…
- Letsencrypt Ubuntu 12.04 Nginx Letsencrypt Ubuntu 12.04 Nginx What is a Let's Encrypt Certificate? Let’s Encrypt is a free, automated, and open certificate authority (CA). It will allow you to secure your website with…
- 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…
- 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…
- Tutorial Install Nginx Di Ubuntu Vps Tutorial Install Nginx Di Ubuntu Vps Step 1: Install Nginx The first step to install nginx in your Ubuntu VPS is to install the nginx package. To do this, use…
- 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 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…
- 502 Bad Gateway Nginx In Ubuntu 502 Bad Gateway Nginx In Ubuntu What Is A 502 Bad Gateway Error? A 502 Bad Gateway Error is an HTTP status code that is not able to connect to…
- 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…
- Install Nginx 1.17 Centos 8 Install Nginx 1.17 Centos 8 Introduction to Nginx Nginx is one of the most popular web servers in the world. It is reliable, free, and open source software. It is…
- 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…
- 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…
- 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…
- 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…
- 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…
- 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 Nginx Ubuntu 18.08 Local Install Nginx on Ubuntu 18.04 Local Installing Nginx on Ubuntu 18.04 can be accomplished quickly and in several different ways. Depending on your needs, some methods may be better suited…
- 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 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…
- 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…
- 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…
- 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…