Deploy WordPress to

an Amazon EC2 instance

Please refer to this article for getting your EC2 instance setup and follow the guide below the get your WordPress installation ready in no time

The first step is to ensure your system is up-to-date by installing the latest versions of the available packages. This command checks for package updates and applies them, making sure your environment is secure and equipped with recent bug fixes. The -y flag automatically confirms all updates.

Step 1: Update Packages

sudo yum update -y

Step 2: Install and Start Apache Web Server

sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

Apache is the software that will serve your website to visitors. In these commands:

  1. sudo yum install httpd -y: Installs Apache. Amazon Linux uses yum as its package manager, and httpd is the package name for Apache on Amazon Linux.
  2. sudo systemctl start httpd: Starts the Apache service, making it active on your server.
  3. sudo systemctl enable httpd: Enables Apache to start automatically when the server reboots, ensuring uptime.

After executing these commands, your EC2 instance should be ready to serve web content. You can check this by visiting the public IP address of your instance in a web browser; you should see Apache’s default welcome page.

Step 3: Install MariaDB (MySQL-Compatible Database Server)

sudo yum install mariadb105-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb

MariaDB is a popular MySQL-compatible database system. This series of commands will:

  1. sudo yum install mariadb105-server -y: Install MariaDB 10.5, a stable version compatible with most MySQL applications.
  2. sudo systemctl start mariadb: Starts the MariaDB service, making the database accessible to your applications.
  3. sudo systemctl enable mariadb: Ensures that MariaDB will start automatically on server reboots.

Once installed, MariaDB provides a database environment where you can store and manage data for dynamic websites and applications.

Step 4: Secure MariaDB Installation

sudo mysql_secure_installation

The final setup step is to secure your MariaDB installation. This interactive script will prompt you to configure important security settings for MariaDB:

  1. Set a root password if you haven’t already.
  2. Remove anonymous users to prevent unauthorized access.
  3. Disallow remote root login for added security.
  4. Remove the test database, which is created by default but isn’t secure for production environments.

Securing your MariaDB installation is crucial for protecting your data from unauthorized access and potential vulnerabilities.

After setting up Apache and MariaDB, the next step is to install PHP and essential extensions required by WordPress:

sudo yum install php php-mysqlnd php-gd php-xml -y

PHP is the scripting language used by WordPress, and these extensions add support for database connections, image processing, and XML parsing. The -y flag confirms installation for all packages.

Restart Apache to Load PHP

sudo systemctl restart httpd

After installing PHP, restart Apache to load the PHP module. This will enable Apache to interpret PHP files, which is crucial for running WordPress.


Now, we’ll create a new database and user for WordPress in MariaDB. This step is essential for separating WordPress data from other applications and securing it with unique credentials.

1. Access MariaDB:

    sudo mysql -u root -p

    Use the root user or your MariaDB administrator account. Enter the password when prompted.

    2. Create a Database and User:

    CREATE DATABASE wordpress_db;
    CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
    FLUSH PRIVILEGES;

    Replace wordpress_db, wp_user, and your_password with your chosen database name, username, and password. FLUSH PRIVILEGES; reloads the grant tables, ensuring that the new user permissions are active.

    3. Exit MariaDB:

    EXIT;

    You now have a secure, dedicated database ready for WordPress.


    Next we will download and configure WordPress

    1. Navigate to the Web Root Directory:

      cd /var/www/html

      2. Download and Extract WordPress:

      sudo wget https://wordpress.org/latest.tar.gz
      sudo tar -xzvf latest.tar.gz
      sudo mv wordpress/* .
      sudo rm -r wordpress latest.tar.gz

      These commands download the latest WordPress archive, extract it to the current directory, and clean up the leftover files. This places WordPress in your web root, ready for configuration.

      3. Configure WordPress Settings:

      sudo cp wp-config-sample.php wp-config.php
      sudo nano wp-config.php

      In the wp-config.php file, locate and update the following lines with your database details:

      define('DB_NAME', 'wordpress_db');
      define('DB_USER', 'wp_user');
      define('DB_PASSWORD', 'your_password');
      define('DB_HOST', 'localhost');

      Save and exit the editor to complete the database connection setup.


      After we have finished setting up WordPress we need to set file permissions for Apache

      sudo chown -R apache:apache /var/www/html
      sudo chmod -R 755 /var/www/html

      These commands grant Apache ownership of the WordPress files and set read/write/execute permissions for directories (755), ensuring WordPress functions correctly.


      We are almost finished now, the last thing we need to do is to ensure that AllowOverride All is enabled for the DocumentRoot or applicable directory in Apache’s configuration. This setting allows .htaccess to override directory permissions as needed.

      1. Allow Overrides in Apache Configuration:

      sudo nano /etc/httpd/conf/httpd.conf

      2. Restart Apache:

      sudo systemctl restart httpd

      Following these steps, your WordPress instance should be up and running on your Amazon Linux EC2 server, with a configured database, admin access, and working permalink structure.


      Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *