How to Install and Use Nginx on Ubuntu

Posted on the 21 March 2023 by Top10

Nginx (pronounced "engine-x") is a popular web server software known for its high performance and reliability. It is an open source software used by many popular websites including Netflix, GitHub and WordPress. Nginx can serve as a web server, load balancer, reverse proxy, and HTTP cache, among other things.

It is known for its high performance, stability and scalability. It is an excellent choice for hosting websites, applications and services that require fast and reliable web hosting. By following this guide, you can install and configure Nginx on your Ubuntu machine and serve up web pages.

Installing Nginx on Ubuntu

Requirements

There are a few prerequisites that need to be met before installing Nginx on Ubuntu:

  • Ubuntu Server Access: You will need access to an Ubuntu server in the form of a virtual machine or a dedicated server.
  • Superuser access: You will need superuser access to the Ubuntu server. This can be achieved by logging in as root or using the sudo command.

Follow the steps below to install Apache on your Ubuntu system.

Step 1. Reboot Ubuntu

It is always recommended to update your Ubuntu server to the latest version before installing any new software. You can do this by running the following commands in a terminal:

sudo apt-get update
sudo apt-get upgrade

Step 2: Set up other existing web servers

If you have other web servers installed on your Ubuntu server, such as Apache, remove them before installing Nginx. This will avoid conflicts or problems with port bindings.

sudo apt-get remove apache2

Alternatively, if you want to run Nginx alongside Apache, you can use Nginx as a reverse proxy for Apache. This configuration allows Nginx to process incoming requests and forward them to Apache for processing. This setup can offer the benefits of both web servers.

Step 3. Install Nginx

Nginx is available in the Ubuntu repositories. So you don't need to add any other 3rd party repositories. Instead, run the command below on a terminal to install Nginx.

sudo apt install nginx

Step 4: Start Nginx:

Once installed, Nginx should start automatically. However, you can run the command below to start the service if it does not start after installation.

sudo systemctl start nginx

Step 5Check the status of Nginx:

You can check the status of Nginx with the following command:

sudo systemctl status nginx

This command will show the current status of Nginx.

In the image above, you can see that the Nginx service is successfully running on our system.

Tip: If you get an error when starting the Nginx service, chances are good that port 80 is already in use. Nginx uses port 80 for HTTP traffic by default. If another service is already using port 80, Nginx will not start. To check if port 80 is in use, you can run the following command:

sudo lsof -i :80

If another service is using port 80, you can either stop that service or configure Nginx to use a different port.

Step 6Set up the firewall

If you have enabled the UFW firewall on your system, make sure it is properly configured to allow incoming traffic on the ports that Nginx uses. The default port used by Nginx is 80 for HTTP and 443 for HTTPS. You can run the commands below to allow traffic to Nginx.

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'

Step 7Testing Nginx

To check if Nginx is working properly, open a web browser and enter the IP address of your Ubuntu server in the address bar. You can get your system's IP address by running any of the commands below.

ifconfig
Or,
ip a s

If Nginx is installed correctly, you should see the default Nginx welcome page.

Create your own website

The website you see when you enter your system's IP address in the browser is Nginx's default website and is located in the /var/www/html directory. If you want to host a static HTML site, you can remove the files from the /var/www/html directory and add your own files.

However, you must set up virtual hosts if you want to host multiple websites. The virtual host configuration allows you to run multiple websites or web applications on a single server. Each virtual host has its own set of configuration files, allowing you to independently customize the behavior of each website.

Follow the instructions below.

Step 1. Create a directory for your website

Use the mkdir command to create a directory for your website files inside the /var/www folder. For example, we will create a directory called test-website.

sudo mkdir /var/www/test-website

Step 2: Create a new HTML file for your website:

Now you need to add your website files to this directory. For this post, we will create a simple HTML file named "index.html". However, if you are working with WordPress or any other, you can extract the files to this new directory.

sudo nano index.html

Add some basic HTML to the file, like so:

    
       How to Install and Use Nginx on Ubuntu (fosslinux.com)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et dolor quis ex posuere euismod. Sed pharetra vel odio in venenatis. Donec eget eros vel nisi varius rutrum. Aliquam efficitur lacus at purus dictum lobortis. Duis ut diam dignissim, dapibus turpis vel, consectetur orci. Aliquam erat volutpat. Nulla facilisi. Praesent ut sapien sapien.

" decoding="async" fetchpriority="high" referrerPolicy="no-referrer" decoding="async" data-src="https://www.fosslinux.com/wp-content/uploads/2021/02/nginx-logo.png" alt="Nginx Logo" title="How to Install and Use Nginx on Ubuntu | foxcow">

Save (Ctrl+O then Enter) and close the file (Ctrl+X).

Step 3: Create a new Nginx configuration file for the virtual host.

The next step is to create a configuration file for your new website. In this post, we will name it "test-website.conf". Launch a terminal and run the command below.

sudo nano /etc/nginx/sites-available/test-website.conf

Add the following configuration to the file:

server {
    listen 80;
    listen [::]:80;
    root /var/www/test_website;
    index index.html;

    server_name test-website.com www.test-website.com;

   location / {
      try_files $uri $uri/ =404;
  }
}

This configuration tells Nginx to listen on port 80 and serve files from the /var/www/test-website directory. It also sets the default index file to index.html and specifies the domain name for the virtual host.

Save (Ctrl+O then Enter) and close the file (Ctrl+X).

Step 4Enable the virtual host

Next, you need to create a symbolic link to enable the virtual host:

sudo ln -s /etc/nginx/sites-available/test-website.conf /etc/nginx/sites-enabled/

You then need to run a test to make sure your configurations and syntax are ok. Run the command below.

sudo nginx -t

If everything is fine with the configuration, you can proceed to restart Nginx to apply the changes:

sudo systemctl restart nginx

Congratulations, you have created your own website and set up a virtual host using Nginx. You can now access your website by entering your domain name or IP address in a web browser.

Step 5: Test Your Site

You can start testing your website by entering the IP address in your browser.

If you have used a different port number (for example, 81), specify it in the IP address as shown below.

192.168.1.27:81

Nginx vs. Apache

Nginx and Apache are two of the most popular web servers in the world. Although they both serve the same purpose, there are some key differences between them.

Performance

Nginx is known for its high performance and low resource consumption. It is designed to handle many concurrent connections with low memory usage. Apache, on the other hand, can be resource intensive and may require more memory to handle the same amount of traffic.

Flexibility

Apache is a more flexible web server than Nginx. It supports many modules and can be easily customized to meet specific needs. On the other hand, Nginx has a more limited set of modules and is less flexible in customization.

Ease of use

Nginx is generally considered to be easier to set up and use than Apache. Its configuration files are simpler and more intuitive, and can be configured quickly and easily. However, Apache can be more difficult to install and configure, especially for beginners.

Tip: Both Nginx and Apache have their strengths and weaknesses. The choice between them depends on the specific needs of the website or application being served. Nginx is a good choice for high performance web servers requiring low resource consumption, while Apache is more flexible and can be customized to meet specific needs.

Conclusion

Nginx is a great choice for a high performance and reliable web server. It is easy to install and set up in Ubuntu. With its ability to serve as a web server, load balancer, reverse proxy, and HTTP cache, among other things, it provides many benefits.

2654420cookie-checkHow to install and use Nginx on Ubuntuno

similar

Publication author

Comments: 0Publications: 98Registration: 22-11-2021
Инструкции,Программы,Рабочее окружение,nginx,ubuntu
#Install #Nginx #Ubuntu