Installing WordPress on an EC2 Instance with LAMP Stack (Linux, Apache, MySQL, PHP 8)
Victor Okonkwo
Posted on November 24, 2024
This documentation provides a detailed guide for setting up WordPress on an EC2 instance using the LAMP stack—Linux, Apache, MySQL, and PHP.
Prerequisites
- An active EC2 instance (Amazon Linux 2 or Ubuntu).
- Security group configured to allow inbound HTTP (port 80) and SSH (port 22) traffic.
- SSH access to the EC2 instance.
Step 1: Connect to Your EC2 Instance
Use SSH to connect to your instance:
ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
Step 2: Update the System
Update the package list and upgrade existing packages:
sudo apt update && sudo apt upgrade -y
Step 3: Install Apache (Web Server)
Install Apache:
sudo apt install apache2 -y
Start and enable Apache to run on system boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 4: Install MySQL (Database Server)
Install MySQL:
sudo apt install mysql-server -y
Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysql
Secure MySQL installation:
sudo mysql_secure_installation
Follow the prompts to set the root password and secure the database.
Create a database and user for WordPress:
sudo mysql -u root -p
Run the following SQL commands:
CREATE DATABASE wordpress;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 5: Install PHP 8
Add the PHP 8 repository and install PHP 8:
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.0 php8.0-mysql -y
Restart Apache to load the PHP 8 module:
sudo systemctl restart apache2
Step 6: Remove the Default Apache Index File
Remove the default index file to allow WordPress to load:
sudo rm /var/www/html/index.html
Step 7: Adjust Apache Configuration
Ensure Apache is configured to serve files from /var/www/html
and process PHP files.
Open the Apache configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Confirm or set the DocumentRoot
directive:
DocumentRoot /var/www/html
Ensure the following line is present to process PHP files:
AddHandler application/x-httpd-php .php
Save the file and reload Apache:
sudo systemctl reload apache2 # Ubuntu
sudo systemctl reload httpd # Amazon Linux 2
Step 8: Download and Configure WordPress
Download the latest WordPress:
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
Move WordPress files to /var/www/html
:
sudo mv wordpress/* /var/www/html/
Set permissions for WordPress files:
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
Rename the sample configuration file:
sudo mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
Edit the wp-config.php
file:
sudo nano /var/www/html/wp-config.php
Update the database settings:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
Step 9: Finalize Installation
Restart Apache:
sudo systemctl restart apache2
Access your WordPress site:
Open your browser and navigate to http://your-ec2-public-ip
. Complete the WordPress setup through the web interface.
Step 10: Verify PHP
Create a PHP info file to ensure PHP 8 is properly installed:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Visit http://your-ec2-public-ip/phpinfo.php
. If you see the PHP info page, PHP 8 is successfully installed.
Remove the test file for security:
sudo rm /var/www/html/phpinfo.php
Conclusion
You have successfully set up WordPress on an EC2 instance using the LAMP stack (Linux, Apache, MySQL, PHP 8). Your site is now live and ready for further customization! If you encounter issues, check Apache and MySQL logs for troubleshooting:
Apache logs:
sudo tail -f /var/log/apache2/error.log
MySQL logs:
sudo tail -f /var/log/mysql/error.log
Posted on November 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.