How To Setup A Linux Server – Apache And MySQL Installation

Building a Linux server starts with choosing a distribution, such as Ubuntu Server, and installing it on dedicated hardware. This guide will show you exactly how to setup a linux server from scratch, covering every step from installation to securing your system. By the end, you’ll have a fully functional server ready for hosting websites, applications, or files.

How To Setup A Linux Server

Setting up a Linux server might sound complex, but it’s actually straightforward if you follow a clear plan. You’ll need some basic hardware, a Linux distribution ISO file, and about an hour of your time. Let’s break it down into manageable steps.

Choosing Your Linux Distribution

First, pick a distribution that matches your needs. Ubuntu Server is great for beginners because of its huge community support. CentOS or Rocky Linux work well for enterprise environments. Debian is stable and lightweight. For this guide, we’ll use Ubuntu Server 22.04 LTS.

  • Ubuntu Server: User-friendly, extensive documentation
  • CentOS/Rocky Linux: Enterprise-grade, RHEL compatible
  • Debian: Rock-solid stability, minimal bloat
  • Fedora Server: Cutting-edge features

Preparing Your Hardware

You don’t need expensive equipment. A basic machine with at least 2GB RAM, 20GB storage, and a dual-core processor works fine. If you’re using a virtual machine, allocate similar resources. Make sure you have a stable internet connection and a USB drive (4GB or larger) for the installer.

Download the Ubuntu Server ISO from the official website. Use a tool like Rufus (Windows) or Balena Etcher (Mac/Linux) to create a bootable USB drive. Insert the USB into your server machine and reboot.

Installing Ubuntu Server

Boot from the USB drive. You’ll see a menu with installation options. Select “Install Ubuntu Server” and follow these steps:

  1. Choose your language and keyboard layout
  2. Select “Ubuntu Server” as the installation type
  3. Configure network interfaces (usually DHCP works)
  4. Set up storage: Use the entire disk or partition manually
  5. Create a user account with a strong password
  6. Install OpenSSH server when prompted (very important for remote access)
  7. Let the installation complete, then reboot

After reboot, remove the USB drive. You’ll see a login prompt. Enter your username and password. Congratulations, you’ve installed Linux!

Initial Server Configuration

Now you need to configure your server for daily use. First, update the system packages. Run these commands:

sudo apt update
sudo apt upgrade -y

This ensures you have the latest security patches and software versions. Next, set the hostname to something meaningful:

sudo hostnamectl set-hostname myserver

Edit the hosts file to map the hostname to your IP:

sudo nano /etc/hosts

Add a line like: 192.168.1.100 myserver

Securing Your Linux Server

Security is critical for any server. Start by disabling root login over SSH. Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Find the line PermitRootLogin yes and change it to no. Also, change the default SSH port (22) to something else like 2222. This reduces automated attacks. Then restart SSH:

sudo systemctl restart sshd

Set up a firewall using UFW (Uncomplicated Firewall):

sudo ufw allow 2222/tcp
sudo ufw enable

Only allow necessary ports. For a web server, you’d also allow 80 and 443. Enable automatic security updates:

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

Setting Up User Accounts

Create individual user accounts for anyone who needs access. Use the adduser command:

sudo adduser john

Add the user to the sudo group for administrative privileges:

sudo usermod -aG sudo john

Each user should have their own SSH key pair. Generate keys on the client machine and copy the public key to the server:

ssh-copy-id john@server-ip

Disable password authentication in SSH for better security. Edit /etc/ssh/sshd_config and set PasswordAuthentication no.

Installing Essential Software

Depending on your server’s purpose, you’ll need different software. For a web server, install Nginx or Apache:

sudo apt install nginx

For a database server, install MySQL or PostgreSQL:

sudo apt install mysql-server

For a file server, install Samba:

sudo apt install samba

Always check the official documentation for each software package. Configure them according to your needs.

Configuring Networking

Set a static IP address so your server always has the same address. Edit the Netplan configuration file:

sudo nano /etc/netplan/00-installer-config.yaml

Example configuration for a static IP:

network:
  ethernets:
    ens33:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  version: 2

Apply the changes:

sudo netplan apply

Test connectivity by pinging an external site:

ping google.com

Monitoring And Logging

Set up basic monitoring to keep an eye on your server’s health. Install tools like htop and netstat:

sudo apt install htop net-tools

Check system logs with journalctl:

sudo journalctl -xe

For more advanced monitoring, consider installing Nagios or Zabbix. But for most small setups, these basics suffice.

Backup Strategies

Regular backups are non-negotiable. Use rsync to copy important data to an external drive or cloud storage:

rsync -avz /var/www/ user@backup-server:/backup/

Automate backups with cron jobs. Edit the crontab:

crontab -e

Add a line to run backups daily at 2 AM:

0 2 * * * rsync -avz /var/www/ /backup/

Test your backups regularly to ensure they work. A backup is only useful if you can restore from it.

Performance Tuning

Optimize your server for better performance. Adjust kernel parameters by editing /etc/sysctl.conf:

net.core.somaxconn = 1024
vm.swappiness = 10

Apply changes with sudo sysctl -p. For database servers, tune MySQL or PostgreSQL settings based on your RAM and workload.

Monitor resource usage with tools like top or htop. If your server runs out of memory, consider adding swap space:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Add the swap entry to /etc/fstab for persistence.

Troubleshooting Common Issues

Even with careful setup, problems arise. Here are common issues and fixes:

  • SSH connection refused: Check if SSH service is running (systemctl status sshd)
  • Firewall blocking traffic: Use sudo ufw status to verify rules
  • DNS resolution fails: Check /etc/resolv.conf for correct nameservers
  • Disk full: Use df -h to check usage and clean up old logs
  • Service won’t start: Check logs with journalctl -u servicename

Always check the system logs first when troubleshooting. They usually contain the exact error message.

Advanced Configuration

For production servers, consider these advanced steps:

Setting Up A Firewall With IPTables

While UFW is simpler, IPTables gives you more control. Here’s a basic ruleset:

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 2222 -j ACCEPT

Save rules with sudo iptables-save > /etc/iptables/rules.v4.

Configuring A Web Server

For Nginx, create a server block:

sudo nano /etc/nginx/sites-available/mysite

Example configuration:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
}

Enable the site and restart Nginx:

sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Automating Tasks With Cron

Cron is essential for automating routine tasks. Common cron jobs include:

  • Daily backups
  • Log rotation
  • System updates
  • Security scans

Example cron job to update packages weekly:

0 3 * * 0 sudo apt update && sudo apt upgrade -y

Always test cron jobs manually before scheduling them. Check the cron log at /var/log/syslog for errors.

Documenting Your Setup

Keep detailed documentation of your server configuration. Note down:

  • IP addresses and hostnames
  • Installed software versions
  • Firewall rules
  • Backup schedules
  • User accounts and permissions

This documentation will save you hours when troubleshooting or rebuilding the server.

Scaling Your Server

As your needs grow, you might need to scale. Options include:

  • Adding more RAM or storage
  • Using load balancers for multiple servers
  • Migrating to cloud providers like AWS or DigitalOcean
  • Implementing containerization with Docker

Start small and scale as needed. Overprovisioning wastes resources.

Frequently Asked Questions

What Is The Easiest Way To Setup A Linux Server?

Ubuntu Server is the easiest for beginners. Download the ISO, create a bootable USB, and follow the installation wizard. The default options work for most setups.

Do I Need A Static IP For My Linux Server?

Yes, a static IP is recommended so clients can always find your server. Configure it during installation or via Netplan after setup.

How Do I Access My Linux Server Remotely?

Enable OpenSSH during installation. From another computer, use ssh username@server-ip. For Windows, use PuTTY or the built-in SSH client.

What Are The Minimum Hardware Requirements For A Linux Server?

2GB RAM, 20GB storage, and a dual-core processor are minimum. For production, aim for 4GB RAM and 50GB storage or more.

How Often Should I Update My Linux Server?

Update at least weekly. Enable automatic security updates to patch critical vulnerabilities promptly.

Setting up a Linux server is a rewarding skill. Start with the basics, secure your system, and gradually add features. With practice, you’ll be able to deploy and manage servers confidently. Remember to backup regularly and keep learning.