Setting up and Configuring Web Server (Nginx/Apache)
1. What is a Web Server?
Today we're gonna talk about Nginx and Apache, two of the most popular web servers out there. By the end of this lecture, your server will proudly greet unsuspecting users on the internet.
Before diving into the commands, let’s figure out what we’re trying to achieve here. A web server is a program that accepts HTTP requests (like when a user types a website address into their browser), processes them, and replies with the appropriate content (an HTML page, a file, or even a "404 Not Found" error). It acts as a bridge between users and your content.
Nginx vs Apache: Battle of the Web Servers
If Nginx and Apache met in a café, then:
- Nginx would order a lavender latte and handle everything asynchronously: "You give me a request, and I’ll get back to you with the file in a minute, no interruptions!”
- Apache, on the other hand, would be more traditional, creating a thread for each task and responding politely, though slower under heavy traffic.
Quick differences:
Feature | Nginx | Apache |
---|---|---|
Architecture | Asynchronous | Thread-oriented |
Performance | High under heavy loads | Good for dynamic apps (PHP/Perl) |
Simplicity | Simple configuration with quirks | More universal but more complex |
When to Choose What?
- If your site is just a bunch of static files (like a blog or landing page), then Nginx is your buddy.
- If you’re dealing with dynamic web apps (like a PHP site), Apache will give you more flexibility.
2. Installing a Web Server
Now that you get why it's needed, it's time to start the installation. We'll look at installing both Nginx and Apache. Pick whatever suits you better, or go ahead and install both to feel like a pro.
Installing Nginx
Update the package list:
sudo apt-get update
Install Nginx:
sudo apt-get install nginx
Check the server status:
sudo systemctl status nginx
If everything's fine, you'll see something like:
active (running)
.Check the default page: Open your browser and type
http://localhost
. You should see the Nginx welcome page, confirming the installation was successful.
Installing Apache
Update the package list:
sudo apt-get update
Install Apache:
sudo apt-get install apache2
Check the server status:
sudo systemctl status apache2
Just like with Nginx, the status should say “active (running)”.
Check the default page: Open your browser at
http://localhost
. Now you'll see Apache's default page.
3. Basic Web Server Configuration
Where are the settings hidden?
For Nginx, the main config file is located at:
/etc/nginx/nginx.conf
For Apache:
/etc/apache2/apache2.conf
Try opening the configuration files with your favorite editor (for example, nano
):
sudo nano /etc/nginx/nginx.conf
This is where your first adventure begins: you might not understand anything right now because config files look like spells from a role-playing game. But don't freak out – we've got this.
Starting, Stopping, and Restarting Servers
If you know how to manage services on Linux, then "half the battle is won." Here are the main commands for managing our web generators:
For Nginx:
sudo systemctl start nginx # Start
sudo systemctl stop nginx # Stop
sudo systemctl restart nginx # Restart
sudo systemctl reload nginx # Reload configuration
For Apache:
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
Checking Configuration
Before restarting the server, always check if you've made any mistakes:
For Nginx:
sudo nginx -t
For Apache:
sudo apachectl configtest
Any errors in the configuration will be shown – you can't accidentally break the server.
4. Demo: Launching our first website
Now we're about to do something web developers really love – create a site with an incredibly creative name: "Hello Server".
For Nginx
Create a directory for your site:
sudo mkdir -p /var/www/hello sudo nano /var/www/hello/index.html
Write the following in the file (yeah, all genius things are simple):
<h1>Hello, Nginx Server!</h1>
Set up the configuration:
sudo nano /etc/nginx/sites-available/hello
Here's a simple config example:
server { listen 80; server_name hello.local; root /var/www/hello; index index.html; location / { try_files $uri $uri/ =404; } }
Activate the configuration:
sudo ln -s /etc/nginx/sites-available/hello /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
Add an entry to
/etc/hosts
to set up the domain namehello.local
on your local machine:127.0.0.1 hello.local
Check the site in your browser: http://hello.local.
The hosts file contains a mapping of IP addresses to domain names. It's your local DNS server. Every operating system has it. On Windows, it's located at C:\Windows\System32\drivers\etc\hosts
For Apache
Create a directory:
sudo mkdir -p /var/www/hello sudo nano /var/www/hello/index.html
Fill the file with:
<h1>Hello, Apache Server!</h1>
Create a virtual host configuration:
sudo nano /etc/apache2/sites-available/hello.conf
Here's an example config:
<VirtualHost *:80> ServerName hello.local DocumentRoot /var/www/hello <Directory /var/www/hello> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
Activate the configuration:
sudo a2ensite hello.conf sudo systemctl reload apache2
Add an entry to
/etc/hosts
:127.0.0.1 hello.local
Check it out: http://hello.local.
After today's lecture, you're ready to independently set up and configure a primary web server. A bit more effort – and you'll be deploying full-fledged websites and apps that no longer look like a kid's drawing. HTTPS is lurking just around the corner, but we'll get to that later.
GO TO FULL VERSION