Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

LAMP Stack (Linux, Apache, MySQL, PHP) Website Setup

The LAMP stack is a popular web server configuration that includes Linux, Apache, MySQL (or MariaDB), and PHP. This stack is widely used for hosting dynamic websites and web applications.

Installing the LAMP Stack

Debian/Ubuntu
# Update package index
sudo apt update

# Install Apache
sudo apt install apache2

# Install MariaDB (MySQL alternative)
sudo apt install mariadb-server

# Install PHP and common modules
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip

# Enable Apache modules
sudo a2enmod rewrite
sudo systemctl restart apache2

# Secure MariaDB installation
sudo mysql_secure_installation

# Start and enable services
sudo systemctl enable apache2
sudo systemctl enable mariadb
sudo systemctl start apache2
sudo systemctl start mariadb
Fedora/RHEL
# Update system
sudo dnf update

# Install Apache
sudo dnf install httpd

# Install MariaDB
sudo dnf install mariadb-server

# Install PHP and modules
sudo dnf install php php-mysqlnd php-cli php-curl php-gd php-mbstring php-xml php-zip

# Start and enable services
sudo systemctl enable httpd
sudo systemctl enable mariadb
sudo systemctl start httpd
sudo systemctl start mariadb

# Secure MariaDB installation
sudo mysql_secure_installation

# Configure firewall
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Arch Linux
# Update system
sudo pacman -Syu

# Install Apache
sudo pacman -S apache

# Install MariaDB
sudo pacman -S mariadb

# Install PHP and modules
sudo pacman -S php php-apache php-mysql php-gd php-curl

# Initialize MariaDB
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

# Configure PHP with Apache
sudo nano /etc/httpd/conf/httpd.conf
# Add these lines:
# LoadModule php_module modules/libphp.so
# AddHandler php-script .php
# Include conf/extra/php_module.conf

# Start and enable services
sudo systemctl enable httpd
sudo systemctl enable mariadb
sudo systemctl start httpd
sudo systemctl start mariadb

# Secure MariaDB installation
sudo mysql_secure_installation

Testing the LAMP Stack

Test PHP installation

echo "" | sudo tee /var/www/html/info.php

Create a test database (optional)
sudo mysql -u root -p
# CREATE DATABASE testdb;
# CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
# GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
# FLUSH PRIVILEGES;
# EXIT;

Set proper permissions

# Set ownership of the web directory
sudo chown -R www-data:www-data /var/www/html  # Debian/Ubuntu
sudo chown -R apache:apache /var/www/html      # Fedora/Arch
Access the test page

Open a web browser and navigate to http://your_server_ip/info.php. You should see the PHP information page, confirming that PHP is working correctly with Apache.

Remove the test page
sudo rm /var/www/html/info.php
Enable HTTPS in Apache

To enable HTTPS, you need to install an SSL certificate. You can use Let's Encrypt for free SSL certificates.

Install Certbot
# Debian/Ubuntu
sudo apt install certbot python3-certbot-apache
# Fedora/RHEL
sudo dnf install certbot python3-certbot-apache
# Arch Linux
sudo pacman -S certbot certbot-apache
Obtain and Install SSL Certificate
sudo certbot --apache -d your_domain.com -d www.your_domain.com

This command will automatically configure Apache to use the SSL certificate. Follow the prompts to complete the installation.

Verify SSL Configuration

After obtaining the SSL certificate, you can verify the configuration by accessing your website using https://your_domain.com. You should see a secure connection with a padlock icon in the browser's address bar.

Renew SSL Certificate

Let's Encrypt certificates are valid for 90 days. To renew the certificate automatically, you can set up a cron job or use the systemd timer.

# Test renewal process
sudo certbot renew --dry-run
Set up a cron job for automatic renewal
# Open the crontab editor
crontab -e
# Add the following line to run the renewal command daily
0 0 * * * /usr/bin/certbot renew --quiet

Finding IP Address and Hostname

To find your server's IP address and hostname, you can use the following commands:

# Find IP address
curl ifconfig.me  # Get the public IP address
curl ipinfo.io/ip  # Another way to get the public IP address
ip addr show  # Show all network interfaces and their IP addresses
ip a  # Shorter version of the above command
ip route get 1  # Get the default route and associated IP address
# Find private IP address
hostname -I  # Get the private IP address(es) of the server

Find hostname

hostname  # Get the hostname
hostname -f  # Get the fully qualified domain name (FQDN)
Last change: