DEV Community

Cover image for Installing WordPress on an EC2 Instance with LAMP Stack (Linux, Apache, MySQL, PHP 8)
Victor Okonkwo
Victor Okonkwo

Posted on

Installing WordPress on an EC2 Instance with LAMP Stack (Linux, Apache, MySQL, PHP 8)

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Update the System

Update the package list and upgrade existing packages:

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Apache (Web Server)

Install Apache:

sudo apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Start and enable Apache to run on system boot:

sudo systemctl start apache2
sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

Step 4: Install MySQL (Database Server)

Install MySQL:

sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

Start and enable MySQL:

sudo systemctl start mysql
sudo systemctl enable mysql
Enter fullscreen mode Exit fullscreen mode

Secure MySQL installation:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to set the root password and secure the database.

Create a database and user for WordPress:

sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Restart Apache to load the PHP 8 module:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Confirm or set the DocumentRoot directive:

DocumentRoot /var/www/html
Enter fullscreen mode Exit fullscreen mode

Ensure the following line is present to process PHP files:

AddHandler application/x-httpd-php .php
Enter fullscreen mode Exit fullscreen mode

Save the file and reload Apache:

sudo systemctl reload apache2   # Ubuntu
sudo systemctl reload httpd     # Amazon Linux 2
Enter fullscreen mode Exit fullscreen mode

Step 8: Download and Configure WordPress

Download the latest WordPress:

wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
Enter fullscreen mode Exit fullscreen mode

Move WordPress files to /var/www/html:

sudo mv wordpress/* /var/www/html/
Enter fullscreen mode Exit fullscreen mode

Set permissions for WordPress files:

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
Enter fullscreen mode Exit fullscreen mode

Rename the sample configuration file:

sudo mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
Enter fullscreen mode Exit fullscreen mode

Edit the wp-config.php file:

sudo nano /var/www/html/wp-config.php
Enter fullscreen mode Exit fullscreen mode

Update the database settings:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
Enter fullscreen mode Exit fullscreen mode

Step 9: Finalize Installation

Restart Apache:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

MySQL logs:

sudo tail -f /var/log/mysql/error.log
Enter fullscreen mode Exit fullscreen mode

Top comments (0)