Initial Server Setup for Ubuntu 24.04

    Everything you need to go from a fresh Ubuntu 24.04 instance to a production-ready web server

    This guide walks you through every essential step after launching a fresh Ubuntu 24.04 LTS Cloud VPS — from system updates and user creation through installing a complete LEMP or LAMP web server stack. Where dedicated documentation already exists for a topic, we link directly to it so you can dive deeper without duplicated information.

    What You'll Set Up

    System updates and package management
    Non-root sudo user with SSH key access
    UFW firewall and SSH hardening
    Swap space and automatic security updates
    LEMP stack (Nginx + MySQL + PHP)
    LAMP stack (Apache + MySQL + PHP)

    Prerequisites

    • • A RamNode Cloud VPS running Ubuntu 24.04 LTS
    • • Root SSH access to the server (see SSH Keys)
    • • A domain name pointed at your server's IP (optional, but required for SSL)

    1. System Updates

    After your first SSH login, update the package index and upgrade all installed packages to their latest versions:

    Update and upgrade packages
    apt update && apt upgrade -y

    If a kernel update was installed, reboot to load it:

    Reboot if needed
    reboot

    2. Hostname & Timezone

    Setting a proper hostname and timezone is important for logging, scheduled tasks, and SSL certificates. We have a dedicated guide covering both topics in detail:

    Setting Hostname and Timezone →

    Configure hostnames (simple or FQDN), update /etc/hosts, set the timezone with timedatectl, and enable NTP synchronization.

    As a quick reference, here are the most common commands:

    Set hostname and timezone
    # Set the hostname
    hostnamectl set-hostname your-server.example.com
    
    # Set timezone to UTC (recommended for servers)
    timedatectl set-timezone UTC
    
    # Verify
    hostnamectl
    timedatectl

    3. Create a Sudo User

    Running everything as root is a security risk. Create a regular user with sudo privileges:

    Create user and grant sudo
    # Create a new user (replace 'deploy' with your preferred username)
    adduser deploy
    
    # Add the user to the sudo group
    usermod -aG sudo deploy

    Copy your SSH key to the new user so you can log in without a password:

    Copy SSH keys to new user
    # As root, copy the authorized_keys file
    rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

    Test the new user before proceeding — open a new terminal and connect:

    Test SSH as the new user
    ssh deploy@YOUR_SERVER_IP

    ⚠️ Important: Do not close your root session until you've confirmed the new user can log in and run sudo commands successfully.

    4. SSH Hardening

    Once your sudo user is working, harden SSH by disabling root login and password authentication:

    Edit SSH configuration
    sudo nano /etc/ssh/sshd_config

    Find and update these settings:

    /etc/ssh/sshd_config
    PermitRootLogin no
    PasswordAuthentication no
    PubkeyAuthentication yes
    Restart SSH
    sudo systemctl restart ssh

    Related Documentation

    5. Firewall Setup (UFW)

    Ubuntu 24.04 ships with UFW (Uncomplicated Firewall). Enable it and allow only the ports you need:

    Configure UFW
    # Allow SSH (always do this first!)
    sudo ufw allow OpenSSH
    
    # Allow HTTP and HTTPS for web traffic
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    
    # Enable the firewall
    sudo ufw enable
    
    # Verify status
    sudo ufw status verbose
    UFW Basics — Full Guide →

    Rate limiting, application profiles, port ranges, subnet rules, logging, and more.

    ⚠️ Warning: Always allow SSH before enabling UFW. If you're using Security Groups in the Cloud Control Panel, note that UFW and Security Groups work independently — both must allow traffic for it to reach your server.

    6. Swap Space

    Adding swap space provides a safety net when your applications temporarily exceed available RAM. This is especially useful on smaller instances.

    Swap Partition — Full Guide →

    Detailed instructions for creating swap files, tuning swappiness, and sizing recommendations per VPS plan.

    Quick setup for a 2 GB swap file:

    Create a swap file
    # Create a 2G swap file
    sudo fallocate -l 2G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    
    # Make it permanent
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
    
    # Verify
    free -h

    7. Automatic Security Updates

    Keeping your server patched against known vulnerabilities is critical. Ubuntu 24.04 can be configured to install security updates automatically using unattended-upgrades.

    Automatic Security Updates — Full Guide →

    Complete walkthrough for Ubuntu/Debian, RHEL/AlmaLinux/Rocky, openSUSE, and Arch Linux — including email notifications and automatic reboots.

    Quick-start for Ubuntu 24.04:

    Enable unattended-upgrades
    sudo apt install unattended-upgrades -y
    sudo dpkg-reconfigure -plow unattended-upgrades

    8. LEMP Stack (Nginx + MySQL + PHP)

    The LEMP stack uses Nginx as the web server, MySQL as the database, and PHP for server-side processing. It's the most popular stack for WordPress, Laravel, and other PHP applications due to Nginx's efficient handling of concurrent connections.

    8a. Install Nginx

    Install Nginx
    sudo apt install nginx -y
    sudo systemctl enable nginx
    sudo systemctl start nginx

    Verify Nginx is running by visiting http://YOUR_SERVER_IP — you should see the default Nginx welcome page.

    8b. Install MySQL

    Install MySQL
    sudo apt install mysql-server -y
    sudo systemctl enable mysql

    Run the security script to set a root password and remove insecure defaults:

    Secure MySQL installation
    sudo mysql_secure_installation

    Recommended answers: set a strong root password, remove anonymous users (Y), disallow remote root login (Y), remove test database (Y), and reload privilege tables (Y).

    Database Deployment Guides

    For more advanced database setups, see our dedicated guides:

    8c. Install PHP

    Install PHP and common extensions
    sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y

    Verify the PHP version and check that php-fpm is running:

    Verify PHP
    php -v
    sudo systemctl status php8.3-fpm

    8d. Configure an Nginx Virtual Host

    Create a server block for your domain. Replace example.com with your actual domain:

    Create Nginx server block
    server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/example.com/html;
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    Enable the site
    # Create the web root
    sudo mkdir -p /var/www/example.com/html
    sudo chown -R www-data:www-data /var/www/example.com
    
    # Save the config above to:
    sudo nano /etc/nginx/sites-available/example.com
    
    # Enable it
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    
    # Test and reload
    sudo nginx -t
    sudo systemctl reload nginx

    8e. Test PHP Processing

    Create a test file
    echo '<?php phpinfo(); ?>' | sudo tee /var/www/example.com/html/info.php

    Visit http://example.com/info.php. If you see the PHP info page, your LEMP stack is working. Delete this file afterward — it exposes sensitive configuration details:

    Remove test file
    sudo rm /var/www/example.com/html/info.php

    9. LAMP Stack (Apache + MySQL + PHP)

    The LAMP stack uses Apache instead of Nginx. Choose LAMP if your application relies on .htaccess files or Apache-specific modules. If you already installed Nginx in Step 8, skip this section — running both web servers on the same ports will cause conflicts.

    ⚠️ Choose one: Install either the LEMP stack (Step 8) or the LAMP stack (Step 9), not both. They serve the same purpose with different web servers.

    9a. Install Apache

    Install Apache
    sudo apt install apache2 -y
    sudo systemctl enable apache2
    sudo systemctl start apache2

    Verify Apache is running by visiting http://YOUR_SERVER_IP.

    9b. Install MySQL

    The MySQL installation is the same as in the LEMP stack above (Step 8b).

    Install and secure MySQL
    sudo apt install mysql-server -y
    sudo systemctl enable mysql
    sudo mysql_secure_installation

    9c. Install PHP for Apache

    Apache uses libapache2-mod-php instead of php-fpm:

    Install PHP with Apache module
    sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y

    9d. Configure an Apache Virtual Host

    Apache virtual host
    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/html
    
        <Directory /var/www/example.com/html>
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
        CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    </VirtualHost>
    Enable the site
    # Create web root
    sudo mkdir -p /var/www/example.com/html
    sudo chown -R www-data:www-data /var/www/example.com
    
    # Save the config above to:
    sudo nano /etc/apache2/sites-available/example.com.conf
    
    # Enable site and mod_rewrite
    sudo a2ensite example.com.conf
    sudo a2enmod rewrite
    sudo a2dissite 000-default.conf
    
    # Test and reload
    sudo apache2ctl configtest
    sudo systemctl reload apache2

    9e. Test PHP Processing

    Create and test
    echo '<?php phpinfo(); ?>' | sudo tee /var/www/example.com/html/info.php
    # Visit http://example.com/info.php, then remove:
    sudo rm /var/www/example.com/html/info.php

    10. SSL Certificates

    Once you have a domain pointed at your server and a web server running, add free SSL/TLS certificates from Let's Encrypt using Certbot. Our dedicated guide covers everything in detail:

    Let's Encrypt via Certbot — Full Guide →

    Install Certbot, obtain certificates for Nginx or Apache, configure auto-renewal, and troubleshoot common issues.

    Quick-start:

    Install Certbot and obtain SSL
    # For Nginx
    sudo apt install certbot python3-certbot-nginx -y
    sudo certbot --nginx -d example.com -d www.example.com
    
    # For Apache
    sudo apt install certbot python3-certbot-apache -y
    sudo certbot --apache -d example.com -d www.example.com
    
    # Verify auto-renewal
    sudo certbot renew --dry-run

    Next Steps

    Your Ubuntu 24.04 server is now secured, updated, and running a full web server stack. Here are some recommended next steps depending on what you plan to deploy: