Skip to main content

Installing Nginx

In this lesson we're going to install Nginx: an open source, high-performance HTTP server. It is one of the most widely used web servers today, and my recommended way to run WordPress and other PHP applications.

Nginx, pronounced 'engine X', is available in the official package repositories for most Linux flavors, including Ubuntu and Debian.

You can often get newer versions from Nginx's packages, but unless there's a specific feature you really need in a newer version, I recommend always sticking to what's available in your OS distribution.

Installing

To install Nginx on an Ubuntu or Debian system from OS packages, run:

sudo apt update
sudo apt install nginx

This will install Nginx and all dependencies. It will also create the necessary systemd service to start, stop, and reload Nginx, and make sure it runs when the system is rebooted.

On some systems Nginx will start automatically after installation, but you may need to start it manually if not. You can start Nginx and check its current status using:

sudo systemctl start nginx
sudo systemctl status nginx

Also make sure that the service is listed as enabled, which means it will be started automatically on boot.

Nginx status

Testing

The default Nginx installation comes with a sample 'Welcome to nginx!' page. You should be able to access this page locally using cURL:

curl http://localhost/

However, if you try to access the page in a browser window, it will probably fail. This is because our firewall configuration only allows external access to port 443, which is the HTTPS port. Nginx is not configured to serve HTTPS traffic by default.

You can update your firewall configuration to allow port 80 to test HTTP traffic if you like, but keep in mind that our end goal in this course is to have HTTPS only traffic. Any HTTP requests will be handled and redirected by Cloudflare, which we will set up later in this module.

Logging

By default, Nginx will write access and error logs to the /var/log/nginx/ directory. Make sure there's a /etc/logrotate.d/nginx configuration to prevent those logs from outgrowing our disk.

Here's a sample configuration for daily rotation with 14 days of retention:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
    endscript
}

If you installed Nginx from an official package, the configuration should already be in place, along with any necessary rotation scripts.

Enroll
Enjoying the course content? Enroll today to keep track of your progress, access premium lessons and more.