LAMP Stack Setup
Linux, Apache, MySQL, PHP installation and configuration.
Quick Install
# All in one
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql -yStep 1: Install Apache
# Install
sudo apt install apache2 -y
# Start and enable
sudo systemctl start apache2
sudo systemctl enable apache2
# Verify
curl http://localhostApache Configuration
# Main config
sudo nano /etc/apache2/apache2.conf
# Disable directory listing
<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
# Enable mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2Step 2: Install MySQL
# Install
sudo apt install mysql-server -y
# Secure installation
sudo mysql_secure_installation
# Answer prompts:
# - Set root password: Y
# - Remove anonymous users: Y
# - Disallow root login remotely: Y
# - Remove test database: Y
# - Reload privileges: YMySQL Configuration
# Login as root
sudo mysql
# Create database and user
CREATE DATABASE myapp;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;Step 3: Install PHP
# Install PHP with common extensions
sudo apt install php libapache2-mod-php php-mysql \
php-curl php-gd php-mbstring php-xml php-zip -y
# Verify
php -vPHP Configuration
# Edit php.ini
sudo nano /etc/php/8.1/apache2/php.ini
# Recommended settings
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256MStep 4: Create Virtual Host
# Create directory
sudo mkdir -p /var/www/mysite
# Set permissions
sudo chown -R $USER:www-data /var/www/mysite
sudo chmod -R 755 /var/www/mysite
# Create virtual host
sudo nano /etc/apache2/sites-available/mysite.confVirtual Host Configuration
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
</VirtualHost># Enable site
sudo a2ensite mysite.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2Step 5: Test PHP
# Create test file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/mysite/info.php
# Visit http://your-server/info.php
# Remove after testing!
sudo rm /var/www/mysite/info.phpSSL with Let's Encrypt
# Install Certbot
sudo apt install certbot python3-certbot-apache -y
# Get certificate
sudo certbot --apache -d mysite.com -d www.mysite.com
# Auto-renewal test
sudo certbot renew --dry-runFirewall Rules
sudo ufw allow 'Apache Full'
sudo ufw statusVerification
| Check | Command |
|---|---|
| Apache running | sudo systemctl status apache2 |
| MySQL running | sudo systemctl status mysql |
| PHP version | php -v |
| Open ports | ss -tuln | grep -E ':80|:443' |
- lamp
- apache
- mysql
- php
- web server