root
)Update local apt package index.
sudo apt-get update
Install nginx
sudo apt-get install nginx
Nginx registers itself as a service with ufw
, our firewall, upon installation.
List application configurations that ufw
knows how to work with.
sudo ufw app list
Three (3) profiles should be available for nginx.
Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
It is recommended to enable the most restrictive profile that will still allow the traffic you’ve configured. Since SSL is not yet configured, only need to allow traffic on port 80 at this time. You can enable this by typing:
sudo ufw allow 'Nginx HTTP'
Verify the change
sudo ufw status
You should see HTTP traffic allowed in the displayed output:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Check with the systemd init system to make sure the service is running:
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 Mon 2016-04-18 16:14:00 EDT; 4min 2s ago
Main PID: 12857 (nginx)
CGroup: /system.slice/nginx.service
├─12857 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─12858 nginx: worker process
To find IP address from the command line: You will get back a few lines. You can try each in your web browser to see if they work.
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
An alternative, should give you your public IP address as seen from another website:
sudo apt-get install curl
curl -4 icanhazip.com
When you have your server’s IP address or domain, enter it into your browser’s address bar:
http://server_domain_or_IP
To stop web server:
sudo systemctl stop nginx
To start web server when it is stopped:
sudo systemctl start nginx
To stop and then start the service again:
sudo systemctl restart nginx
If simply making config changes, Nginx can often reload without dropping connections. Use:
sudo systemctl reload nginx
To disable Nginx auto-start up at boot:
sudo systemctl disable nginx
To re-enable the service to start up at boot:
sudo systemctl enable nginx
/var/www/html
: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /var/www/html
directory. This can be changed by altering Nginx configuration files./etc/nginx
: The nginx configuration directory. All of the Nginx configuration files reside here./etc/nginx/nginx.conf
: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration./etc/nginx/sites-available
: The directory where per-site “server blocks” can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled
directory (see below). Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory./etc/nginx/sites-enabled/
: The directory where enabled per-site “server blocks” are stored. Typically, these are created by linking to configuration files found in the sites-available
directory./etc/nginx/snippets
: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets./var/log/nginx/access.log
: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise./var/log/nginx/error.log
: Any Nginx errors will be recorded in this log.These step-by-step instructions are taken from various tutorials on digitalocean.com, phusionpassenger.com, and other sites. Some sources have been linked. Most step descriptions have been shortened and simplified. Some step sequences are from one source while other may come from various sources.
These instructions and commands are placed on GitHub so I can conveniently find them. I am a novice with Ubuntu, Nginx, Passenger, command line, etc., so I probably can’t answer any questions. However, I’ll be glad to incorporate any corrections that are needed.
Use instructions and commands at your own risk.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.