Setting Up a Web Server

Here I will describe how to set up a web server to run on a Linux computer.

Our web server software will be NGINX Open Source.

nginx

Usually, NGINX is available as a package on Linux computers that may be installed as per usual.

Once installed, it should be available as a service that may be started, restarted, and stopped.

Test the installation with sudo nginx -t This will display any warnings or errors with the configuration file which is located at: /etc/nginx/nginx.conf.

To start the server, run the command: sudo systemctl start nginx.

Then, in a web browser the http://localhost page should display a welcome message (Welcome to nginx!). So it is serving the page to the standard port 80 for http urls.

The config file contains server blocks which have the settings for localhost and any domain names.

If you want to develop and test PHP websites on a PC, then you may want to add dummy domain names to your /etc/hosts file so that they resolve to localhost. For example:

127.0.1.1   mydomain.test

Install PHP

PHP is a popular scripting language which runs on a server and the code is embedded in the web pages and files which may be included. The code is interpreted each time the web page is loaded.

A typical use for PHP is to respond to requests for content which is obtained from a database or log in a user.

To serve PHP websites, we need to install PHP-FPM sudo apt install php-fpm This will also install the command line version of PHP.

And run it: sudo systemctl start php-fpm

In the browser, a common cause of an error 500 is when php-fpm is not running.

A typical path to the files of a website might be:

/var/www/mydomain.test/public/

The public folder stores at least index.html or index.php. It also allows for having secret files stored in the parent folder for access by your scripts.

The owner and group are usually www-data and you would add yourself to this group.

File permissions depend on use cases, but a rule of thumb is 755 for folders and 644 for files.

Website Configuration

For each domain name that we want to serve, we will add a server block to the NGINX Config file.

Typical server block

This is added to /etc/nginx/nginx.config inside of the html block.

server {
    server_name .mysite.com;
    root /var/www/mysite.com/public;
    access_log /var/log/nginx/mysite.com.access.log;

    listen 80;
    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/mysite.com/chain.pem;

    index index.php index.html;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # static file 404's aren't logged and expires header is set to maximum age
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}

This code is actioned when the requested domain in the URL matches the server_name and port. The dot prefix allows for matching with or without a subdomain such as www.

The root value points to the location of the index file for the website.

The access_log value assigns a log file to record visitor and bot traffic data.

The listen directives specify which ports that this server block responds to. It should be 80 for http and 443 for ssl (https). You may omit port 80 if you only want to have https pages.

In the case of ssl, you need to install certificates. These can be automatically added by using certbot. cerbot is a package that may be freely installed and run.

The index directive specifies the name of the index file in order of preference. For a static site, you would only specify index.html.

Next are some location directives to match file patterns. Files for robots.txt and favicon.ico may be missing but browsers will request them, but we are not interested in logging the hit, so we tell the server to ignore them.

Also, we don’t want to log hits on individual image files, unless we want to detect external links to our files.

Errors will be logged in the default error log at /var/log/nginx/error.log because we didn’t specify one here.

If we are using PHP we need to specify some standard settings and ensure that the php version matches what we have installed.

Controlling the server

To test the configuration: sudo nginx -t

To start the server: sudo systemctl start nginx

To restart after editing settings: sudo systemctl restart nginx

File locations

Server configuration: /etc/nginx

Command to find PHP location: whereis php

PHP configuration file: php.ini

Server logs: /var/log/nginx

Web sites/pages: /var/www

Test pages

Any text may be added to index.html for a quick test that it displays in a browser.

For PHP, the standard check is to display the config information. Add to index.php:

<?php
php_info();

If there is a blank page (error 500 or 404), possible problems are:

PHP errors will appear in the error log at: /var/log/nginx/error.log

Conclusion

I hope that this simplified guide to setting up a web server was helpful.