Posted in

Comprehensive Server Setup: A Critical Guide to Essential Installations on Ubuntu 24.04

Comprehensive Server Setup: A Critical Guide to Essential Installations on Ubuntu 24.04
Comprehensive Server Setup: A Critical Guide to Essential Installations on Ubuntu 24.04

Establishing a robust and functional server environment is a foundational step for deploying any web application or service. This guide provides a precise, step-by-step methodology for setting up a comprehensive server infrastructure on Ubuntu 24.04, encompassing web servers, databases, programming runtimes, and essential development tools. By following these instructions, you will systematically configure your server from Basic user management to advanced application deployment readiness, ensuring a stable and secure foundation for your projects.

Prerequisites

Before commencing, ensure the following requirements are met:

  • An Ubuntu 24.04 server instance.
  • SSH access to the server.
  • A non-root user with sudo privileges. This guide assumes you are executing commands as this sudo user.
  • An active internet connection for package downloads.

Initial Server Configuration

Create a Sudo User on Ubuntu 24.04

For enhanced security and best practice, avoid using the root user directly. Create a new user and grant them administrative privileges.

  1. Add a new user:
    sudo adduser your_username
    Follow the prompts to set a strong password and user information.
  2. Add the user to the sudo group:
    sudo usermod -aG sudo your_username
    This grants your user the ability to execute commands with root privileges using sudo.
  3. Verify sudo access:
    Log out and log back in as the new user, then run: sudo whoami. It should return root.

Pro-Tip: Always use strong, unique passwords for new user accounts to prevent unauthorized access.

Set Up SSH Keys on Ubuntu 24.04

SSH keys provide a more secure and convenient method of authentication than passwords. This involves copying your local public key to the server.

  1. From your local machine, copy your public key:
    ssh-copy-id your_username@your_server_ip
    If you have a custom key, specify it with -i ~/.ssh/your_key.pub.
  2. Test SSH login:
    ssh your_username@your_server_ip
    You should be logged in without a password prompt.

Warning: Ensure your private key file (~/.ssh/id_rsa or similar) on your local machine has restrictive permissions (chmod 600 ~/.ssh/id_rsa).

Change Hostname on Ubuntu 24.04

A descriptive hostname makes server identification easier, especially in multi-server environments.

  1. Set the new hostname:
    sudo hostnamectl set-hostname your_new_hostname
  2. Edit the /etc/hosts file:
    sudo nano /etc/hosts
    Ensure the line 127.0.0.1 localhost is followed by 127.0.1.1 your_new_hostname.
  3. Verify the change:
    hostnamectl

Tip: Choose a hostname that reflects the server’s primary function or location.

Set Up Time Synchronization (NTP) on Ubuntu 24.04

Accurate timekeeping is critical for logging, security, and consistent data management across systems.

  1. Verify systemd-timesyncd status:
    timedatectl status
    Ubuntu 24.04 typically uses systemd-timesyncd by default.
  2. Ensure NTP is enabled and synchronized:
    If NTP is not active, enable it: sudo timedatectl set-ntp true

Warning: Incorrect time synchronization can lead to issues with SSL certificates, database replication, and log analysis.

Web Server Installations and Configuration

Install Nginx Web Server on Ubuntu 24.04

Nginx is a high-performance web server, reverse proxy, and load balancer, known for its efficiency and scalability.

  1. Update package lists:
    sudo apt update
  2. Install Nginx:
    sudo apt install nginx -y
  3. Adjust the firewall (UFW):
    sudo ufw allow 'Nginx HTTP' (for port 80)
    sudo ufw allow 'Nginx HTTPS' (for port 443, required later for SSL)
    sudo ufw reload
  4. Verify Nginx status:
    systemctl status nginx
    Access your server’s IP in a web browser to see the default Nginx welcome page.

Install Apache Web Server on Ubuntu 24.04

Apache HTTP Server is a widely used, robust, and feature-rich web server.

  1. Update package lists:
    sudo apt update
  2. Install Apache:
    sudo apt install apache2 -y
  3. Adjust the firewall (UFW):
    sudo ufw allow 'Apache' (for HTTP and HTTPS)
    sudo ufw reload
  4. Verify Apache status:
    systemctl status apache2
    Access your server’s IP in a web browser to see the default Apache welcome page.

Note: You typically choose either Nginx or Apache for primary web serving. If both are installed, they must listen on different ports to avoid conflicts.

Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 24.04

Server blocks allow Nginx to host multiple domains on a single server.

  1. Create a directory for your domain:
    sudo mkdir -p /var/www/your_domain/html
  2. Create a sample index.html file:
    echo '<h1>Hello from your_domain!</h1>' | sudo tee /var/www/your_domain/html/index.html
  3. Set correct permissions:
    sudo chown -R $USER:$USER /var/www/your_domain/html
    sudo chmod -R 755 /var/www/your_domain
  4. Create the server block configuration file:
    sudo nano /etc/nginx/sites-available/your_domain
    Paste the following, replacing your_domain:

    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/your_domain/html;
        index index.html index.htm index.nginx-debian.html;
    
        server_name your_domain www.your_domain;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  5. Enable the server block by creating a symlink:
    sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
  6. Test Nginx configuration for syntax errors:
    sudo nginx -t
  7. Reload Nginx to apply changes:
    sudo systemctl reload nginx

Pro-Tip: Always test your Nginx configuration with sudo nginx -t before reloading to avoid downtime from syntax errors.

Set Up Apache Virtual Hosts on Ubuntu 24.04

Virtual hosts enable Apache to serve multiple websites from a single server.

  1. Create a directory for your domain:
    sudo mkdir -p /var/www/your_domain/html
  2. Create a sample index.html file:
    echo '<h1>Hello from your_domain!</h1>' | sudo tee /var/www/your_domain/html/index.html
  3. Set correct permissions:
    sudo chown -R $USER:$USER /var/www/your_domain/html
    sudo chmod -R 755 /var/www/your_domain
  4. Create the virtual host configuration file:
    sudo nano /etc/apache2/sites-available/your_domain.conf
    Paste the following, replacing your_domain:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName your_domain
        ServerAlias www.your_domain
        DocumentRoot /var/www/your_domain/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  5. Enable the virtual host:
    sudo a2ensite your_domain.conf
  6. Disable the default virtual host (optional, but recommended for clean setups):
    sudo a2dissite 000-default.conf
  7. Test Apache configuration:
    sudo apache2ctl configtest
  8. Reload Apache to apply changes:
    sudo systemctl reload apache2

Warning: Ensure your domain’s DNS A records point to your server’s IP address for the virtual host to be accessible.

Secure Nginx with Let’s Encrypt SSL on Ubuntu 24.04

Encrypting traffic with SSL/TLS is crucial for security and user trust. Let’s Encrypt provides free certificates.

  1. Install Certbot and its Nginx plugin:
    sudo apt install certbot python3-certbot-nginx -y
  2. Obtain and install SSL certificate:
    sudo certbot --nginx -d your_domain -d www.your_domain
    Follow the interactive prompts (email, terms of service, redirection).
  3. Verify automatic renewal:
    sudo systemctl status certbot.timer
    Certbot automatically sets up a cron job or systemd timer for renewals.
  4. Test renewal process:
    sudo certbot renew --dry-run

Important: This step requires your domain to be publicly accessible and its DNS records correctly pointing to your server.

Database Server Installations

Install MySQL 8 on Ubuntu 24.04

MySQL is a popular open-source relational database management system.

  1. Install MySQL Server:
    sudo apt install mysql-server -y
  2. Run the security script:
    sudo mysql_secure_installation
    Follow prompts to set a root password, remove anonymous users, disallow remote root login, and remove test database.
  3. Verify MySQL status:
    systemctl status mysql

Pro-Tip: Always run mysql_secure_installation immediately after installing MySQL to enhance security.

Install MariaDB Database on Ubuntu 24.04

MariaDB is a community-developed fork of MySQL, offering similar functionality with some performance and feature enhancements.

  1. Install MariaDB Server:
    sudo apt install mariadb-server -y
  2. Run the security script:
    sudo mysql_secure_installation
    Similar to MySQL, follow the prompts for security hardening.
  3. Verify MariaDB status:
    systemctl status mariadb

Note: Choose either MySQL or MariaDB; installing both simultaneously is generally not recommended unless for specific testing scenarios.

Install PostgreSQL on Ubuntu 24.04

PostgreSQL is a powerful, open-source object-relational database system known for its reliability and feature robustness.

  1. Install PostgreSQL:
    sudo apt install postgresql postgresql-contrib -y
  2. Switch to the postgres user:
    sudo -i -u postgres
  3. Access the PostgreSQL prompt:
    psql
  4. Set a password for the postgres user (optional, but recommended):
    password postgres
    Enter and confirm a strong password.
  5. Exit PostgreSQL and the postgres user:
    q (to exit psql)
    exit (to exit postgres user)

Tip: PostgreSQL uses ‘roles’ instead of traditional ‘users’ for authentication and authorization. The default administrative role is ‘postgres’.

Install Redis Server on Ubuntu 24.04

Redis is an open-source, in-memory data structure store, used as a database, cache, and message broker.

  1. Install Redis:
    sudo apt install redis-server -y
  2. Verify Redis status:
    systemctl status redis-server
  3. Test Redis functionality:
    redis-cli ping (should return PONG)

Warning: For production environments, consider securing Redis by enabling password authentication and binding it to a local interface or specific IP addresses only.

Programming Language Runtimes and Tools

Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache)

PHP is a widely used server-side scripting language, essential for many web applications.

  1. Add the PPA for newer PHP versions:
    sudo apt install software-properties-common -y
    sudo add-apt-repository ppa:ondrej/php -y
    sudo apt update
  2. Install PHP 8.3 FPM (for Nginx) and common extensions:
    sudo apt install php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip -y
    For Apache, install libapache2-mod-php8.3 instead of or in addition to php8.3-fpm.
  3. Verify PHP-FPM status (for Nginx):
    systemctl status php8.3-fpm
  4. Configure Nginx to use PHP-FPM:
    Edit your Nginx server block (e.g., /etc/nginx/sites-available/your_domain) to include the PHP processing block, typically uncommenting and adjusting the location ~ .php$ block to point to /run/php/php8.3-fpm.sock.
  5. Reload Nginx:
    sudo systemctl reload nginx

Tip: Always install necessary PHP extensions based on your application’s requirements. Use php -m to list installed modules.

Install Composer (PHP Dependency Manager) on Ubuntu 24.04

Composer is an essential dependency manager for PHP projects.

  1. Download Composer installer:
    curl -sS https://getcomposer.org/installer | php
  2. Move Composer to a global executable path:
    sudo mv composer.phar /usr/local/bin/composer
  3. Verify Composer installation:
    composer --version

Pro-Tip: Keeping Composer globally available simplifies project dependency management across your server.

Install Node.js and npm on Ubuntu 24.04

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and npm is its package manager.

  1. Install curl and git (if not already present):
    sudo apt install curl git -y
  2. Install NVM (Node Version Manager) for flexible Node.js versions:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    Close and reopen your terminal, or source your shell’s rc file (e.g., source ~/.bashrc).
  3. Install the latest LTS version of Node.js:
    nvm install --lts
  4. Verify Node.js and npm versions:
    node -v
    npm -v

Warning: Using NVM is highly recommended for managing multiple Node.js versions on a single server, preventing conflicts between projects.

Install Python 3 and pip on Ubuntu 24.04

Python 3 is usually pre-installed on Ubuntu 24.04. This step ensures pip, its package installer, is also available.

  1. Verify Python 3 installation:
    python3 --version
  2. Install pip for Python 3:
    sudo apt install python3-pip -y
  3. Verify pip installation:
    pip3 --version

Tip: Use python3 -m pip install package_name for clarity, especially if you have multiple Python versions.

Install Java (OpenJDK) on Ubuntu 24.04

OpenJDK is a free and open-source implementation of the Java Platform, Standard Edition.

  1. Install the default JRE (Java Runtime Environment):
    sudo apt install default-jre -y
  2. Install the default JDK (Java Development Kit) for development:
    sudo apt install default-jdk -y
  3. Verify Java version:
    java -version
    javac -version

Pro-Tip: For specific Java versions, search for openjdk-XX-jre or openjdk-XX-jdk, replacing XX with the desired version number.

Development and Containerization Tools

Install Git on Ubuntu 24.04

Git is a distributed version control system essential for collaborative development and code management.

  1. Install Git:
    sudo apt install git -y
  2. Configure Git with your identity:
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
  3. Verify Git installation:
    git --version

Tip: Proper Git configuration ensures your commits are correctly attributed.

Install Docker and Docker Compose on Ubuntu 24.04

Docker provides containerization for packaging applications and their dependencies, while Docker Compose facilitates multi-container Docker applications.

  1. Update package lists and install dependencies:
    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
  2. Add Docker’s official GPG key:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  3. Add the Docker repository:
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  4. Install Docker Engine:
    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io -y
  5. Add your user to the docker group:
    sudo usermod -aG docker $USER
    Log out and log back in for the changes to take effect.
  6. Verify Docker installation:
    docker run hello-world (should download and run a test container)
  7. Install Docker Compose:
    sudo apt install docker-compose-plugin -y (for Docker Compose V2, the recommended approach)
    Or, for V1 (legacy):
    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
  8. Verify Docker Compose installation:
    docker compose version (for V2)
    docker-compose --version (for V1)

Warning: Running Docker commands without sudo (after adding your user to the docker group) grants significant privileges. Exercise caution.

With these installations and configurations complete, your Ubuntu 24.04 server is now equipped with a robust set of tools and services. You can proceed to deploy your applications, configure specific security policies, or delve deeper into optimizing individual components based on your project requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *