Web Server Setup
Nginx¶
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¶
- Securing the Server:
- 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.
-
For local networks, self-signed certificates or a local Certificate Authority can be used.
-
Testing the Setup:
- 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
-
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. -
Managing Nginx and PHP Services:
-
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
-
Regular Updates and Maintenance:
- 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.