Setup NGINX in Ubuntu for your first Web Server

Hammam Firdaus
4 min readMay 31, 2023

--

Photo by Taylor Vick on Unsplash

When I work as a Backend Engineer I don’t know how the server runs just and I know just in Basic or Common Cases. So, because I got a free credit of $200 in 60 days I will repeat my lesson to know deeper how the server runs as well.

by the way, if you guys want to get free credit like me you can register in Digital Ocean with this link: https://m.do.co/c/a96f4e10566e

- Thank you 😁

Let’s create our first Web Server!

Before creating the web server, we must know about what is a web server? and what is Nginx? in general.

A web server is a software application that runs on a computer and is responsible for delivering web content over the internet. It responds to requests from web clients (usually web browsers) and delivers the requested files or executes specific tasks.

Nginx (pronounced “engine-x”) is a popular web server software known for its high performance, scalability, and efficient resource utilization.

Setup Nginx to the server

Before beginning the guide, in this article I use Ubuntu 22.04 (LTS) x64.

1. Install Nginx

Nginx is one of the default packages in Ubuntu so we can install it from the apt but before installing the Nginx better update the apt first.

~# sudo apt update
~# sudo apt install nginx

2. Allow the Firewall

Before make allow the firewall and test the Nginx, you can see list of available applications in your ufw(Uncomplicated Firewall) with the command below.

~# sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

Explanation :

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
  • OpenSSH: OpenSSH is a suite of secure networking utilities that provides encrypted communication between a client and a server. It includes programs such as ssh (Secure Shell) for secure remote login, scp (Secure Copy) for secure file transfer, and sftp (Secure FTP) for secure file access.

*explanation above I got from Digital Ocean Blog.

In this part, I want just to allow traffic on port 80 Nginx HTTP

~# sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)

You can verify ufw allowed application with command below

~# sudo ufw status

But, if you get a result like this

~# sudo ufw status
Status: inactive

ufw needs to enable first, and you can run the command below (don't forget to allow with type y when procced ask you)

~# sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
~# sudo ufw status
Status: active
To                         Action      From
-- ------ ----
Nginx HTTP ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere (v6)

after the Nginx has been allowed, you can check the nginx server status.

~# systemctl status nginx
● 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 2023-05-30 13:30:27 UTC; 3min 27s ago
Docs: man:nginx(8)
Main PID: 23169 (nginx)
Tasks: 2 (limit: 2323)
Memory: 2.7M
CPU: 23ms
CGroup: /system.slice/nginx.service
├─23169 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─23172 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Notice: journal has been rotated since unit was started, output may be incomplete.

if you see the Active status is active and running you can check the default Nginx website. But, if you get the status Inactive and dead like this

~# systemctl status nginx
○ nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2023-05-30 15:17:24 UTC; 2s ago
Docs: man:nginx(8)
Process: 24365 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 23169 (code=exited, status=0/SUCCESS)
CPU: 36ms

Notice: journal has been rotated since unit was started, output may be incomplete.

you can start the Nginx first, with the command below.

~# sudo systemctl start nginx

So, visit your IP address http://your_server_ip if you don't configure it with the domain, and you got default website like this.

3. Basic Command Nginx

  • Start web server
~# sudo systemctl start nginx
  • Stop web server
~# sudo systemctl stop nginx
  • Restart web server
~# sudo systemctl restart nginx
  • Reload web server (if you change your configuration)
~# sudo systemctl reload nginx
  • Disable web server
~# sudo systemctl disable nginx
  • Enable web server
~# sudo systemctl enable nginx

--

--

No responses yet