This comprehensive guide details the precise installation and configuration of a robust set of services and development tools on a fresh Ubuntu 24.04 server. By following these instructions, you will establish a solid foundation for deploying web applications, managing databases, and facilitating efficient development workflows. The focus is on critical, direct steps to ensure a secure and functional server environment, addressing common pitfalls and best practices.
Prerequisites
Before commencing, ensure you have:
- A freshly provisioned Ubuntu 24.04 server instance.
- SSH access to the server.
- A stable internet connection.
- Root privileges or access to a user with
sudocapabilities. While root access is initially available, creating a dedicated sudo user is a critical security measure addressed below.
1. Create a Sudo User on Ubuntu 24.04
Operating as the root user for daily tasks is a significant security risk. Creating a non-root user with administrative privileges is a fundamental best practice.
1.1. Add the New User
Execute the following command to create a new user, replacing yourusername with your desired username:
sudo adduser yourusername
You will be prompted to set a password and provide optional user information. A strong, unique password is non-negotiable.
1.2. Grant Sudo Privileges
Add the new user to the sudo group, which grants them the ability to execute commands with root privileges:
sudo usermod -aG sudo yourusername
Pro-tip: Always test the new sudo user by logging in and running a simple sudo apt update before logging out of the root session. This prevents locking yourself out of administrative control.
2. Set Up SSH Keys on Ubuntu 24.04
SSH keys provide a more secure and convenient method for logging into your server than passwords. Password-based authentication is inherently vulnerable to brute-force attacks.
2.1. Generate SSH Key Pair (Local Machine)
If you don’t already have an SSH key pair on your local machine, generate one:
ssh-keygen -t rsa -b 4096
Press Enter to accept the default file location (~/.ssh/id_rsa) and optionally set a strong passphrase for added security.
2.2. Copy Public Key to Server
Use ssh-copy-id to transfer your public key to the server. Replace yourusername and your_server_ip accordingly:
ssh-copy-id yourusername@your_server_ip
You will be prompted for your user’s password on the server. If ssh-copy-id is not available, you can manually copy the key:
cat ~/.ssh/id_rsa.pub | ssh yourusername@your_server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Warning: After verifying key-based login, consider disabling password authentication in /etc/ssh/sshd_config by setting PasswordAuthentication no and restarting the SSH service (sudo systemctl restart ssh). This significantly hardens your server’s security posture.
3. Change Hostname on Ubuntu 24.04
A descriptive hostname improves server identification and management, especially in multi-server environments.
3.1. Modify Hostname
Use hostnamectl to set the new hostname:
sudo hostnamectl set-hostname your_new_hostname
3.2. Update Hosts File
Edit the /etc/hosts file to reflect the new hostname:
sudo nano /etc/hosts
Ensure a line similar to 127.0.0.1 your_new_hostname exists and is correct. For example:
127.0.0.1 localhost
127.0.1.1 your_new_hostname
Pro-tip: While hostnamectl often handles the change gracefully, a reboot (sudo reboot) is the most reliable way to ensure all system components recognize the new hostname.
4. Set Up Time Synchronization (NTP) on Ubuntu 24.04
Accurate time synchronization is critical for logging, security, and consistent application behavior. Ubuntu 24.04 uses systemd-timesyncd by default.
4.1. Verify NTP Status
Check if time synchronization is active:
timedatectl status
Look for NTP synchronized: yes. If it’s no, enable it:
sudo timedatectl set-ntp true
Practical Tip: While systemd-timesyncd is sufficient for most cases, for high-precision requirements or specific network configurations, consider installing and configuring the more feature-rich ntpstat or chrony packages.
5. Install Git on Ubuntu 24.04
Git is the industry-standard version control system, essential for any development workflow.
5.1. Update and Install Git
sudo apt update
sudo apt install git -y
5.2. Configure Git
Set your global Git username and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Use Case: This configuration ensures your commits are correctly attributed, a fundamental aspect of collaborative development.
6. Install Nginx Web Server on Ubuntu 24.04
Nginx is a high-performance web server, excellent for serving static content, acting as a reverse proxy, and handling high concurrency.
6.1. Install Nginx
sudo apt update
sudo apt install nginx -y
6.2. Adjust Firewall
Allow Nginx traffic through UFW (Uncomplicated Firewall):
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw enable
sudo ufw status
Warning: Failing to configure the firewall will prevent external access to your web server, leading to frustrating connectivity issues.
6.3. Verify Nginx Status
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
Access your server’s IP address in a web browser to see the default Nginx welcome page.
7. Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 24.04
Server blocks allow Nginx to host multiple domains on a single server.
7.1. Create Directory Structure
Create a directory for your domain’s files:
sudo mkdir -p /var/www/your_domain
Assign ownership:
sudo chown -R $USER:$USER /var/www/your_domain
sudo chmod -R 755 /var/www/your_domain
7.2. Create Sample Index Page
Create a Basic index.html file:
sudo nano /var/www/your_domain/index.html
Add simple HTML content:
<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain server block is working!</h1>
</body>
</html>
7.3. Create Server Block Configuration
Create a new configuration file for your domain:
sudo nano /etc/nginx/sites-available/your_domain
Add the following content, replacing your_domain:
server {
listen 80;
listen [::]:80;
root /var/www/your_domain;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
7.4. Enable Server Block and Test
Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Test Nginx configuration for syntax errors:
sudo nginx -t
If successful, restart Nginx:
sudo systemctl restart nginx
Pro-tip: Always test your Nginx configuration with sudo nginx -t before restarting. This prevents downtime caused by syntax errors.
8. Install Apache Web Server on Ubuntu 24.04
Apache is another robust and widely used web server, known for its flexibility and extensive module ecosystem.
8.1. Install Apache
sudo apt update
sudo apt install apache2 -y
8.2. Adjust Firewall
Allow Apache traffic:
sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status
8.3. Verify Apache Status
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
Access your server’s IP to see the default Apache welcome page.
9. Set Up Apache Virtual Hosts on Ubuntu 24.04
Virtual hosts enable Apache to serve multiple websites from a single server.
9.1. Create Directory Structure
sudo mkdir -p /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain
sudo chmod -R 755 /var/www/your_domain
9.2. Create Sample Index Page
sudo nano /var/www/your_domain/index.html
Add basic HTML content:
<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
9.3. Create Virtual Host Configuration
Create a new configuration file:
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following, replacing your_domain:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
9.4. Enable Virtual Host and Restart
Enable the virtual host and rewrite module:
sudo a2ensite your_domain.conf
sudo a2enmod rewrite
Disable the default virtual host to avoid conflicts:
sudo a2dissite 000-default.conf
Test Apache configuration:
sudo apache2ctl configtest
If successful, restart Apache:
sudo systemctl restart apache2
Warning: Ensure your domain’s DNS A/AAAA records point to your server’s IP address for virtual hosts to function correctly.
10. Secure Nginx with Let’s Encrypt SSL on Ubuntu 24.04
SSL/TLS encryption is mandatory for modern web security and user trust.
10.1. Install Certbot
sudo apt install certbot python3-certbot-nginx -y
10.2. Obtain SSL Certificate
Run Certbot for Nginx. Replace your_domain:
sudo certbot --nginx -d your_domain -d www.your_domain
Follow the prompts: enter an email, agree to terms, and decide on HTTP to HTTPS redirection. Certbot automatically modifies your Nginx configuration.
10.3. Verify Auto-Renewal
Test the renewal process:
sudo certbot renew --dry-run
Critical Insight: Let’s Encrypt certificates are valid for 90 days. Certbot automatically creates a systemd timer or cron job for renewal, but verifying it works is crucial to prevent certificate expiry and service interruption.
11. Install MySQL 8 on Ubuntu 24.04
MySQL is a popular open-source relational database management system.
11.1. Install MySQL Server
sudo apt update
sudo apt install mysql-server -y
11.2. Run Security Script
Enhance MySQL security:
sudo mysql_secure_installation
Follow the prompts: set a strong root password, remove anonymous users, disallow remote root login, remove test databases, and reload privilege tables. This script is vital for securing your database instance.
11.3. Test MySQL Login
sudo mysql -u root -p
Enter your root password. Type exit; to leave the prompt.
12. Install MariaDB Database on Ubuntu 24.04
MariaDB is a community-developed fork of MySQL, offering similar functionality and often enhanced performance.
12.1. Install MariaDB Server
sudo apt update
sudo apt install mariadb-server -y
12.2. Run Security Script
Secure your MariaDB installation:
sudo mysql_secure_installation
The process is identical to MySQL’s security script, focusing on setting a root password and hardening access.
12.3. Test MariaDB Login
sudo mariadb -u root -p
Enter your root password. Type exit; to leave the prompt.
13. Install PostgreSQL on Ubuntu 24.04
PostgreSQL is a powerful, open-source object-relational database system known for its reliability, feature robustness, and performance.
13.1. Install PostgreSQL
sudo apt update
sudo apt install postgresql postgresql-contrib -y
13.2. Access PostgreSQL Prompt
Switch to the postgres user (default admin user) and access the PostgreSQL prompt:
sudo -i -u postgres
psql
Type q to exit the prompt.
13.3. Set Password for Postgres User
While in the psql prompt, set a password for the postgres user:
ALTER USER postgres WITH PASSWORD 'your_strong_password';
Warning: PostgreSQL’s authentication is nuanced. For remote access, you’ll need to configure pg_hba.conf and postgresql.conf, a task beyond a basic installation guide but crucial for production deployments.
14. Install Redis Server on Ubuntu 24.04
Redis is an in-memory data structure store, used as a database, cache, and message broker. It excels in performance-critical applications.
14.1. Install Redis
sudo apt update
sudo apt install redis-server -y
14.2. Verify Redis Status
sudo systemctl status redis-server
It should be active (running).
14.3. Test Redis Functionality
Access the Redis CLI and perform a simple ping:
redis-cli
ping
exit
You should receive a PONG response.
Pro-tip: For production, review /etc/redis/redis.conf to set a strong password (requirepass), bind to specific interfaces, and configure persistence options.
15. 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.
15.1. Add Ondrej PHP Repository
Ubuntu’s default repositories might not have the latest PHP versions. Add the Ondrej PPA:
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
15.2. Install PHP 8.3 and FPM (for Nginx)
sudo apt install php8.3 php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip -y
Start and enable PHP-FPM:
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
Nginx Integration: Modify your Nginx server block (e.g., /etc/nginx/sites-available/your_domain) to process PHP files:
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Then sudo nginx -t and sudo systemctl reload nginx.
15.3. Install PHP 8.3 and Apache Module (for Apache)
sudo apt install php8.3 libapache2-mod-php8.3 php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip -y
Apache’s PHP module is automatically enabled upon installation. If not, enable it:
sudo a2enmod php8.3
Restart Apache:
sudo systemctl restart apache2
Critical Note: Ensure you install the correct PHP FPM package for Nginx (php8.3-fpm) or the Apache module (libapache2-mod-php8.3), not both if you only use one web server.
16. Install Composer (PHP Dependency Manager) on Ubuntu 24.04
Composer is an indispensable tool for managing PHP project dependencies.
16.1. Download and Install Composer
cd ~/
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php
16.2. Verify Installation
composer -V
You should see the Composer version number.
Use Case: Composer simplifies the management of external libraries, ensuring consistent environments across development and production.
17. Install Node.js and npm on Ubuntu 24.04
Node.js is a JavaScript runtime, and npm is its package manager, crucial for modern front-end and full-stack development.
17.1. Install Node.js and npm (from NodeSource)
Using NodeSource’s PPA provides the latest stable versions:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y
This command installs both Node.js and npm.
17.2. Verify Installation
node -v
npm -v
Pro-tip: For managing multiple Node.js versions, consider using nvm (Node Version Manager).
18. Install Python 3 and pip on Ubuntu 24.04
Python 3 is pre-installed on Ubuntu 24.04, but pip (Python’s package installer) often needs to be installed separately.
18.1. Verify Python 3
python3 --version
18.2. Install pip for Python 3
sudo apt update
sudo apt install python3-pip -y
18.3. Verify pip
pip3 --version
Warning: Avoid using pip directly for system-wide package installations as it can conflict with system packages. Prefer virtual environments (python3 -m venv myenv) for project-specific dependencies.
19. Install Java (OpenJDK) on Ubuntu 24.04
OpenJDK is the open-source implementation of the Java Platform, Standard Edition (Java SE).
19.1. Install OpenJDK 17 (LTS)
sudo apt update
sudo apt install openjdk-17-jdk -y
19.2. Verify Installation
java --version
javac --version
19.3. Set JAVA_HOME Environment Variable (Optional but Recommended)
Edit ~/.bashrc or /etc/environment:
sudo nano ~/.bashrc
Add the following line (adjust version if different):
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
Reload your shell:
source ~/.bashrc
Practical Tip: Setting JAVA_HOME is crucial for many Java-based applications and build tools to correctly locate the Java runtime.
20. Install Docker and Docker Compose on Ubuntu 24.04
Docker enables containerization, providing isolated and reproducible environments for applications. Docker Compose orchestrates multi-container applications.
20.1. Install Docker Engine
Add Docker’s official GPG key and repository:
sudo apt update
sudo apt install ca-certificates curl gnupg -y
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] 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
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
20.2. Add User to Docker Group
To run Docker commands without sudo, add your user to the docker group:
sudo usermod -aG docker $USER
Log out and log back in (or run newgrp docker) for the changes to take effect.
20.3. Verify Docker Installation
docker run hello-world
This command downloads and runs a test image, confirming Docker is operational.
20.4. Verify Docker Compose Plugin
docker compose version
Critical Insight: The Docker installation process is more involved due to adding external repositories. Following these precise steps ensures you install the official and latest Docker versions, avoiding potential issues with outdated packages from Ubuntu’s default repositories.
With these foundational services and tools established, your Ubuntu 24.04 server is now prepared for advanced deployments. Proceed to configure your web applications, databases, and development environments as required for your specific projects, leveraging the robust infrastructure you’ve just built.
