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:
- Install Rapidssl Nginx Ubuntu 18.04 Install RapidSSL with Nginx on Ubuntu 18.04 Step 1: Installing Nginx The first step in setting up RapidSSL with Nginx on Ubuntu 18.04 is to install Nginx itself. This can…
- 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 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…
- 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…
- 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…
- Install Paid Ssl Nginx Ubuntu 18.04 Install Paid SSL Nginx Ubuntu 18.04 What Is Nginx? Nginx is an open-source web server and reverse proxy used in many applications worldwide. It is a lightweight, high-performance server that…
- 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…
- 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…
- How To Check Whether 2 Nginx Is Installed In Ubuntu How To Check Whether 2 Nginx Is Installed In Ubuntu Overview Nginx is an open-source web server that is used to host web sites and applications. It is very popular…
- Completely Uninstall Phpmyadmin On Nginx Ubuntu 18.04 Completely Uninstall PhpMyAdmin On NGINX Ubuntu 18.04 Introduction PhpMyAdmin is a very popular web-based graphical tool for managing MySQL databases in the Ubuntu operating system. It is used by web…
- Perfect Server Ubuntu 18.04 Nginx Perfect Server Ubuntu 18.04 Nginx 1. Introduction Ubuntu is one of the most popular Linux distributions. It is popular due to its user friendliness and availability of excellent free software…
- 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…
- 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…
- 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…
- 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.…
- 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…
- 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,…
- Centos 7 Install Nginx Php Mariadb Centos 7 Install Nginx Php Mariadb Installing Centos 7 for Nginx CentOS 7 is one of the most widely used linux distributions for web servers, and it is an ideal…
- 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 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…
- 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…
- 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…
- How To Install Moodle On Nginx Postgresql Ubuntu How To Install Moodle On Nginx Postgresql Ubuntu Overview of Moodle and Requirements Moodle is an open-source learning management system (LMS) used by educators, businesses, and other organizations to create…
- 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…
- 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…
- 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.…
- 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…
- Install Wordpress On Nginx Ubuntu Install Wordpress On Nginx Ubuntu Introduction WordPress is a popular open-source blogging platform and content management system (CMS). It is made to be used on the internet, and it is…
- 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…