Friday, July 4, 2025
Linux Tutorial

Steps to Set Up a Linux Web Server

Setting up your own Linux web server gives you full control over your hosting environment and can be a cost-effective solution for hosting websites and web applications. This comprehensive guide will walk you through the entire process from start to finish.

Prerequisites

Before you begin, you’ll need:

  • A computer or virtual machine running Linux (Ubuntu Server 22.04 LTS recommended)
  • Root or sudo access
  • A stable internet connection
  • A domain name (optional but recommended for production)

Step 1: Install a Linux Operating System

  1. Download Ubuntu Server from the official website
  2. Create a bootable USB using tools like Rufus or BalenaEtcher
  3. Install Ubuntu Server on your machine:
    • Select your language
    • Configure network settings
    • Set up disk partitioning (default is fine for most cases)
    • Create a user account with sudo privileges
    • Install OpenSSH server when prompted (for remote administration)
Linux Web Server
Complete Setup Guide
🔧 Apache/Nginx • MySQL • PHP • SSL

Step 2: Update the System

After installation, log in and run these commands:

sudo apt update && sudo apt upgrade -y
sudo apt autoremove -y

Step 3: Install a Web Server (Apache or Nginx)

Option A: Apache Web Server

sudo apt install apache2 -y

Check if Apache is running:

sudo systemctl status apache2

Option B: Nginx Web Server

sudo apt install nginx -y

Check if Nginx is running:

sudo systemctl status nginx

Step 4: Configure Firewall (UFW)

Allow HTTP (80) and HTTPS (443) traffic:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'  # For Apache
# OR
sudo ufw allow 'Nginx Full'   # For Nginx
sudo ufw enable

Verify firewall status:

sudo ufw status

Step 5: Install MySQL/MariaDB Database Server

sudo apt install mariadb-server -y
sudo mysql_secure_installation

Follow the prompts to set a root password and secure your installation.

Step 6: Install PHP (For Dynamic Content)

sudo apt install php libapache2-mod-php php-mysql -y  # For Apache
# OR
sudo apt install php-fpm php-mysql -y                  # For Nginx

Install additional PHP modules as needed:

sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

Step 7: Test Your Web Server

Create a test PHP file:

sudo nano /var/www/html/info.php

Add this content:

<?php phpinfo(); ?>

Visit http://your_server_ip/info.php in a web browser to verify PHP is working.

Important: Remove this file after testing for security:

sudo rm /var/www/html/info.php

Step 8: Configure Virtual Hosts (For Multiple Websites)

Apache Configuration:

Create a directory for your site:

sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html

Create a virtual host file:

sudo nano /etc/apache2/sites-available/yourdomain.com.conf

Add this configuration (adjust as needed):

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    ServerAdmin webmaster@yourdomain.com
    DocumentRoot /var/www/yourdomain.com/html
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and reload Apache:

sudo a2ensite yourdomain.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Nginx Configuration:

Create a directory for your site:

sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html

Create a server block file:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add this configuration (adjust as needed):

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    
    root /var/www/yourdomain.com/html;
    index index.html index.htm index.php;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

Enable the site and reload Nginx:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 9: Install SSL Certificate with Let’s Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y  # For Nginx
# OR
sudo apt install certbot python3-certbot-apache -y # For Apache

Obtain and install certificate:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com  # For Nginx
# OR
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com # For Apache

Certbot will automatically configure your web server to use HTTPS and set up automatic renewal.

Step 10: Set Up Automatic Updates

Keep your server secure with automatic security updates:

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

Step 11: Install phpMyAdmin (Optional for Database Management)

sudo apt install phpmyadmin -y

During installation:

  • Select your web server (Apache or Nginx)
  • Choose “Yes” to configure with dbconfig-common
  • Set a strong password for phpMyAdmin

For Nginx, create a symbolic link:

sudo ln -s /usr/share/phpmyadmin /var/www/yourdomain.com/html/phpmyadmin

Secure phpMyAdmin by restricting access:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add this inside your server block:

location /phpmyadmin {
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/pma_pass;
}

Create a password file:

sudo sh -c "echo -n 'admin:' >> /etc/nginx/pma_pass"
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/pma_pass"

Step 12: Monitor Server Performance

Install monitoring tools:

sudo apt install htop sysstat -y

Use these commands to monitor your server:

  • htop – Interactive process viewer
  • df -h – Disk space usage
  • free -m – Memory usage
  • uptime – System load and uptime

Step 13: Regular Maintenance Tasks

Backup your data regularly

Check for updates weekly:

sudo apt update && sudo apt upgrade -y

Monitor logs:

sudo tail -f /var/log/nginx/error.log  # For Nginx
# OR
sudo tail -f /var/log/apache2/error.log # For Apache

Conclusion

You now have a fully functional Linux web server running either Apache or Nginx, MySQL/MariaDB, and PHP. This setup is suitable for hosting websites, web applications, and content management systems like WordPress, Joomla, or Drupal.

Remember to:

  • Keep your server updated
  • Implement strong passwords
  • Regularly back up your data
  • Monitor server performance
  • Consider implementing additional security measures like fail2ban

For production environments, you may want to explore additional optimizations such as:

  • Implementing a CDN
  • Setting up server caching
  • Configuring a reverse proxy
  • Implementing load balancing for high-traffic sites

A big thank you for exploring TechsBucket! Your visit means a lot to us, and we’re grateful for your time on our platform. If you have any feedback or suggestions, we’d love to hear them.

Leave a Response