In this guide, we will walk you through the process of installing and configuring Apache, MySQL, and PHP-FPM on Ubuntu 24.04. This guide is written in simple English and designed for beginners. We will also cover how to secure your Apache server to ensure your system is safe.

Step 1: Update Your System

Before we begin, it’s always a good practice to update your system to ensure you have the latest software versions and security patches.

  1. Open your terminal.
  2. Run the following commands to update your package list:
  3. sudo apt update
    sudo apt upgrade
    

Step 2: Install Apache

Apache is one of the most widely used web servers. To install Apache, follow these steps:

  1. In the terminal, run the following command:
    sudo apt install apache2
    
  2. After installation, start Apache with the following command:
    sudo systemctl start apache2
    
  3. Enable Apache to start automatically at boot:
    sudo systemctl enable apache2
    
  4. To check if Apache is running, open your browser and type your server’s IP address. You should see the Apache default welcome page. For example: http://your_server_ip/
How to Install and Configure Apache, MySQL, and PHP-FPM on Ubuntu 24.04 Apache General Articles PHP php-fpm Ubuntu 24.04
Apache default page

Step 3: Secure Apache

Securing Apache is important to protect your server from attacks. Here’s how to secure Apache:

  1. Disable directory listing by editing the Apache configuration file:
    sudo nano /etc/apache2/apache2.conf
    
  2. Scroll down to the section and change the Options line to:
    
    Options -Indexes  FollowSymLinks
    
    
  3. Save and exit by pressing CTRL X, then Y, and Enter.
  4. Restart Apache to apply changes:
    sudo systemctl restart apache2
    
  5. To further secure your Apache installation, use the following command to hide version information:
    sudo nano /etc/apache2/conf-available/security.conf
    
  6. Set the following options:
    
    ServerTokens Prod
    ServerSignature Off
    
    
  7. Save and exit the file, then restart Apache:
    sudo systemctl restart apache2
    

Step 4: Install MySQL

MySQL is the database that will store your website’s data. To install MySQL, follow these steps:

  1. Install MySQL server by running the following command:
    sudo apt install mysql-server
    
  2. Once installed, secure your MySQL installation:
    sudo mysql_secure_installation
    
    • You will be prompted to set up a root password (choose a strong password).
    • Follow the prompts to remove anonymous users, disable remote root login, and remove test databases.
  3. After securing MySQL, log in to the MySQL root account to test the installation:
    sudo mysql -u root -p
    
  4. Exit MySQL by typing:
    exit;
    

Step 5: Install PHP and PHP-FPM

PHP-FPM (FastCGI Process Manager) improves the performance of PHP scripts when used with Apache. To install PHP and PHP-FPM using a PPA (Personal Package Archive), follow these steps:

  1. Add the PPA for PHP:
    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
  2. Install PHP along with PHP-FPM:
    sudo apt install php8.3 php8.3-fpm libapache2-mod-fcgid
    

    (At the time of writing, PHP 8.3 is the latest version, but you can replace it with any newer version if necessary.)

  3. Enable the Apache modules for PHP-FPM:
    sudo a2enmod proxy_fcgi setenvif
    sudo a2enconf php8.3-fpm
    
  4. Restart Apache to enable PHP-FPM:
    sudo systemctl restart apache2
    

Step 6: Test PHP with Apache

To check if PHP is working correctly with Apache, follow these steps:

  1. Create a simple PHP test file:
    sudo nano /var/www/html/info.php
    
  2. Add the following code to the file:
    
    
    
    
  3. Save and exit by pressing CTRL X, then Y, and Enter.
  4. Now, in your web browser, visit: http://your_server_ip/info.php
  5. How to Install and Configure Apache, MySQL, and PHP-FPM on Ubuntu 24.04 Apache General Articles PHP php-fpm Ubuntu 24.04
    PHP info function
  6. After confirming PHP is working, remove the test file to avoid exposing sensitive information:
    sudo rm /var/www/html/info.php
    

Step 7: Configure MySQL Database for Your Website

  1. Log in to MySQL as the root user:
    sudo mysql -u root -p
    
  2. Create a new database:
    CREATE DATABASE your_database_name;
    
  3. Create a new MySQL user and grant them privileges:
    
    CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
    FLUSH PRIVILEGES;
    
    
    
  4. Exit MySQL:
    
    EXIT;
    
    

Step 8: Enable UFW Firewall and Allow Necessary Ports

To secure your server, we recommend enabling UFW (Uncomplicated Firewall) and allowing essential services.

  1. Enable UFW:
    sudo ufw enable
    
  2. Allow OpenSSH (for remote access) and HTTP/HTTPS traffic:
    sudo ufw allow OpenSSH
    sudo ufw allow 'Apache Full'
    
  3. Check UFW status:
    sudo ufw status
    

Step 9: Final Apache Security Settings (Optional but Recommended)

For added security, consider enabling the following Apache modules and settings:

  1. ModSecurity: A web application firewall for added protection.
    sudo apt install libapache2-mod-security2
    sudo a2enmod security2
    sudo systemctl restart apache2
    
  2. SSL (for HTTPS): You can also secure your site with SSL using a free certificate from Let’s Encrypt.
    sudo apt install certbot python3-certbot-apache
    sudo certbot --apache
    

    Follow the prompts to install the SSL certificate.

Conclusion

Congratulations! You have successfully installed and configured Apache, MySQL, and PHP-FPM on your Ubuntu 24.04 server. This setup will allow you to host dynamic websites and applications with improved performance and security.

By following the additional security measures, you can also ensure that your server is protected from common threats. If you have any issues or need more advanced configurations, feel free to explore the documentation for each software or consult community forums for support.