Table of Contents
- Prerequisites
- Step 1: Choose a Linux Distribution
- Step 2: Select a Hardware or Virtual Environment
- Step 3: Install the Linux Operating System
- Step 4: Update the System
- Step 5: Choose a Web Server Stack
- Step 6: Install the LAMP Stack (Apache, MySQL, PHP)
- Step 7: Configure the Firewall
- Step 8: Test the Web Server
- Step 9: Set Up a Domain Name (Optional)
- Step 10: Install an SSL Certificate (HTTPS)
- Step 11: Server Maintenance & Best Practices
- Troubleshooting Common Issues
- References
Prerequisites
Before starting, ensure you have:
- Basic Linux Knowledge: Familiarity with terminal commands (e.g.,
cd,sudo,apt/dnf). - Hardware/Virtual Access: A physical server, VPS (e.g., DigitalOcean, AWS EC2), or local VM (VirtualBox/VMware).
- Internet Connection: To download packages and updates.
- Domain Name (Optional): For public access (e.g.,
example.com). - Root/Sudo Access: To install software and modify system settings.
Step 1: Choose a Linux Distribution
The first decision is selecting a Linux distribution (distro). For web servers, stability, long-term support (LTS), and community documentation are critical. Here are the top choices:
| Distro | Use Case | Pros | Cons |
|---|---|---|---|
| Ubuntu Server LTS | Beginners, small to medium sites | User-friendly, huge community, 5-year LTS | Slightly more resource-heavy than Debian |
| Debian | Minimal, security-focused servers | Lightweight, stable, strict package policy | Slower updates (prioritizes stability) |
| CentOS Stream | Enterprise, Red Hat-compatible environments | RHEL-based, enterprise tools, strong security | Smaller community than Ubuntu/Debian |
Recommendation for beginners: Ubuntu Server LTS (e.g., 22.04 LTS). It’s beginner-friendly, well-documented, and widely supported by hosting providers.
Step 2: Select a Hardware or Virtual Environment
You’ll need a server to host your Linux OS. Choose from:
Option 1: Physical Server
Use a dedicated computer (old laptop/desktop) with:
- CPU: 2+ cores (4+ for high traffic).
- RAM: 2GB+ (4GB+ for dynamic sites).
- Storage: 20GB+ SSD (faster than HDD for web apps).
- Network: Ethernet port (for stable connectivity).
Option 2: Virtual Private Server (VPS)
Ideal for most users (no hardware setup!). Popular VPS providers:
- DigitalOcean: $5/month (1GB RAM, 25GB SSD, 1TB transfer).
- AWS EC2: Free tier available (t2.micro, 1GB RAM).
- Linode: $5/month (1GB RAM, 25GB SSD).
Option 3: Local Virtual Machine (VM)
For testing (not public access): Use VirtualBox, VMware, or Proxmox. Allocate:
- 2GB RAM, 20GB storage, and 2 CPU cores.
We’ll use Ubuntu Server 22.04 LTS on a VPS for this guide (most accessible for beginners).
Step 3: Install the Linux Operating System
If using a VPS, most providers (DigitalOcean, Linode) let you skip manual installation—simply select “Ubuntu Server 22.04 LTS” during setup. For physical servers or local VMs, follow these steps:
1. Download the Ubuntu Server ISO
Go to the Ubuntu Server downloads page and grab the 22.04 LTS ISO.
2. Create a Bootable USB Drive
Use tools like Rufus (Windows) or balenaEtcher (macOS/Linux) to flash the ISO to a USB drive (8GB+).
3. Boot from the USB Drive
- Insert the USB into your server/VM.
- Restart and enter the BIOS/UEFI (press
F2,F12, orDelduring boot). - Set the USB as the primary boot device.
4. Run the Installer
Follow the on-screen prompts:
- Language: Select your language.
- Keyboard Layout: Choose your keyboard (e.g., “English (US)”).
- Network Configuration: Ubuntu auto-detects Ethernet/Wi-Fi. For static IP (recommended for servers), select “Edit IPv4” and set:
- Subnet:
192.168.1.0/24(or your network’s subnet). - Address:
192.168.1.100(static IP). - Gateway:
192.168.1.1(router IP). - Nameservers:
8.8.8.8, 8.8.4.4(Google DNS).
- Subnet:
- Disk Setup: Choose “Use an entire disk” (simplest for beginners) or “Custom storage layout” (for LVM/RAID).
- User Setup: Create a non-root user (e.g.,
webadmin), set a password, and enable SSH access (check “Install OpenSSH server”). - Snap Packages: Skip (we’ll use
aptfor better control).
5. Reboot
After installation, remove the USB and reboot. You’ll now have a running Ubuntu Server!
Step 4: Update the System
First, log in via SSH (or directly on the server) using your user credentials. Always update your system to patch security vulnerabilities and get the latest packages:
# For Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# For CentOS Stream
sudo dnf update -y
apt update: Fetches the latest package lists.apt upgrade -y: Installs updates (-y auto-confirms prompts).
Step 5: Choose a Web Server Stack
A web server stack is a combination of software to serve websites. The two most popular are:
LAMP Stack: Linux + Apache + MySQL + PHP
- Apache: The most widely used web server (easy to configure, great for dynamic content).
- MySQL/MariaDB: Relational database for storing data (e.g., user accounts, blog posts).
- PHP: Server-side scripting language for dynamic sites (e.g., WordPress, Drupal).
LEMP Stack: Linux + Nginx + MySQL + PHP
- Nginx: Faster than Apache for static content (e.g., HTML/CSS/JS) and better at handling high traffic.
We’ll start with LAMP (simpler for beginners). Later, we’ll briefly cover LEMP as an alternative.
Step 6: Install the LAMP Stack (Apache, MySQL, PHP)
6.1 Install Apache Web Server
Apache is the “A” in LAMP. Install it with:
sudo apt install apache2 -y
Verify Apache is running:
sudo systemctl status apache2
You should see active (running). If not, start it:
sudo systemctl start apache2
sudo systemctl enable apache2 # Start on boot
6.2 Install MySQL/MariaDB Database
MySQL (or its fork MariaDB) stores your website’s data. Install MariaDB (default on Ubuntu):
sudo apt install mariadb-server -y
Secure the installation (critical for production!):
sudo mysql_secure_installation
Answer the prompts:
- Enter current password for root: Press
Enter(no password by default). - Set root password?:
Y(create a strong password). - Remove anonymous users?:
Y. - Disallow root login remotely?:
Y. - Remove test database?:
Y. - Reload privilege tables now?:
Y.
6.3 Install PHP
PHP processes dynamic content (e.g., WordPress, contact forms). Install PHP and required modules:
sudo apt install php libapache2-mod-php php-mysql -y
libapache2-mod-php: Enables Apache to run PHP scripts.php-mysql: Allows PHP to communicate with MySQL.
Verify PHP is installed:
php -v # Should output PHP version (e.g., 8.1.x)
Step 7: Configure the Firewall
A firewall blocks unauthorized access. Ubuntu uses ufw (Uncomplicated Firewall) by default.
Allow Essential Ports
Web servers need to allow:
- SSH (22): For remote management.
- HTTP (80): For unencrypted web traffic.
- HTTPS (443): For encrypted web traffic (we’ll set this up later).
# Allow SSH
sudo ufw allow 22/tcp
# Allow HTTP
sudo ufw allow 80/tcp
# Enable the firewall
sudo ufw enable
# Check status
sudo ufw status
Output should show:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
Step 8: Test the Web Server
Let’s verify Apache and PHP work.
Test Apache
Open a browser and visit your server’s IP address (find it with hostname -I). You’ll see the Apache default page:

Test PHP
Create a PHP info file in Apache’s default web root (/var/www/html):
sudo nano /var/www/html/info.php
Add this code:
<?php phpinfo(); ?>
Save with Ctrl+O, exit with Ctrl+X.
Visit http://[your-server-ip]/info.php in a browser. You’ll see PHP’s configuration details (e.g., version, modules).
Important: Delete info.php after testing (it exposes sensitive info!):
sudo rm /var/www/html/info.php
Step 9: Set Up a Domain Name (Optional)
To use a domain (e.g., example.com) instead of an IP, follow these steps:
1. Purchase a Domain
Buy a domain from registrars like Namecheap, GoDaddy, or Cloudflare.
2. Point the Domain to Your Server IP
In your registrar’s DNS settings, add an A record:
- Host:
@(or leave blank for root domain). - Value: Your server’s public IP (e.g.,
203.0.113.5). - TTL: 300 (5 minutes).
DNS changes take 10–30 minutes to propagate.
3. Configure Apache Virtual Hosts
Apache uses virtual hosts to host multiple sites on one server. Create a virtual host for your domain:
sudo nano /etc/apache2/sites-available/example.com.conf
Add:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined
</VirtualHost>
Create the website directory and test page:
sudo mkdir -p /var/www/example.com/public_html
sudo echo "Hello, example.com!" > /var/www/example.com/public_html/index.html
sudo chown -R $USER:$USER /var/www/example.com # Set permissions
Enable the site and reload Apache:
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf # Disable default site
sudo systemctl reload apache2
Visit http://example.com—you’ll see “Hello, example.com!”.
Step 10: Install an SSL Certificate (HTTPS)
HTTPS encrypts traffic between the server and users (required for SEO and security). Use Let’s Encrypt (free SSL certificates) with Certbot.
Install Certbot
sudo apt install certbot python3-certbot-apache -y
Obtain & Install SSL Certificate
Run Certbot and follow prompts:
sudo certbot --apache -d example.com -d www.example.com
- Enter your email (for renewal notices).
- Agree to terms.
- Choose whether to redirect HTTP to HTTPS (select “2” for automatic redirect).
Certbot will:
- Fetch an SSL certificate.
- Configure Apache to use HTTPS.
- Set up auto-renewal (certificates expire after 90 days).
Verify HTTPS: Visit https://example.com—you’ll see a padlock in the browser.
Step 11: Server Maintenance & Best Practices
Regular Updates
Keep your server secure with monthly updates:
sudo apt update && sudo apt upgrade -y
Back Up Data
Back up databases and website files:
# Backup MySQL database
mysqldump -u root -p --all-databases > backup_$(date +%F).sql
# Backup website files
sudo tar -czf /var/backups/example.com_$(date +%F).tar.gz /var/www/example.com
Monitor Performance
Use tools like htop (CPU/RAM usage) or nmon (system monitor):
sudo apt install htop nmon -y
htop # Press F10 to exit
Secure SSH
- Disable password login (use SSH keys instead):
Edit/etc/ssh/sshd_config:
Setsudo nano /etc/ssh/sshd_configPasswordAuthentication noandChallengeResponseAuthentication no.
Restart SSH:sudo systemctl restart sshd.
Troubleshooting Common Issues
Apache Won’t Start
Check logs:
sudo tail -f /var/log/apache2/error.log
Common fixes:
- Port 80/443 in use:
sudo lsof -i :80(kill the process withsudo kill -9 [PID]). - Invalid config:
sudo apache2ctl configtest.
MySQL Connection Errors
- Access denied: Ensure the database user has the correct permissions.
- Service not running:
sudo systemctl restart mariadb.
PHP Not Executing
- Ensure
libapache2-mod-phpis installed:sudo apt install libapache2-mod-php -y. - Restart Apache:
sudo systemctl reload apache2.
References
- Ubuntu Server Documentation
- Apache HTTP Server Documentation
- MariaDB Documentation
- PHP Documentation
- Let’s Encrypt & Certbot
- UFW Firewall Guide
By following these steps, you’ve built a secure, production-ready Linux web server. Whether hosting a blog, e-commerce site, or custom app, this foundation will scale with your needs. Happy hosting! 🚀