LAMP Web Server Setup
On this page
Installing Apache, PHP 8.4, and MySQL 8 on Ubuntu Server
This guide provides a step-by-step walkthrough of installing Apache, PHP 8.4, and MySQL 8 on a new Ubuntu server. We will cover configuration, setup, and testing of each component.
Prerequisites
- A new Ubuntu server with a user account and sudo privileges
- Basic knowledge of bash and Linux commands
Step 1: Update and Upgrade Ubuntu
Before installing any software, update and upgrade your Ubuntu server:
sudo apt update
sudo apt upgrade -yStep 2: Install Apache
Install Apache using the following command:
sudo apt install apache2 -yOnce installed, start Apache and enable it to start automatically on boot:
sudo systemctl start apache2
sudo systemctl enable apache2Step 3: Install PHP 8.4
Install PHP 8.4 and required extensions:
sudo apt install php8.4 libapache2-mod-php8.4 php8.4-mysql php8.4-curl php8.4-gd php8.4-mbstring -yConfigure PHP to use the Apache module:
sudo a2enmod php8.4Restart Apache to apply changes:
sudo systemctl restart apache2Step 4: Configure PHP
Edit the PHP configuration file to set memory limits and other settings:
sudo nano /etc/php/8.4/apache2/php.iniUpdate the following settings:
memory_limit = 256M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300Save and close the file. Restart Apache to apply changes:
sudo systemctl restart apache2Step 5: Install MySQL 8
Install MySQL 8:
sudo apt install mysql-server -ySecure the MySQL installation by running the following command:
sudo mysql_secure_installationFollow the prompts to set a root password, remove anonymous users, and restrict root access.
Step 6: Configure MySQL
Log in to MySQL as the root user:
sudo mysql -u root -pEnter the root password you set earlier. Create a new admin user for daily administrative use:
CREATE USER 'admin'@'%' IDENTIFIED BY'strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%';
FLUSH PRIVILEGES;Replace strong_password with a secure password.
Step 7: Configure Apache Virtual Hosts
Create a new virtual host file:
sudo nano /etc/apache2/sites-available/example.com.confAdd the following configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>Replace example.com with your domain name. Save and close the file.
Enable the new virtual host and restart Apache:
sudo a2ensite example.com.conf
sudo systemctl restart apache2Step 8: Configure mod_rewrite
Enable mod_rewrite:
sudo a2enmod rewriteRestart Apache to apply changes:
sudo systemctl restart apache2Create a new file in the /var/www/example.com directory:
sudo nano /var/www/example.com/.htaccessAdd the following configuration:
RewriteEngine On
RewriteRule ^index\.html$ index.php [L]Save and close the file.
Step 9: Test Apache Configuration
Test the Apache configuration before reloading:
sudo apache2ctl configtestIf there are no errors, reload Apache:
sudo systemctl reload apache2Step 10: Create a Test PHP File
Create a new PHP file in the /var/www/example.com directory:
sudo nano /var/www/example.com/index.phpAdd the following code:
<?php
$servername = "localhost";
$username = "admin";
$password = "strong_password";
$dbname = "example";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if(!$conn){
echo "failed to connect to database \n";
}else{
echo "successfully connected to database \n";
}Now you should be able to use a web browser to access http://example.com/index.php and see whether PHP can execute code to connect to your MySQL database. But wait… because you don’t actually own example.com you’ll need to setup a hosts file entry to direct this domain to your local loopback IP address 127.0.0.1 . You can also execute the PHP from the command line via:
php /var/www/example.com/index.phpwhich assuming a connection can be made should print to the terminal successfully connected to database.