What you need before you start
Installing WordPress on Amazon Linux 2 means you will be working directly on your server using command-line tools instead of clicking through a web interface. You need an Amazon EC2 instance (a virtual server) already running Amazon Linux 2, SSH access to that instance, and a domain name pointed to your server's IP address. If you do not have these yet, set them up first — the installation itself assumes you can log in and run commands.
You will also need a text editor to modify configuration files. Nano is built into Amazon Linux 2 and works fine for this. The entire process takes about 30 minutes if you follow the steps in order and do not skip anything.
Key Takeaways
- Amazon Linux 2 requires you to install a web server (Apache), a database (MariaDB), and PHP before WordPress can run.
- You will read WordPress from wordpress.org, extract it to your web server's document root, and create a database and user for WordPress to store its data.
- The wp-config.php file connects WordPress to your database and must contain your database name, username, and password.
- After installation, you will log in through your domain name and run the WordPress setup wizard to create your first user account.
Install Apache, MariaDB, and PHP on your server
Log into your Amazon Linux 2 instance using SSH. Once you are connected, update the system package list by running sudo yum update -y. This ensures you have the latest security patches.
Next, install Apache (the web server), MariaDB (the database), and PHP (the language WordPress runs on) in one command: sudo yum install httpd mariadb-server php php-mysql -y. This will take a few minutes. When it finishes, start Apache and MariaDB and set them to start automatically when your server reboots:
- sudo systemctl start httpd
- sudo systemctl start mariadb
- sudo systemctl enable httpd
- sudo systemctl enable mariadb
Verify Apache is running by visiting your server's IP address in a web browser. You should see the Apache test page. If you do not, check that your security group allows inbound traffic on port 80 (HTTP).
Create a database and user for WordPress
WordPress stores all your posts, pages, and settings in a database. You need to create that database and a user account that WordPress will use to access it. Log into MariaDB by running sudo mysql -u root. You will see a MariaDB [(none)]> prompt.
Run these commands one at a time, pressing Enter after each. Replace wordpress_db with any name you want, and replace wp_user and your_password with a username and a strong password you choose:
- 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;
- EXIT;
Write down the database name, username, and password somewhere safe. You will need them in the next section.
read and extract WordPress
WordPress files need to live in Apache's document root, which is /var/www/html on Amazon Linux 2. Navigate there and read WordPress:
- cd /var/www/html
- sudo wget https://wordpress.org/latest.tar.gz
- sudo tar -xzf latest.tar.gz
This creates a folder called wordpress inside /var/www/html. If you want WordPress at your domain root (so visitors go to example.com instead of example.com/wordpress), move the files up one level:
- sudo mv wordpress/* .
- sudo rmdir wordpress
- sudo rm latest.tar.gz
Now set the correct ownership and permissions so Apache can read and write the files:
- sudo chown -R apache:apache /var/www/html
- sudo chmod -R 755 /var/www/html
Configure WordPress to connect to your database
WordPress reads its configuration from a file called wp-config.php. A sample file is included, so copy it and edit it with your database details:
- sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
- sudo nano /var/www/html/wp-config.php
Find these lines and replace the placeholder text with your actual database name, username, and password from the earlier step:
- define( 'DB_NAME', 'database_name_here' ); → change to your database name
- define( 'DB_USER', 'username_here' ); → change to your username
- define( 'DB_PASSWORD', 'password_here' ); → change to your password
Leave DB_HOST as localhost. When you are done editing, press Ctrl+O to save, then Ctrl+X to exit nano. Restart Apache so it picks up the changes: sudo systemctl restart httpd.
Run the WordPress setup wizard
Open a web browser and visit your domain name (or your server's IP address if you have not set up a domain yet). You will see the WordPress setup page asking for your site title, username, password, and email. Fill these in — this creates your first WordPress user account, which is different from the database user you created earlier.
After you submit the form, WordPress writes a few more settings to wp-config.php and sets up the database tables it needs. You will then see a login screen. Log in with the username and password you just created, and you will land in the WordPress dashboard.
find your installation
Before you start building your site, make a few security changes. First, delete the default WordPress files you do not need:
- sudo rm /var/www/html/wp-config-sample.php
- sudo rm /var/www/html/readme.html
Second, restrict write permissions on wp-config.php so only you can read it: sudo chmod 400 /var/www/html/wp-config.php. Third, change the permissions on the uploads folder so WordPress can write to it: sudo chmod 755 /var/www/html/wp-content/uploads.
Finally, set up HTTPS (encrypted traffic) using a free certificate from Let's Encrypt. Install Certbot and request a certificate:
- sudo yum install certbot python3-certbot-apache -y
- sudo certbot --apache -d your-domain.com
Replace your-domain.com with your actual domain. Certbot will ask for your email and whether you agree to the terms. It will then automatically configure Apache to use HTTPS and set up automatic renewal.
Frequently Asked Questions
What if I get a "connection refused" error when WordPress tries to connect to the database?
Check that MariaDB is running: sudo systemctl status mariadb. If it is not, start it with sudo systemctl start mariadb. Also verify that your database name, username, and password in wp-config.php match exactly what you created in MariaDB — even a single character difference will cause this error.
Can I install WordPress in a subfolder instead of at the domain root?
Yes. Instead of moving the WordPress files up one level, leave them in the wordpress folder. WordPress will then be at example.com/wordpress. You do not need to change anything else in the installation process.
How do I update WordPress after installation?
WordPress checks for updates automatically and shows a notification in the dashboard. Click the update button and WordPress updates itself. For plugins and themes, you update them the same way through the dashboard. Make sure your server has enough disk space before updating.
What if my domain name is not pointing to my server yet?
You can still test the installation by visiting your server's IP address in a browser. However, you will need to update your domain's DNS records to point to your server's IP before visitors can reach your site. Once DNS is updated, go to WordPress Settings and change the Site URL to your domain name.
Do I need to do anything special to back up my WordPress site?
Back up both your WordPress files and your database regularly. For files, use sudo tar -czf backup.tar.gz /var/www/html. For the database, use sudo mysqldump -u wp_user -p wordpress_db > backup.sql (it will prompt for your database password). Store these backups somewhere outside your server.