Deploy WordPress to a Ubuntu

distribution on Amazon EC2

In this guide, we’ll walk through setting up a LAMP (Linux, Apache, MySQL, PHP) stack on an Ubuntu server. This stack is a popular choice for hosting dynamic websites and applications like WordPress. Each command in this tutorial ensures your server is ready for a secure and optimized WordPress installation.

1. Update and Upgrade the Server

sudo apt update
sudo apt upgrade -y

Starting with updating your system ensures all packages are current. Running sudo apt update fetches the list of available updates, and sudo apt upgrade -y installs them automatically.

2. Install Apache

sudo apt install apache2 -y

This command installs the Apache web server, the backbone of your LAMP stack. Apache will serve the content to users who access your website.

3. Start and Enable Apache

sudo systemctl start apache2
sudo systemctl enable apache2

Here, sudo systemctl start apache2 initiates the Apache service, and sudo systemctl enable apache2 configures it to start automatically on boot, ensuring Apache is always available after server restarts.

4. Install MySQL

sudo apt install mysql-server -y

MySQL is a powerful database management system, essential for storing WordPress data, including posts, user information, and settings.

5. Secure the MySQL Installation

sudo mysql_secure_installation

This command guides you through securing MySQL by setting a root password, removing anonymous users, disallowing root login remotely, and removing test databases. Following these steps increases database security.

6. Install PHP and Required Extensions

sudo apt install php php-mysql libapache2-mod-php php-xml php-gd php-curl -y

PHP is the programming language used by WordPress. Here, we install PHP along with essential extensions:

  • php-mysql: Allows PHP to communicate with MySQL.
  • libapache2-mod-php: Integrates PHP with Apache.
  • php-xml, php-gd, php-curl: Enable various functionalities, like XML handling, image processing, and making HTTP requests.

7. Restart Apache to Apply PHP Configuration

sudo systemctl restart apache2

Restarting Apache ensures it recognizes the new PHP configurations, enabling it to handle PHP files effectively.

8. Create a MySQL Database and User for WordPress

sudo mysql -u root -p

This command logs you into MySQL as the root user.

a) Create the WordPress Database

CREATE DATABASE wordpress_db;

This SQL command creates a new database called wordpress_db, where WordPress will store its data.

b) Create a WordPress User

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_password';

We create a dedicated MySQL user wp_user with a password (your_password) for WordPress. This approach follows security best practices by avoiding the use of the root user.

c) Grant Privileges to the WordPress User

GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;

This grants the wp_user full access to wordpress_db, and FLUSH PRIVILEGES; reloads the permission settings immediately.

d) Exit MySQL

EXIT;

9. Navigate to the Web Root

cd /var/www/html

Finally, we move to Apache’s default web directory, /var/www/html. This is where your WordPress files will go, making them accessible through your domain.

With the LAMP stack in place, it’s time to download, configure, and finalize your WordPress setup.

1. Download the Latest WordPress Package

sudo wget https://wordpress.org/latest.tar.gz

The wget command downloads the latest version of WordPress from the official site. The .tar.gz file format compresses the files for faster download.

2. Extract the WordPress Archive

sudo tar -xzvf latest.tar.gz

This command extracts the WordPress files from the compressed archive (latest.tar.gz). The -xzvf options tell tar to extract (x), decompress (z), output file names (v), and specify the file (f).

3. Move WordPress Files to the Web Root

sudo mv wordpress/* .

After extraction, move the WordPress files to the root directory /var/www/html (represented by .). This step makes WordPress directly accessible at your domain root.

4. Remove Unnecessary Files

sudo rm -rf wordpress latest.tar.gz

After moving the files, this command deletes the now-empty wordpress directory and the downloaded archive, cleaning up unnecessary files from your server.

5. Configure WordPress

a) Copy the Sample Configuration File

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

WordPress provides a sample configuration file (wp-config-sample.php). Copy it to wp-config.php, the main configuration file for WordPress, where database settings and other critical configurations will be defined.

b) Open wp-config.php for Editing

sudo nano wp-config.php

Using nano (a text editor), open the configuration file to input your database details.

c) Define the Database Credentials

Within wp-config.php, locate and set the following lines:

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

These entries connect WordPress to the wordpress_db database using the wp_user credentials you set up in MySQL.

6. Set Proper Permissions for WordPress Files

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

To allow Apache to access WordPress files, www-data (the default Apache user) is set as the owner of all files and folders in /var/www/html. Changing permissions to 755 allows read, write, and execute access for the owner and restricts write permissions for group and others, enhancing security.

7. Remove the Default Apache Page

sudo rm /var/www/html/index.html

Apache’s default page, index.html, could interfere with WordPress. Deleting it ensures that WordPress’s index page will load as the homepage.

With these final steps, your WordPress site is ready! You should now be able to access your site by navigating to your server’s IP address or domain name in a browser, where you’ll be prompted to complete the WordPress setup.


Comments

Leave a Reply

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