This guide provides step-by-step instructions for installing and configuring each component, with options for manual setup or an all-in-one script for quick installation
Contents:
Prerequisites:
- Ubuntu 24.04 Server
- User with Sudo Privileges
- Basic Command-Line Knowledge
- Domain Name (Optional). Having a registered domain name is optional but can simplify the Apache web server setup. If no domain is available, you can use the server’s IP address
1. Updating the System
Before starting the installation, it’s essential to ensure that your system’s package list and installed packages are up to date. This helps avoid any compatibility issues and ensures that you are installing the latest available versions of Apache, PHP, and MongoDB.
1.1. Update Package List:
Run the following command to update the package list. This retrieves the latest version information for the software packages from the repositories configured on your system:
sudo apt update
1.2. Upgrade Installed Packages:
Next, upgrade the installed packages on your system. This command ensures that all existing packages are updated to their latest stable versions, which is important for security and compatibility.
sudo apt upgrade
2. Installing Apache Web Server
The first step is to install Apache, which will serve as the foundation of your web server. Apache is a powerful, widely-used web server known for its stability and flexibility, making it an ideal choice for hosting your PHP application.
2.1. Install Apache:
Use the following command to install Apache. This will download and set up the latest version of Apache along with its dependencies
sudo apt install apache2
2.2. Start and Enable Apache service:
Once installed, start the Apache service and configure it to start automatically whenever the server reboots. This ensures that your web server remains active after a restart
sudo systemctl start apache2
sudo systemctl enable apache2
2.3. Verify Installation:
To confirm that Apache is running, you can check its status with:
sudo systemctl status apache2
2.4. Enabling Necessary Apache Headers and Modules:
After installing Apache, enable specific headers and modules required for enhanced performance, security, and flexibility of your web server. These modules allow Apache to handle requests more efficiently, manage headers, and support URL rewriting and SSL connections
sudo a2enmod headers
sudo a2enmod expires
sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2
3. Installing PHP
To enable Apache to process dynamic PHP scripts, we first need to install PHP. This involves adding a third-party repository to access the latest PHP versions. Run the following commands to install PHP:
3.1. Install prerequisites
Start by installing the software-properties package, which allows us to manage repositories:
sudo apt install software-properties-common
3.2. Add PHP Repository
Add the ppa:ondrej/php repository, known for providing up-to-date PHP packages:
sudo add-apt-repository ppa:ondrej/php
3.3. Install PHP
With the repository added, install PHP:
sudo apt install php
3.4. Integrating PHP with Apache
To make Apache handle PHP scripts, install the PHP module for Apache. This module links PHP and Apache, enabling Apache to interpret PHP files:
sudo apt install libapache2-mod-php
# Once installed, restart Apache to apply the changes
sudo systemctl restart apache2
3.5. Installing Essential PHP Extensions
To support common functionality in PHP applications, install several essential PHP extensions. These modules expand PHP's capabilities, allowing it to handle database interactions, XML parsing, image processing, and more:
sudo apt install php-cli
sudo apt install php-common
sudo apt install php8.3-mongodb
sudo apt install php-imap
sudo apt install php-mbstring
sudo apt install php-gd
sudo apt install php-curl
sudo apt install php-xml
sudo apt install php-cli unzip composer
4. Installing MongoDb
To set up MongoDB, we first need to add the official MongoDB repository to ensure we’re installing the latest version. This involves adding MongoDB’s GPG key, configuring the repository, and then installing MongoDB
4.1. Add MongoDB Repository Key
Add MongoDB’s public GPG key to authenticate packages:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -;
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
4.2. Add MongoDB Repository:
Add MongoDB Repository and update the package list:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update
4.3. Install MongoDB
Update the package list and install MongoDB:
sudo apt install mongodb-org
4.4. Enabling and Verifying MongoDB Service
Once MongoDB is installed, we’ll enable and start the MongoDB service to ensure it runs automatically at boot:
sudo systemctl enable mongod.service
sudo systemctl status mongod.service
5. Checking Performance
To verify that PHP is correctly installed and configured to work with Apache, you can use the phpinfo() function, which provides detailed information about PHP’s configuration, including loaded modules, server settings, and environment variables.
5.1. Create a PHP Info Page
Use the following command to create a info.php file in Apache’s default web directory. This file will contain the phpinfo() function, which outputs a summary of your PHP environment:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
5.2. Open the PHP Info Page in a Browser
Open a web browser and navigate to http://SERVER_IP/info.php (replace SERVER_IP with your server’s IP address). You should see a page displaying PHP’s configuration details. This confirms that Apache and PHP are working together as expected.
5.3. Security Note
Once verified, it’s recommended to delete the info.php file, as it exposes server information that could be useful to attackers
sudo rm /var/www/html/info.php
For the mega lazy😀
If you want to skip the manual steps, here’s a script that will automatically set up Apache, PHP, and MongoDB on your Ubuntu 20.04 server. This script will install all necessary packages, configure Apache and PHP, enable required modules, and start MongoDB—all in one go:
# General setup
sed -i "s/#\$nrconf{kernelhints} = -1;/\$nrconf{kernelhints} = -1;/g" /etc/needrestart/needrestart.conf;
sed -i "s/#\$nrconf{restart} = 'i';/\$nrconf{restart} = 'a';/g" /etc/needrestart/needrestart.conf;
sudo apt update -y && sudo apt upgrade -y;
# Installing Apache
sudo apt install apache2 -y;
sudo systemctl start apache2;
sudo systemctl enable apache2;
sudo a2enmod headers;
sudo a2enmod expires;
sudo a2enmod rewrite;
sudo a2enmod ssl;
rm -rf /var/www/html;
systemctl restart apache2;
# Installing PHP & Extensions
sudo apt install software-properties-common -y;
sudo add-apt-repository ppa:ondrej/php -y;
sudo apt install php -y;
sudo apt install libapache2-mod-php -y;
sudo apt install php-cli -y;
sudo apt install php-common -y;
sudo apt install php8.3-mongodb -y;
sudo apt install php-imap -y;
sudo apt install php-mbstring -y;
sudo apt install php-gd -y;
sudo apt install php-curl -y;
sudo apt install php-xml -y;
sudo apt install php-cli unzip composer -y;
# Installing MongoDb
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -;
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor --yes;
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list;
sudo apt update -y;
sudo apt install mongodb-org -y;
sudo systemctl enable mongod.service;