Lesson 7.1: Nginx Installation

In this lesson, you'll install Nginx web server on your Ubuntu system. This is the foundation for your web development project and demonstrates your package management skills.

Why Nginx? Nginx is lightweight, fast, and widely used in production. It's perfect for learning web server administration and serves as a solid foundation for web development.

What is Nginx?

Nginx (pronounced "engine-x") is a high-performance web server that has gained popularity for its efficiency and scalability:

Installing Nginx

We'll use APT to install Nginx, applying what you learned in Module 6:

Update Package Lists

Always start by updating your package lists:

$ sudo apt update Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Fetched 25.6 MB in 2s (12.3 MB/s) Reading package lists... Done

Install Nginx

Install the Nginx web server package:

$ sudo apt install nginx Reading package lists... Done Building dependency tree... Done The following additional packages will be installed: nginx-common nginx-core Suggested packages: nginx-doc Do you want to continue? [Y/n] Y

Verify Installation

Check that Nginx was installed correctly:

$ nginx -v nginx version: nginx/1.18.0 (Ubuntu) $ which nginx /usr/sbin/nginx

Understanding Nginx Structure

After installation, Nginx creates several important directories and files:

Key Directories

Configuration Files

Starting and Managing Nginx

Use systemctl to manage the Nginx service:

Start Nginx

$ sudo systemctl start nginx # No output means success

Check Service Status

$ sudo systemctl status nginx ● 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 2024-12-10 15:45:32 UTC; 5min ago Docs: man:nginx(8) Main PID: 1234 (nginx) Tasks: 2 (limit: 4667) Memory: 2.1M CGroup: /system.slice/nginx.service

Enable Auto-Start

Make Nginx start automatically when system boots:

$ sudo systemctl enable nginx Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.

Stop and Restart

$ sudo systemctl stop nginx $ sudo systemctl restart nginx

Testing Nginx Installation

Verify that Nginx is working correctly:

Check if Nginx is Running

$ sudo systemctl is-active nginx active

Test Web Server

Use curl to test the web server:

$ curl localhost Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Test in Web Browser

Open your web browser and navigate to:

Finding Your IP: Use ip addr show or hostname -I to find your IP address.

Firewall Configuration

If you have a firewall enabled, you need to allow web traffic:

Check Firewall Status

$ sudo ufw status Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere

Allow Web Traffic

$ sudo ufw allow 'Nginx Full' Rule added Rule added (v6) # Or allow specific ports $ sudo ufw allow 80/tcp $ sudo ufw allow 443/tcp

Verify Firewall Rules

$ sudo ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere 80/tcp ALLOW IN Anywhere 443/tcp ALLOW IN Anywhere Nginx Full ALLOW IN Anywhere

Troubleshooting Installation Issues

Common problems and solutions:

Port Already in Use

If another web server is running on port 80:

$ sudo ss -tlnp | grep :80 LISTEN 0 128 0.0.0.0:80 1234/nginx # Stop the other service $ sudo systemctl stop apache2 $ sudo systemctl start nginx

Permission Denied

If you get permission errors:

$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/var/log/nginx/error.log" failed (13: Permission denied) # Check and fix permissions $ sudo chown -R www-data:www-data /var/log/nginx $ sudo chmod -R 755 /var/log/nginx

Service Won't Start

$ sudo journalctl -u nginx -- Logs begin at Mon 2024-12-10 15:45:32 UTC, end at Mon 2024-12-10 15:50:15 UTC. -- Dec 10 15:45:32 ubuntu systemd[1]: Started A high performance web server... Dec 10 15:45:33 ubuntu nginx[1234]: nginx/1.18.0 (Ubuntu) Dec 10 15:45:33 ubuntu nginx[1234]: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Practice Exercises

Exercise 1: Basic Installation

  1. Update package lists
  2. Install Nginx
  3. Start the Nginx service
  4. Enable auto-start
  5. Verify installation with curl
Click for solution
$ sudo apt update $ sudo apt install nginx $ sudo systemctl start nginx $ sudo systemctl enable nginx $ curl localhost

Exercise 2: Service Management

  1. Check Nginx status
  2. Stop the Nginx service
  3. Restart the Nginx service
  4. View Nginx logs
  5. Test web server after each operation
Click for solution
$ sudo systemctl status nginx $ sudo systemctl stop nginx $ sudo systemctl restart nginx $ sudo journalctl -u nginx -f $ curl localhost

What's Next?

Great! You have Nginx installed and running. Next, we'll configure it to serve your own website files.

Continue to Website Configuration

Key Takeaways

  • Use APT to install Nginx web server
  • systemctl manages Nginx service (start, stop, restart)
  • Nginx configuration is in /etc/nginx/
  • Default website files go in /var/www/html/
  • Test with curl or web browser
  • Configure firewall to allow web traffic