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 -y
Step 2: Install Apache
Install Apache using the following command:
sudo apt install apache2 -y
Once installed, start Apache and enable it to start automatically on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 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 -y
Configure PHP to use the Apache module:
sudo a2enmod php8.4
Restart Apache to apply changes:
sudo systemctl restart apache2
Step 4: Configure PHP
Edit the PHP configuration file to set memory limits and other settings:
sudo nano /etc/php/8.4/apache2/php.ini
Update the following settings:
memory_limit = 256M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
Save and close the file. Restart Apache to apply changes:
sudo systemctl restart apache2
Step 5: Install MySQL 8
Install MySQL 8:
sudo apt install mysql-server -y
Secure the MySQL installation by running the following command:
sudo mysql_secure_installation
Follow 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 -p
Enter 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.conf
Add 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 apache2
Step 8: Configure mod_rewrite
Enable mod_rewrite:
sudo a2enmod rewrite
Restart Apache to apply changes:
sudo systemctl restart apache2
Create a new file in the /var/www/example.com
directory:
sudo nano /var/www/example.com/.htaccess
Add 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 configtest
If there are no errors, reload Apache:
sudo systemctl reload apache2
Step 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.php
Add the following code:
<?php
$servername = "localhost";
$username = "admin";
$password = "strong_password";
$dbname = "example";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection