• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install Nginx on Ubuntu 18.04 Server

Written by Admin, Updated On May 7, 2019
nginx, ubuntu
How to Install Nginx On Ubuntu 18.04

Nginx is an open-source and high performance and HTTP and reverse proxy server. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers. It is a more flexible and lightweight program than Apache HTTP Server that’s why it powers some of the largest sites on the Internet. Nginx can handle the bigger amount of connection than apache and consuming smaller memory. This tutorial outlines how to install Nginx on Ubuntu 18.04 server.

Prerequisites#

  • You should logged in Ubuntu 18.04 server with a non-root sudo enabled user account.
  • Ensure that Apache or any other services are not running on port 80 or 443.

Install Nginx on Ubuntu#

Nginx packages are available in the default Ubuntu repositories. The installation is pretty straight forward. First, you need to update packages index to the latest version by type:

sudo apt update

Now install Nginx by typing:

sudo apt install nginx

On the completion of installation, Nginx service will start automatically. You can verify it by running the following command:

sudo systemctl status nginx
Output
● 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 Wed 2019-04-17 16:55:01 IST; 2h 48min ago
Docs: man:nginx(8)
Process: 1003 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 911 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 1004 (nginx)
Tasks: 5 (limit: 4489)
CGroup: /system.slice/nginx.service
├─1004 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─1005 nginx: worker process

Adjust Firewall#

You need to open HTTP(80) and HTTPS(443) ports if firewall is enabled on your server. Execute the following commands to open both HTTP(80) and HTTPS(443) ports:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

If your using iptables to filter connections to your system, you’ll need to open HTTP (80) and HTTPS (443) ports.

Open the necessary ports by issuing the following command:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

You can verify your Nginx installation by visiting below URL in your browser:

http://YOUR_SERVER_IP_ADDRESS

It will show you default Nginx welcome page as shown below:

nginx web server default page

Managing Nginx Service#

Finally, you installed Nginx HTTP server on your machine. It’s easy to manage Nginx service same as other systemd service. Below are few basic commands to manage Nginx service:

You can stop nginx service by typing:

sudo systemctl stop nginx

Start it again by run:

sudo systemctl start nginx

Restart (stop and start) Nginx service by:

sudo systemctl restart nginx

If you made in configuration of nginx and want to reload service then you can do it by below command:

sudo systemctl reload nginx

In Ubuntu system, by default Nginx service starts on boot. You can disable the Nginx service to start at boot by execute following command:

sudo systemctl disable nginx

Again to do re-enable type:

sudo systemctl enable nginx

Nginx Configuration Files Structure#

  • All configuration files are located in the /etc/nginx/ directory.
  • Nginx main configuration file is at /etc/nginx/nginx.conf.
  • It’s best practice to create a separate configuration file of each domain for better maintainability.
  • New server blocks (configuration file) of each domain should be stored in /etc/nginx/sites-availabledirectory. You need to make symlink of these configuration files at /etc/nginx/sites-enabled to take in used by Nginx.
  • Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a /etc/nginx/sites-enabled directory to the sites-enabled directory.
  • The default server web root directory is /var/www/html
  • It’s best practice to to follow standard naming convention. Nginx server block files name should as domain name and must end with .conf extension. For example, your domain name is example.com then server block file name should example.com.conf
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It’s also recommended to have a different access and error log files for each server block.

Conclusion#

In this tutorial, you learned how to install Nginx on Ubuntu 18.04 server. If you have any question or suggestion don’t hesitate to comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install phpMyAdmin with Nginx on CentOS 7
Next Article   How to Set Up Nginx Server Blocks on Ubuntu 18.04

Related Posts

  • How to Install and Use PHP Composer on Ubuntu 22.04

    How to Install Composer on Ubuntu 22.04

    January 31, 2023
  • How to Install Nginx on Ubuntu 22.04

    How to Install Nginx on Ubuntu 22.04

    January 28, 2023
  • How to Install Puppet Agent on Ubuntu 22.04

    How to Install Puppet Agent on Ubuntu 22.04

    January 22, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook