Skip to content

Web Server Setup

Nginx

web-chacho

The following guide provides a walkthrough for setting up a web server on a local network using Nginx. This guide assumes you are using Ubuntu or another debian-based flavor of Linux as your operating system.

Step 1: Install Nginx

Before we start, make sure to update your package manager:

sudo apt update

Next, install Nginx:

sudo apt install nginx

Once the installation is complete, check the status of Nginx:

sudo systemctl status nginx

Ensure Nginx is running. You should see output indicating that the service is active (running).

Verify that port 80 (the default HTTP port) is open:

netstat -ant

If your server is behind a firewall, ensure it has rules to allow for internal SSH connections and incoming traffic on ports 80 and 443 from any IP address.

Step 2: Configure UFW (Uncomplicated Firewall)

Enable UFW:

sudo ufw enable

Allow SSH connections to prevent being locked out:

sudo ufw allow OpenSSH

Allow HTTP and HTTPS traffic using the Nginx Full profile:

sudo ufw allow 'Nginx Full'

Check the status of UFW to confirm the changes:

sudo ufw status

This should show that the necessary ports are open.

Step 3: Install PHP, PHP-FPM, and PHP-MySQL

PHP is a scripting language used for web development, while PHP-FPM (FastCGI Process Manager) enables you to run PHP scripts as separate processes, making your server more efficient and scalable. PHP-MySQL provides the necessary extension to connect to MySQL databases from within PHP.

Install PHP, PHP-FPM, and PHP-MySQL:

sudo apt install php-fpm php-mysql

Check the status of PHP-FPM:

sudo systemctl status php7.4-fpm

Replace 7.4 with the actual version of PHP installed.

Step 4: Configure Nginx to Use PHP Processing

Edit the default Nginx server block configuration:

sudo nano /etc/nginx/sites-available/default

Add the following location block inside the server block to handle PHP requests:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Adjust the fastcgi_pass directive to match your PHP version.

Test for syntax errors:

sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

sudo systemctl reload nginx

Additional Considerations

  1. Securing the Server:
  2. While this guide does not cover SSL/TLS encryption (as it is intended for a local network setup), consider implementing SSL/TLS for any production environment to secure incoming traffic.
  3. For local networks, self-signed certificates or a local Certificate Authority can be used.

  4. Testing the Setup:

  5. You can test your setup by creating a simple PHP file in the web root directory (/var/www/html):
    echo "<?php phpinfo(); ?>" > /var/www/html/info.php
    
  6. Navigate to http://your-ubuntu-vm-ip/info.php in your browser. You should see the PHP information page if everything is set up correctly.

  7. Managing Nginx and PHP Services:

  8. You can manage Nginx and PHP services using systemctl commands:

    sudo systemctl start nginx
    sudo systemctl stop nginx
    sudo systemctl restart nginx
    sudo systemctl start php7.4-fpm
    sudo systemctl stop php7.4-fpm
    sudo systemctl restart php7.4-fpm
    

  9. Regular Updates and Maintenance:

  10. Regularly update your server packages and Nginx to ensure you have the latest security patches and features:
    sudo apt update && sudo apt upgrade
    

By following these steps and considerations, you should have a fully functional Nginx web server on your local network.