Setting up a robust server environment on Ubuntu 24.04 involves installing and configuring various services and tools. This comprehensive guide will walk you through the essential steps to prepare your server for web hosting, application development, and data management. You’ll learn how to secure your Nginx web server with Let’s Encrypt SSL, install popular databases like MySQL, MariaDB, and PostgreSQL, set up caching with Redis, and install development essentials such as PHP, Node.js, Python, Java, Git, and Docker. By the end of this guide, you will have a well-equipped Ubuntu 24.04 server ready for your projects.
Prerequisites
Before you begin, ensure you have:
- A freshly installed Ubuntu 24.04 server (physical or virtual machine).
- SSH access to your server.
- A user with root privileges or the ability to use
sudo. - For Nginx with Let’s Encrypt: A registered domain name pointing to your server’s public IP address.
Create a Sudo User on Ubuntu 24.04
It’s best practice to operate as a non-root user with sudo privileges for daily administrative tasks, enhancing security.
Add a New User
First, create a new user. Replace yourusername with your desired username.
sudo adduser yourusername
You will be prompted to set a password and provide some optional user information. You can press Enter to skip the optional fields.
Grant Sudo Privileges
Add the new user to the sudo group to grant them administrative privileges.
sudo usermod -aG sudo yourusername
Pro-Tip: After adding the user, log out of your root session and log back in as yourusername. From now on, prepend sudo to commands that require root privileges.
Change Hostname on Ubuntu 24.04
A descriptive hostname makes it easier to identify your server, especially in multi-server environments.
Set the New Hostname
Use the hostnamectl command to change your system’s hostname. Replace new_hostname with your desired name.
sudo hostnamectl set-hostname new_hostname
Update the Hosts File
Edit the /etc/hosts file to reflect the new hostname, ensuring proper resolution.
sudo nano /etc/hosts
Find the line starting with 127.0.1.1 and change the old hostname to your new_hostname. It should look something like this:
127.0.0.1 localhost
127.0.1.1 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 the file (Ctrl+O, Enter, Ctrl+X). Reboot your server for the changes to take full effect, or simply log out and back in.
Set Up Time Synchronization (NTP) on Ubuntu 24.04
Accurate time synchronization is crucial for logging, security, and proper functioning of many services.
Verify Current Time Sync Status
Ubuntu 24.04 uses systemd-timesyncd by default. Check its status:
timedatectl status
If NTP service: active and System clock synchronized: yes are shown, you’re likely good. If not, or if you prefer a dedicated NTP client like Chrony:
Install and Configure Chrony (Optional, Recommended)
Chrony is a more robust alternative to systemd-timesyncd for precise time synchronization.
sudo apt update
sudo apt install chrony
Chrony will start automatically. You can verify its status:
chronyc tracking
chronyc sources
Warning: Installing Chrony will typically disable systemd-timesyncd automatically. Do not run both simultaneously.
How to Secure Nginx with Let’s Encrypt SSL on Ubuntu 24.04
Encrypting your Nginx web server with an SSL certificate from Let’s Encrypt secures communication between your server and clients.
Install Nginx
sudo apt update
sudo apt install nginx
Allow Nginx through the firewall:
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
Configure Nginx Server Block
Create an Nginx server block for your domain (e.g., /etc/nginx/sites-available/your_domain). Replace your_domain with your actual domain name.
sudo nano /etc/nginx/sites-available/your_domain
Add the following basic configuration:
server {
listen 80;
listen [::]:80;
server_name your_domain www.your_domain;
root /var/www/your_domain;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
Create the web root directory and a simple test page:
sudo mkdir -p /var/www/your_domain
sudo echo "<h1>Welcome to your_domain!</h1>" | sudo tee /var/www/your_domain/index.html
Enable the server block and test Nginx configuration:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
You should now be able to access your domain via HTTP.
Install Certbot
Certbot is the easiest way to obtain and install Let’s Encrypt SSL certificates.
sudo apt install certbot python3-certbot-nginx
Obtain and Install SSL Certificate
Run Certbot, which will automatically configure Nginx for HTTPS.
sudo certbot --nginx -d your_domain -d www.your_domain
Follow the prompts: enter your email, agree to terms, and choose whether to redirect HTTP traffic to HTTPS (recommended).
Verify Auto-Renewal
Let’s Encrypt certificates are valid for 90 days. Certbot sets up a cron job for automatic renewal.
sudo systemctl status certbot.timer
Test the renewal process:
sudo certbot renew --dry-run
Pro-Tip: Always ensure your domain’s DNS A records point to your server’s IP address before attempting to obtain a certificate. Otherwise, Certbot will fail.
How to Install MySQL 8 on Ubuntu 24.04
MySQL is a popular open-source relational database management system.
Install MySQL Server
sudo apt update
sudo apt install mysql-server
Secure MySQL Installation
Run the security script to improve the security of your MySQL installation.
sudo mysql_secure_installation
Follow the prompts: set a root password, remove anonymous users, disallow remote root login, remove test databases, and reload privilege tables. For the password validation plugin, it’s generally safe to enable it.
Test MySQL Access
sudo mysql
You should see the MySQL prompt. Type exit; to quit.
How to Install MariaDB Database on Ubuntu 24.04
MariaDB is a community-developed, commercially supported fork of the MySQL relational database management system.
Install MariaDB Server
sudo apt update
sudo apt install mariadb-server mariadb-client
Secure MariaDB Installation
Run the security script, similar to MySQL.
sudo mysql_secure_installation
Follow the prompts to set a root password and secure your installation.
Test MariaDB Access
sudo mariadb
You should see the MariaDB prompt. Type exit; to quit.
Warning: Do not install both MySQL and MariaDB on the same server unless you have a specific, advanced use case and know how to manage potential conflicts.
How to Install PostgreSQL on Ubuntu 24.04
PostgreSQL is a powerful, open-source object-relational database system known for its reliability, feature robustness, and performance.
Install PostgreSQL Server
sudo apt update
sudo apt install postgresql postgresql-contrib
Access PostgreSQL Prompt
By default, PostgreSQL creates a user named postgres with root privileges for the database. Switch to this user to access the PostgreSQL prompt.
sudo -i -u postgres
psql
You’ll be in the PostgreSQL prompt. Type q to quit and exit to return to your regular user.
Create a New Database and User (Optional)
For application use, it’s common to create dedicated users and databases.
sudo -i -u postgres
createuser --interactive
createdb your_database -O your_user
Pro-Tip: To allow remote connections, you’ll need to edit /etc/postgresql/16/main/postgresql.conf (change listen_addresses) and /etc/postgresql/16/main/pg_hba.conf (add connection rules), then restart PostgreSQL.
How to 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.
Install Redis Server
sudo apt update
sudo apt install redis-server
Verify Redis Status
Redis will start automatically after installation.
sudo systemctl status redis-server
You should see active (running).
Test Redis Functionality
Use the Redis command-line interface to interact with the server.
redis-cli
ping
It should respond with PONG. Type exit to quit.
Warning: By default, Redis listens on 127.0.0.1. If you need to access it from other servers, you’ll need to modify /etc/redis/redis.conf and set a strong password.
How to Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache)
PHP is a popular server-side scripting language, essential for many web applications.
Add Ondrej PHP PPA
Ubuntu’s default repositories might not have the latest PHP versions. Ondrej’s PPA is a reliable source.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Install PHP 8.3 and Extensions
Install PHP-FPM (FastCGI Process Manager) for Nginx, along with common extensions.
sudo apt install php8.3-fpm php8.3-mysql php8.3-cli php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip
Configure Nginx for PHP Processing
Edit your Nginx server block (e.g., /etc/nginx/sites-available/your_domain) to process PHP files. Add the following inside the server block, typically within or before the location / block:
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Also, change the index directive to include index.php:
index index.php index.html index.htm index.nginx-debian.html;
Test Nginx configuration and restart:
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm
Test PHP Processing
Create a PHP info file in your web root (e.g., /var/www/your_domain/info.php).
sudo nano /var/www/your_domain/info.php
Add the following content:
<?php phpinfo(); ?>
Save and exit. Navigate to http://your_domain/info.php in your browser. You should see the PHP information page. Delete this file after verification for security reasons.
How to Install Composer (PHP Dependency Manager) on Ubuntu 24.04
Composer is an essential dependency manager for PHP projects.
Download and Install Composer
Download the Composer installer and run it.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Verify Installation
composer --version
This should output the Composer version. If you encounter issues, ensure your PHP CLI is properly installed and accessible.
How to Install Node.js and npm on Ubuntu 24.04
Node.js is a JavaScript runtime environment, and npm is its package manager, crucial for modern web development.
Install from NodeSource PPA
NodeSource provides up-to-date Node.js packages. This example installs Node.js 20 (LTS).
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs
Verify Installation
node -v
npm -v
This will display the installed Node.js and npm versions.
Pro-Tip: For managing multiple Node.js versions, consider using NVM (Node Version Manager) instead of the PPA. Install NVM first, then use nvm install --lts and nvm use --lts.
How to Install Python 3 and pip on Ubuntu 24.04
Python 3 is usually pre-installed on Ubuntu 24.04. This section ensures pip (Python package installer) and venv (virtual environments) are available.
Verify Python 3 Installation
python3 --version
Install pip and venv
sudo apt update
sudo apt install python3-pip python3-venv
Verify pip Installation
pip3 --version
Pro-Tip: Always use virtual environments for your Python projects to manage dependencies cleanly and avoid conflicts with system-wide packages. Create one with python3 -m venv myproject_venv and activate with source myproject_venv/bin/activate.
How to Install Java (OpenJDK) on Ubuntu 24.04
OpenJDK is a free and open-source implementation of the Java Platform, Standard Edition.
Install Default JRE and JDK
Install the default Java Runtime Environment (JRE) and Java Development Kit (JDK).
sudo apt update
sudo apt install default-jre default-jdk
Verify Installation
java -version
javac -version
This will display the installed Java and Java compiler versions.
Pro-Tip: If you need a specific Java version (e.g., Java 17, Java 21), you can install it explicitly: sudo apt install openjdk-17-jdk. Use sudo update-alternatives --config java to switch between installed versions.
How to Install Git on Ubuntu 24.04
Git is a free and open-source distributed version control system.
Install Git
sudo apt update
sudo apt install git
Configure Git
Set your user name and email, which will be embedded in your commits.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Verify Installation
git --version
Pro-Tip: Consider generating an SSH key pair and adding your public key to services like GitHub or GitLab for secure, password-less repository access.
How to Install Docker and Docker Compose on Ubuntu 24.04
Docker is a platform for developing, shipping, and running applications in containers. Docker Compose is a tool for defining and running multi-container Docker applications.
Install Docker Engine
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Add User to Docker Group
To run Docker commands without sudo, add your user to the docker group. Log out and back in for this to take effect.
sudo usermod -aG docker yourusername
Verify Docker Installation
docker run hello-world
This command downloads a test image and runs it in a container. If successful, Docker is correctly installed.
Install Docker Compose
Docker Compose is typically installed as a plugin for Docker now.
sudo apt install docker-compose-plugin
Verify Docker Compose Installation
docker compose version
Warning: Running Docker commands without sudo after adding your user to the docker group can pose a security risk, as it grants capabilities equivalent to the root user. Only add trusted users to this group.
With these essential services and tools installed and configured, your Ubuntu 24.04 server is now a powerful foundation for a wide range of web applications, development projects, and data management tasks. Explore the capabilities of each service and tool to build and deploy your next big idea!
