Ravindra BagaleCourses & study guides

Guides

How to Install WordPress on AWS EC2 (Amazon Linux 2023)

To install WordPress on AWS EC2 with Amazon Linux 2023, install Apache, PHP and MariaDB with sudo yum install -y httpd mariadb105-server php php-fpm php-mysqlnd ..., create a wordpress database and user, download WordPress to /var/www/wordpress, set the database details in wp-config.php, give the files to the apache user and add an Apache virtual host. Then open http://<ELASTIC_IP>/ and finish the five-field installer. Attach an Elastic IP first, because WordPress stores the site URL in its database.

Hello friends! WordPress is a CMS that runs on PHP and MySQL – once installed, you can publish posts without writing code. Today we build your own blog, where you can post your AWS notes and project updates every day. There are many steps, but don't worry – do each step with me and don't skip anything.

Quick answer

On Amazon Linux 2023 (Elastic IP attached, HTTP 80 open), the whole install in short:

sudo yum install -y httpd mariadb105-server php php-fpm php-mysqlnd php-gd php-mbstring php-xml php-intl php-opcache wget
sudo service httpd start && sudo service mariadb start && sudo service php-fpm start
sudo systemctl enable httpd mariadb php-fpm
# create DB "wordpress" + user "wpuser" (Step 2), then:
cd /tmp && wget https://wordpress.org/latest.tar.gz && tar -xzf latest.tar.gz && sudo mv wordpress /var/www/wordpress
# wp-config.php (Step 4), ownership (Step 5), virtual host (Step 6), then open http://<ELASTIC_IP>/

What do I need before installing WordPress?

How does WordPress serve a page on EC2?

How a WordPress page is built on EC2 The browser asks Apache on port 80 for a page. Apache hands the PHP file to PHP-FPM, WordPress's PHP code reads the posts from the MariaDB database named wordpress, and the finished HTML page goes back to the browser. Browser EC2 instance (Amazon Linux 2023) Apacheport 80 PHP-FPMWordPress code MariaDBDB "wordpress" GET / index.php SELECT posts HTML page

A WordPress page: Apache receives the request, PHP-FPM runs the WordPress code, which reads the posts from the MariaDB database, and the finished HTML page goes back to the browser.

WordPress is an open-source content management system (CMS) written in PHP that stores posts, users and settings in a MySQL/MariaDB database. Requirements: PHP 7.4+ (8.x recommended), MySQL 8.0+ or MariaDB 10.5+, and a web server with URL rewriting.

How do I install WordPress on Amazon Linux 2023?

Step 1 — Install Apache, PHP and MariaDB (LAMP)

sudo yum install -y httpd mariadb105-server php php-fpm php-mysqlnd \
  php-gd php-mbstring php-xml php-intl php-opcache wget
sudo service httpd start
sudo service mariadb start
sudo service php-fpm start
sudo systemctl enable httpd mariadb php-fpm
sudo mysql_secure_installation          # answer the questions; remove anonymous users and the test DB

Step 2 — Create the WordPress database and user

Replace Wp@Pass123 with your own strong password here and in Step 4:

sudo mysql <<'SQL'
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'Wp@Pass123';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
SQL

Step 3 — Download and extract WordPress

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo mv wordpress /var/www/wordpress
ls /var/www/wordpress

Step 4 — Configure wp-config.php

cd /var/www/wordpress
sudo cp wp-config-sample.php wp-config.php
sudo sed -i "s/database_name_here/wordpress/" wp-config.php
sudo sed -i "s/username_here/wpuser/" wp-config.php
sudo sed -i "s/password_here/Wp@Pass123/" wp-config.php
grep DB_ wp-config.php

Add unique security keys (salts) from the official generator:

curl -s https://api.wordpress.org/secret-key/1.1/salt/
sudo nano /var/www/wordpress/wp-config.php

In nano, delete the eight lines that contain put your unique phrase here and paste the eight lines printed by curl. Save (Ctrl+O, Enter) and exit (Ctrl+X).

Step 5 — Set ownership and permissions

On Amazon Linux 2023 Apache and PHP-FPM run as the user apache:

sudo chown -R apache:apache /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 755 {} \;
sudo find /var/www/wordpress -type f -exec chmod 644 {} \;
sudo chmod 640 /var/www/wordpress/wp-config.php

Step 6 — Add the Apache virtual host

sudo tee /etc/httpd/conf.d/wordpress.conf > /dev/null <<'VHOST'
<VirtualHost *:80>
    ServerName blog.local
    DocumentRoot /var/www/wordpress
    DirectoryIndex index.php index.html
    <Directory /var/www/wordpress>
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog  /var/log/httpd/wordpress_error.log
    CustomLog /var/log/httpd/wordpress_access.log combined
</VirtualHost>
VHOST
sudo apachectl configtest && sudo service httpd reload

mod_rewrite is enabled by default on Amazon Linux 2023, and AllowOverride All lets WordPress's .htaccess create pretty permalinks. Because this is the only vhost, it also answers requests to the Elastic IP; when you have a domain, set ServerName to it.

Ravindra Bagale's Tip

Attach an Elastic IP before you open the WordPress installer. WordPress saves the site URL in the database, and if the public IP changes later, every link and CSS file points to the old IP – the site looks broken. Two minutes of planning saves an hour of fixing.

Step 7 — Finish the installation in the browser

Open http://<ELASTIC_IP>/ → choose a language → enter the Site Title, an admin Username (not admin), a strong Password and your Email → Install WordPress → log in at http://<ELASTIC_IP>/wp-admin. Your blog is live! 🎉

How do I secure WordPress after installing it?

  • Strong admin password; never the username admin.
  • Keep WordPress core, themes and plugins updated; delete unused ones.
  • wp-config.php at 640, and disable file editing in the dashboard: add define('DISALLOW_FILE_EDIT', true); to wp-config.php.
  • Point a domain to the server and enable HTTPS — see How to Connect a GoDaddy Domain to AWS EC2 and How to Install a Free SSL Certificate on EC2 (use certbot --apache).
  • Back up the database (mysqldump wordpress) and wp-content regularly, and take EBS snapshots.
  • Bigger media uploads: raise upload_max_filesize and post_max_size in /etc/php.ini, then sudo service php-fpm restart.

Ravindra Bagale's Tip

💡 Many students see a 404 on every post after setting permalinks to Post name. The reason: the vhost has no AllowOverride All, so .htaccess doesn't work. On Nginx you need try_files $uri $uri/ /index.php?$args;. And never use admin as the login username – bots try that first.

How do I fix common WordPress errors on EC2?

Ghabru naka 😅 — these are the usual ones:

Symptom Likely cause Fix
Error establishing a database connection Wrong DB name/user/password in wp-config.php, or MariaDB stopped grep DB_ /​var/​www/​wordpress/​wp-​config.​php; sudo service mariadb status
Browser downloads index.php or shows PHP code PHP-FPM not running sudo service php-​fpm start; sudo service httpd restart
Apache test page instead of WordPress vhost not loaded sudo apachectl -​S; check /​etc/​httpd/​conf.​d/​wordpress.​conf; reload
Site redirects to an old IP Public IP changed after stop/start Attach an Elastic IP; add define('WP_​HOME','http://​NEW_​IP'); define('WP_​SITEURL','http://​NEW_​IP'); to wp-config.php
Posts give 404 after changing permalinks .htaccess ignored AllowOverride All in the vhost, reload
Plugin/theme install asks for FTP details Files not owned by apache sudo chown -​R apache:​apache /​var/​www/​wordpress
Upload file size error PHP limits Raise upload_​max_​filesize / post_max_size in /etc/php.ini, restart PHP-FPM

Learn it properly

This guide is the short path. The free AWS course explains every step in depth, with labs (Ubuntu, CentOS Stream 9 and Nginx/LEMP variants too):

Got it? Stack, database, wp-config.php, ownership, vhost and the browser install – those are the six steps of WordPress. Now add a domain and HTTPS, and publish your first post.

Frequently asked questions

How do I install WordPress on AWS EC2?

Install Apache, PHP and MariaDB, create a database and user, download WordPress to /var/www/wordpress, set the database details in wp-config.php, give ownership to apache, add a virtual host, and finish the installer in the browser.

Which EC2 instance type do I need for WordPress?

WordPress needs PHP, MySQL or MariaDB and about 1 GB of RAM. A free-tier eligible t3.micro works for learning; add swap if memory runs short.

Why should I attach an Elastic IP before installing WordPress?

WordPress stores the site URL in its database. If the public IP changes after a stop/start, every link and CSS file points to the old IP and the site breaks.

WordPress redirects to an old IP address. How do I fix it?

Add define('WP_HOME','http://NEW_IP'); and define('WP_SITEURL','http://NEW_IP'); to wp-config.php, or better, attach an Elastic IP or a domain and use that.

Why do my WordPress posts show 404 after changing permalinks?

Apache must allow .htaccess: the virtual host needs AllowOverride All. On Nginx use try_files $uri $uri/ /index.php?$args;.

Should I use Apache or Nginx for WordPress?

Both work. Apache (LAMP) is the simplest because WordPress's .htaccess rules work at once. Nginx (LEMP) with PHP-FPM needs its own server block but uses less memory under load.