Install and configure NGINX in ubuntu
Introduction
Nginx is one among the foremost popular web servers within the world and is liable for hosting a number of the most important and highest-traffic sites on the web . it’s more resource-friendly than Apache in most cases and may be used as an internet server or reverse proxy. In this we will install and configure NGINX in ubuntu
Lets start how to Install and configure NGINX in ubuntu step by step
Prerequisites
Before installing nginx, we must have a regular, non-root user with sudo privileges configured on your server/system. you can read out more on Official documentation of nginx
Installing Nginx in ubuntu
Nginx is available in ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.
Now start with updating the ubuntu first –
$ sudo apt update
$ sudo apt install nginx
Adjusting Firewall
The firewall must adjusting to permit access to the service. Nginx registers itself as a service with ufw upon installation.
List the application configurations that ufw knows –
$ sudo ufw app list
It will show as list below –
Available applications:
CUPS
Nginx Full
Nginx HTTP
Nginx HTTPS
you can see, there are three profiles available –
1. Nginx Full : it opens both port 80 and port 443. port 80 for normal/un-encrypted traffic and 443 for TLS/SSL encrypted traffic
2. Nginx HTTP : it opens on 80 port
3. Nginx HTPS : it open 443 port
It is highly recommended that you enable the most restrictive profile or as you required. you can enable by this command –
$ sudo ufw allow ‘Nginx HTTP’
and you can verify the change by following command –
$ sudo ufw status
and you will see HTTP traffic allowed in the displayed output.
Checking the web server
you can check the status of your web server by following command –
$ systemctl status nginx
and you will see the output as –
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 Fri 2020-05-22 09:04:50 IST; 1 day 8h ago
Docs: man:nginx(8)
Main PID: 888 (nginx)
Tasks: 5 (limit: 14220)
Memory: 9.6M
CGroup: /system.slice/nginx.service
├─888 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─889 nginx: worker process
├─890 nginx: worker process
├─891 nginx: worker process
└─892 nginx: worker process
May 22 09:04:50 software-LIFEBOOK-A555 systemd[1]: Starting A high performance web server and a reverse proxy server…
May 22 09:04:50 software-LIFEBOOK-A555 systemd[1]: Started A high performance web server and a reverse proxy server.
Now you can check as your host – http://localhost OR http://youripordomain
It will show the default nginx page as follows –

Managing the Nginx Process
Now you can manage the nginx server like start/stop. Here are some basic management commands –
$ sudo systemctl stop nginx //stop nginx
$ sudo systemctl start nginx //start nginx
$ sudo systemctl restart nginx //restart nginx
$ sudo systemctl reload nginx //reload nginx
$ sudo systemctl disable nginx //disable nginx
$ sudo systemctl enable nginx //enable nginx
Setting the configuration
We can configure the web server by updating the default config file on you ubuntu –
you can write the command
$ sudo nano /etc/nginx/sites-available/default
and update the file as follows if required
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
#fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Configuring PhpMyadmin
phpmyadmin is a web page tool to manage the mysql database using user-friendly user interface for developer, where you can easily create and update database tables and records. you can install phpmyadmin on ubuntu by writing one command –
$ sudo apt install phpmyadmin
Now to configure phpmyadmin we need to update the default configuration file in nginx. copy and paste the following code after location element –
location /phpmyadmin {
root /usr/share/;
index index.php;
try_files $uri $uri/ =404;
location ~ ^/phpmyadmin/(doc|sql|setup)/ {
deny all;
}
location ~ /phpmyadmin/(.+.php)$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
Now you can access your phpmyadmin by url http://localhost/phpmyadmin or http://yourip/phpmyadmin easility from browser.
That’s it about installing the NGINX on ubuntu. Hope it will help you.