How to install Nginx on Ubuntu
install
nginx
ubuntu
Nginx is one of the most popular web servers in the world. It is also known to be more resource-friendly than Apache in most cases and can be used as a web server or as a reverse proxy.
In this tutorial will present how to install nginx
. This will work on Ubuntu 18.04, Ubuntu 16.04 or Ubuntu 20.04.
Install Nginx on Ubuntu
Nginx is available in the Ubuntu's default repositories, therefore it is available to be installed from these repositories using the
apt packaging system
.
adm@admfactorytest:~$ sudo apt install nginx
adm@admfactorytest:~$
Add Nginx to the firewall
For Nginx to be access from web it has to be added in the firewall rules.
If you use the firewalld
, you can use the command:
adm@admfactorytest:~$ sudo firewall-cmd --add-service={http,https} --permanent
success
adm@admfactorytest:~$
For more details about firewalld
, please check the Install and use firewalld on Ubuntu tutorial.
If you use the ufw
Ubuntu's firewall, you can use the command to add HTTP, port 80:
adm@admfactorytest:~$ sudo ufw allow 80
or the command to add HTTPS, port 443
adm@admfactorytest:~$ sudo ufw allow 443
Check Nginx server
To check the Nginx service status run the following command:
adm@admfactorytest:~$ 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 Mon 2020-03-30 11:36:14 UTC; 1min 9s ago
Docs: man:nginx(8)
Main PID: 17270 (nginx)
Tasks: 2 (limit: 1108)
CGroup: /system.slice/nginx.service
├─17270 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─17272 nginx: worker process
Mar 30 11:36:14 admfactorytest.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 30 11:36:14 admfactorytest.com systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Mar 30 11:36:14 admfactorytest.com systemd[1]: Started A high performance web server and a reverse proxy server.
adm@admfactorytest:~$
Check Nginx from browser
After we checked that the Nginx service is active and the firewall was set up properly, you can also check from the browser. To do that enter in the browser the IP of your server. You should see the Nginx Welcome page.
Managing the Nginx Process
Managing the Nginx process mostly refers to the options that systemctl
command offers.
Here are some basic management commands.
Enable Nginx service
To enable the service to start up at boot, you can type:
adm@admfactorytest:~$ sudo systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx
adm@admfactorytest:~$
Disable Nginx service
Nginx is configured to start automatically when the server boots. To disable this behaviour you can run the following command:
adm@admfactorytest:~$ sudo systemctl disable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable nginx
adm@admfactorytest:~$
Stop Nginx service
To stop the Nginx service use the following command:
adm@admfactorytest:~$ sudo systemctl stop nginx
adm@admfactorytest:~$
Start Nginx service
To start the Nginx service when it is stopped, use the following command:
adm@admfactorytest:~$ sudo systemctl start nginx
adm@admfactorytest:~$
Restart Nginx service
To restart the Nginx service (stop and start) use the following command:
adm@admfactorytest:~$ sudo systemctl restart nginx
adm@admfactorytest:~$
Reload Nginx service
If you are made configuration changes, Nginx can reload without dropping connections. To do this, use the command:
adm@admfactorytest:~$ sudo systemctl reload nginx
adm@admfactorytest:~$