Posted in

Mastering Your Ubuntu 24.04 Server: A Comprehensive Setup Guide for Developers

Mastering Your Ubuntu 24.04 Server: A Comprehensive Setup Guide for Developers
Mastering Your Ubuntu 24.04 Server: A Comprehensive Setup Guide for Developers

Setting up a robust development or production server can seem daunting, but with Ubuntu 24.04 as your base, it’s a streamlined process. This comprehensive guide will walk you through installing and configuring essential tools and services, from Basic server management and security to web servers, databases, programming languages, and containerization. By the end, you’ll have a fully equipped server ready for almost any modern application, ensuring a stable, secure, and efficient environment for your projects.

Prerequisites

Before you begin, ensure you have the following:

  • A fresh installation of Ubuntu 24.04 server.
  • SSH access to your server.
  • Basic familiarity with the Linux command line.
  • Root or sudo privileges for executing administrative commands.

1. Initial Server Setup: Create a Sudo User and Update System

It’s best practice to operate as a non-root user with sudo privileges for daily tasks, enhancing security and preventing accidental system damage.

Create a New Sudo User

Add a new user: Replace your_username with your desired username.

sudo adduser your_username

Follow the prompts to set a password and user information. You can usually press Enter to accept the defaults for the full name and other details.

Grant sudo privileges: Add your new user to the sudo group.

sudo usermod -aG sudo your_username

Pro-Tip: Log out of your root session and log back in as your new user to confirm sudo access. Test it with sudo whoami; it should return root.

Update and Upgrade System Packages

Always start with updating your package lists and upgrading installed packages to their latest versions.

sudo apt update
sudo apt upgrade -y

Warning: The -y flag automatically confirms prompts. Use it carefully, especially on production systems, but it’s generally safe for initial updates.

2. Configure Hostname and Time Synchronization

A descriptive hostname helps identify your server, and accurate time synchronization is crucial for logging, security, and many applications.

Change Hostname

View current hostname:

hostname

Set a new hostname: Replace your_new_hostname with your preferred name.

sudo hostnamectl set-hostname your_new_hostname

Update /etc/hosts file: This ensures your server can resolve its own name.

sudo nano /etc/hosts

Add an entry like this, replacing 127.0.0.1 if you have a specific internal IP, and your_new_hostname with your chosen name:

127.0.0.1       localhost
127.0.1.1       your_new_hostname

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Save and exit (Ctrl+X, Y, Enter).

Set Up Time Synchronization (NTP)

Ubuntu 24.04 uses systemd-timesyncd by default. Verify its status:

timedatectl status

If it’s active and synchronizing, you’re good. If not, you can ensure it’s enabled:

sudo timedatectl set-ntp true

Tip: Accurate time stamps are vital for debugging and security audits.

3. Install Essential Development Tools: Git, Python, Node.js, Java

These tools are fundamental for most development workflows, from version control to running applications.

Install Git

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

sudo apt install git -y

Configure Git (optional but recommended):

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Install Python 3 and pip

Python 3 is usually pre-installed on Ubuntu 24.04. Verify its version:

python3 --version

Install pip (Python package installer):

sudo apt install python3-pip -y

Example: Install a Python package like requests: pip install requests.

Install Node.js and npm

Node.js is a JavaScript runtime, and npm is its package manager.

sudo apt install nodejs npm -y

Verify installation:

node -v
npm -v

Pro-Tip: For managing multiple Node.js versions, consider using NVM (Node Version Manager) in a production environment, but for a quick setup, apt is fine.

Install Java (OpenJDK)

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

sudo apt install openjdk-17-jdk -y

Verify installation:

java -version

This will install Java 17, a long-term support (LTS) release.

4. Set Up Web Server and PHP Environment: Nginx, PHP 8.3, Composer

Nginx is a high-performance web server, and PHP-FPM allows Nginx to process PHP applications efficiently.

Install Nginx

sudo apt install nginx -y

Verify Nginx status:

sudo systemctl status nginx

Nginx should be active (running). You can access your server’s IP address in a web browser to see the default Nginx welcome page.

Install PHP 8.3 and FPM

sudo apt install php8.3 php8.3-fpm php8.3-mysql php8.3-cli php8.3-curl php8.3-json php8.3-mbstring php8.3-xml php8.3-gd php8.3-zip -y

Verify PHP-FPM status:

sudo systemctl status php8.3-fpm

Configure Nginx for PHP

Edit the default Nginx configuration file to process PHP files:

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

Locate the server block and make the following changes (uncomment or add lines):

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #	location ~ /.ht {
    #		deny all;
    #	}
}

Save and exit. Test Nginx configuration for syntax errors:

sudo nginx -t

If successful, reload Nginx:

sudo systemctl reload nginx

Example: Create a test PHP file (/var/www/html/info.php) with <?php phpinfo(); ?> and access http://your_server_ip/info.php to verify PHP is working.

Install Composer (PHP Dependency Manager)

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Verify installation:

composer --version

5. Secure Nginx with Let’s Encrypt SSL

Encrypting your website with SSL is crucial for security, SEO, and user trust.

Install Certbot

Certbot is the tool to obtain and renew Let’s Encrypt certificates.

sudo apt install certbot python3-certbot-nginx -y

Obtain and Install SSL Certificate

Warning: Ensure your domain’s DNS records (A/AAAA records) point to your server’s public IP address before proceeding.

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Follow the interactive prompts: enter your email, agree to terms, and choose whether to redirect HTTP to HTTPS. Certbot will automatically configure Nginx and set up automatic renewal.

Verify Auto-renewal

Test the dry run for renewal:

sudo certbot renew --dry-run

This confirms the renewal process is correctly configured.

6. Install Popular Databases: MySQL, MariaDB, PostgreSQL, Redis

Choose the database that best fits your application’s needs.

Install MySQL 8

sudo apt install mysql-server -y

Secure MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password, remove anonymous users, disallow remote root login, and remove test databases.

Install MariaDB Database

MariaDB is a community-developed fork of MySQL.

sudo apt install mariadb-server mariadb-client -y

Secure MariaDB installation:

sudo mysql_secure_installation

The prompts are similar to MySQL.

Install PostgreSQL

PostgreSQL is a powerful, open-source object-relational database system.

sudo apt install postgresql postgresql-contrib -y

Access PostgreSQL prompt as default user:

sudo -i -u postgres
psql

Exit with q.

Tip: You might want to create a new user and database for your application instead of using the default postgres user.

Install Redis Server

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

sudo apt install redis-server -y

Verify Redis status:

sudo systemctl status redis-server

Test Redis connectivity:

redis-cli ping

It should respond with PONG.

7. Implement Containerization with Docker and Docker Compose

Docker allows you to package applications into containers, making them portable and consistent across environments.

Install Docker Engine

Add Docker’s official GPG key:

sudo apt update
sudo apt install ca-certificates curl -y
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Add the repository to Apt sources:

echo 
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu 
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | 
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

Install Docker packages:

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Add User to Docker Group

This allows you to run Docker commands without sudo.

sudo usermod -aG docker your_username

Log out and log back in for the changes to take effect.

Verify Docker Installation

docker run hello-world

This command downloads a test image and runs it in a container. If successful, Docker is working correctly.

Install Docker Compose

Docker Compose is now part of the Docker CLI (docker-compose-plugin was installed above).

Verify Docker Compose:

docker compose version

You’ve now set up a robust Ubuntu 24.04 server with all the essential tools and services. You’re ready to deploy your applications, configure more specific services, or explore further customization options for your development or production needs.

Leave a Reply

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