Replace Apache To Nginx Ubuntu 18


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.

Leave a Reply

Your email address will not be published. Required fields are marked *