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
sudoprivileges. 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.
- Add a new user:
sudo adduser your_username
Follow the prompts to set a strong password and user information. - Add the user to the
sudogroup:
sudo usermod -aG sudo your_username
This grants your user the ability to execute commands with root privileges usingsudo. - Verify sudo access:
Log out and log back in as the new user, then run:sudo whoami. It should returnroot.
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.
- 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. - 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.
- Set the new hostname:
sudo hostnamectl set-hostname your_new_hostname - Edit the
/etc/hostsfile:
sudo nano /etc/hosts
Ensure the line127.0.0.1 localhostis followed by127.0.1.1 your_new_hostname. - 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.
- Verify systemd-timesyncd status:
timedatectl status
Ubuntu 24.04 typically usessystemd-timesyncdby default. - 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.
- Update package lists:
sudo apt update - Install Nginx:
sudo apt install nginx -y - 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 - 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.
- Update package lists:
sudo apt update - Install Apache:
sudo apt install apache2 -y - Adjust the firewall (UFW):
sudo ufw allow 'Apache'(for HTTP and HTTPS)
sudo ufw reload - 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.
- Create a directory for your domain:
sudo mkdir -p /var/www/your_domain/html - Create a sample
index.htmlfile:
echo '<h1>Hello from your_domain!</h1>' | sudo tee /var/www/your_domain/html/index.html - Set correct permissions:
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain - Create the server block configuration file:
sudo nano /etc/nginx/sites-available/your_domain
Paste the following, replacingyour_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; } } - Enable the server block by creating a symlink:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/ - Test Nginx configuration for syntax errors:
sudo nginx -t - 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.
- Create a directory for your domain:
sudo mkdir -p /var/www/your_domain/html - Create a sample
index.htmlfile:
echo '<h1>Hello from your_domain!</h1>' | sudo tee /var/www/your_domain/html/index.html - Set correct permissions:
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain - Create the virtual host configuration file:
sudo nano /etc/apache2/sites-available/your_domain.conf
Paste the following, replacingyour_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> - Enable the virtual host:
sudo a2ensite your_domain.conf - Disable the default virtual host (optional, but recommended for clean setups):
sudo a2dissite 000-default.conf - Test Apache configuration:
sudo apache2ctl configtest - 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.
- Install Certbot and its Nginx plugin:
sudo apt install certbot python3-certbot-nginx -y - Obtain and install SSL certificate:
sudo certbot --nginx -d your_domain -d www.your_domain
Follow the interactive prompts (email, terms of service, redirection). - Verify automatic renewal:
sudo systemctl status certbot.timer
Certbot automatically sets up a cron job or systemd timer for renewals. - 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.
- Install MySQL Server:
sudo apt install mysql-server -y - 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. - 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.
- Install MariaDB Server:
sudo apt install mariadb-server -y - Run the security script:
sudo mysql_secure_installation
Similar to MySQL, follow the prompts for security hardening. - 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.
- Install PostgreSQL:
sudo apt install postgresql postgresql-contrib -y - Switch to the
postgresuser:
sudo -i -u postgres - Access the PostgreSQL prompt:
psql - Set a password for the
postgresuser (optional, but recommended):
password postgres
Enter and confirm a strong password. - Exit PostgreSQL and the
postgresuser:
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.
- Install Redis:
sudo apt install redis-server -y - Verify Redis status:
systemctl status redis-server - 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.
- 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 - 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, installlibapache2-mod-php8.3instead of or in addition tophp8.3-fpm. - Verify PHP-FPM status (for Nginx):
systemctl status php8.3-fpm - 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 thelocation ~ .php$block to point to/run/php/php8.3-fpm.sock. - 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.
- Download Composer installer:
curl -sS https://getcomposer.org/installer | php - Move Composer to a global executable path:
sudo mv composer.phar /usr/local/bin/composer - 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.
- Install
curlandgit(if not already present):
sudo apt install curl git -y - 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). - Install the latest LTS version of Node.js:
nvm install --lts - 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.
- Verify Python 3 installation:
python3 --version - Install pip for Python 3:
sudo apt install python3-pip -y - 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.
- Install the default JRE (Java Runtime Environment):
sudo apt install default-jre -y - Install the default JDK (Java Development Kit) for development:
sudo apt install default-jdk -y - 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.
- Install Git:
sudo apt install git -y - Configure Git with your identity:
git config --global user.name "Your Name"
git config --global user.email "[email protected]" - 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.
- Update package lists and install dependencies:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y - 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 - 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 - Install Docker Engine:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y - Add your user to the
dockergroup:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect. - Verify Docker installation:
docker run hello-world(should download and run a test container) - 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 - 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.
