To host your WordPress website on AWS EC2 with a database, follow these steps:
1. Create an AWS EC2 Instance
- Log in to your AWS Management Console.
- Navigate to EC2 Dashboard and click on Launch Instance.
- Select the Amazon Linux 2 AMI or Ubuntu as your OS.
- Choose an instance type like t2.micro (free tier eligible).
- Configure the instance details and set the key pair for SSH access.
- Open port 80 (HTTP) and 443 (HTTPS) in the security group settings.
- Launch the instance and connect to it via SSH using your key pair.
2. Set Up LAMP Stack
On your EC2 instance, you’ll need to install the LAMP (Linux, Apache, MySQL, PHP) stack.
For Amazon Linux:
sudo yum update -y
sudo yum install -y httpd mariadb-server php php-mysqlnd
For Ubuntu:
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
Start the Apache and MySQL services:
sudo systemctl start httpd # for Amazon Linux
sudo systemctl start apache2 # for Ubuntu
sudo systemctl start mariadb
Enable services on boot:
sudo systemctl enable httpd mariadb # Amazon Linux
sudo systemctl enable apache2 mysql # Ubuntu
3. Secure MySQL and Create Database
Run the MySQL secure installation to set up a root password and remove insecure defaults:
sudo mysql_secure_installation
Log in to MySQL and create a database for WordPress:
sudo mysql -u root -p
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
4. Download and Configure WordPress
Navigate to the web root and download WordPress:
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rm latest.tar.gz
Set permissions:
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html
Configure the WordPress database settings:
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Update the following lines:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
5. Configure Apache
For Amazon Linux:
sudo nano /etc/httpd/conf/httpd.conf
For Ubuntu:
sudo nano /etc/apache2/sites-available/000-default.conf
Ensure the DocumentRoot
is set to /var/www/html
and restart Apache:
sudo systemctl restart httpd # for Amazon Linux
sudo systemctl restart apache2 # for Ubuntu
6. Configure Domain (Optional)
If you have a domain, you can point it to your EC2 instance’s Elastic IP.
- Allocate an Elastic IP from the Elastic IPs section of the EC2 dashboard and associate it with your instance.
- Update your domain’s DNS settings to point to this Elastic IP.
7. Complete WordPress Installation
In your browser, go to http://<your-ec2-public-ip>
or your domain name. This will bring up the WordPress installation screen.
- Follow the on-screen instructions to complete the installation by setting your site name, admin credentials, and other details.
8. Secure Your Website (Optional)
- Install SSL using Let’s Encrypt by following the Certbot instructions for Apache on your server OS.
- Set up firewall rules using AWS Security Groups to restrict access to only necessary ports.
After following these steps, your WordPress website should be hosted on AWS EC2 with a MySQL database, and you can manage it via the WordPress dashboard.
Leave a Reply