Skip to content

Install WordPress on Ubuntu Linux

9 minute read
Content level: Intermediate
3

Guide to deploy performance optimized WordPress with Amazon CloudFront (optional) on Amazon EC2 instance running Ubuntu Linux 24.04

Overview

This article suggests how you can install WordPress on Ubuntu Linux EC2 instances with a LAMP stack. It also covers optional steps to deploy Amazon CloudFront CDN and to install domain validated HTTPS certificate on your EC2 instance.

To install WordPress on Amazon Linux 2023 (AL2023), refer to this article

WordPress requirements

At time of writing, WordPress requirements are

  • PHP 8.3 or greater
  • MySQL 8.0 or greater OR MariaDB 10.6 or greater
  • HTTPS support

Both MySQL 8.0 and MariaDB 10.6 end community support in mid 2026

Method 1: Manual Installation

Launch EC2 instance

Launch a new EC2 instance with the following features

  • Ubuntu Linux 24.04
  • Outbound internet connectivity
  • Attached security group allows inbound HTTP and HTTPS
  • Both x86_64 and arm64 architectures are supported, with AWS Graviton processors usually providing better price performance

Launch Ubuntu EC2 instance

To prevent your EC2 public IPv4 address from changing, associate an Elastic IP address

You may be eligible for t4g free trial. Refer to Announcing Amazon EC2 T4g Free Trial Extension for details

Connect to EC2 instance

Connect to your EC2 instance as ubuntu

Install Apache web server

Install Apache with HTTPS, HTTP/2 and PHP-FPM support

sudo apt update
sudo apt install -y apache2 libapache2-mod-fcgid

sudo a2enmod headers rewrite http2 ssl mime
sudo a2ensite default-ssl

sudo sed -i '$a <Directory /var/www/html/>\n  AllowOverride all\n</Directory>' /etc/apache2/apache2.conf
sudo sed -i '/\/VirtualHost/i RewriteEngine on\nRewriteCond %{REQUEST_URI} !\/\\.well\\-known\/?.*\nRewriteCond %{HTTP:X-AMZ-CF-ID} ^\$ \nRewriteCond %{HTTP:X-VARNISH} ^\$ \nRewriteRule ^ https:\/\/%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]' /etc/apache2/sites-enabled/000-default.conf
sudo systemctl enable --now apache2
sudo systemctl restart apache2

Optional: HTTPS certificate

This section covers how you can associate a valid HTTPS certificate with your WordPress site, and is optional but recommended

To ensure proper operation for IPv4 addresses, associate an Elastic IP address with your EC2 instance

Option 1: Amazon CloudFront

You can use Amazon CloudFront global content delivery network (CDN) with EC2 instance as custom origin or VPC origin, to get a domain name with valid HTTPS certificate. This section covers CloudFront with custom origin.

CloudFront distribution can be created after installing WordPress

EC2 Public DNS name

Go to EC2 console, select your EC2 instance and copy out Public DNS value

EC2 instance public DNS

Optional: IPv6 only DNS name

If your EC2 instance is not associated with an elastic IP address and is in a dual-stack subnet, you can select IPv6-only DNS name as CloudFront origin

IPv6 only

Create distribution

Go to CloudFront console to create a distribution

Create CloudFront distribution

You can select either flat-rate or Pay as you go pricing plan.

Origin type

In Specify type page Origin type section, select Other

Paste you EC2 instance Public DNS value into Custom origin text box

Custom origin

Origin IP address type

If you specify a IPv6 only or dual stack domain, modify Origin IP address type setting accordingly. Else leave it default as IPv4-only

IP address type

Origin settings

In Origin settings section, select Customize origin settings. Under Protocol section, select HTTP only

Origin settings

Cache settings

In Cache settings section, select Customize cache setings and use the following values

Cache settings

Review your settings, and proceed to create a CloudFront distribution

Alternate domain name

CloudFront provides a distribution domain name with valid HTTPS certificate, e.g. https://d111111abcdef8.cloudfront.net. You can add alternate domain name(also known as a CNAME), and use AWS Certificate Manager to request non-exportable public SSL/TLS certificates at no additional charge

CloudFront domain name

After which, update your DNS record to route traffic from alternate domain to the CloudFront distribution domain name

Option 2: HTTPS certificate on EC2 instance

This section requires a FQDN (fully qualified domain name) whose DNS entry resolves to your EC2 instance public internet IP address.

Skip this section if you do not have a domain name. Alternatively, go to Amazon Route 53 console to register a new domain. After which,create a DNS record to your EC2 instance public IP address.

Using Certbot

While there are other options, this section shows how to use Certbot to request and install free domain validated (DV) HTTPS certificates.

Install Certbot through Snap

sudo snap install certbot --classic
sudo ln -s -f /snap/bin/certbot /usr/bin/certbot
sudo snap set certbot trust-plugin-with-root=ok
sudo snap install certbot-dns-route53

To obtain and install Let's Encrypt certificate for HTTPS

sudo certbot --apache

Certbot with Apache on Ubuntu

Refer to Use Certbot to enable HTTPS on Ubuntu Linux EC2 instances running Apache or Nginx for more details

Install PHP

We will use Ondřej Surý's excellent ppa:ondrej/php repository to install PHP. PHP 8.3 will be installed as per PHP Compatibility and WordPress Versions for good compatibility across the wider ecosystem of plugins and themes. We also install APC User Cache and Valkey, a Redis replacement, for use by caching plugins to improve performance

Modify phpVersion value below if you want to install a different PHP version

phpVersion=php8.3
sudo apt install -y ca-certificates apt-transport-https software-properties-common lsb-release
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update

sudo apt install -y $phpVersion
sudo apt install -y $phpVersion-{common,fpm}
sudo apt install -y $phpVersion-opcache
sudo apt install -y $phpVersion-apcu
sudo apt install -y $phpVersion-redis
sudo apt install -y $phpVersion-mysql
sudo apt install -y $phpVersion-{xml,zip,intl,curl}
sudo apt install -y $phpVersion-{mbstring,bcmath,imagick,gd}
sudo apt install -y $phpVersion-{igbinary,msgpack}

sudo a2dismod $phpVersion
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf $phpVersion-fpm

if ( ! cat /etc/apache2/mods-enabled/dir.conf | grep -q "DirectoryIndex index.php" ); then
  sudo sed -i "s/\bDirectoryIndex\b/& index.php/" /etc/apache2/mods-enabled/dir.conf
fi

sudo systemctl enable --now $phpVersion-fpm
sudo systemctl restart apache2

sudo apt install -y imagemagick
sudo apt install -y ghostscript

sudo apt install -y valkey-server
sudo systemctl enable --now valkey-server

WordPress Hosting Handbook provides a complete list of recommended and optional extensions to install.

Changing installed PHP version

To update to a newer PHP version in future, you will need to disable currently installed version

# version to upgrade from
phpVersion=php8.3

sudo systemctl stop apache2 $phpVersion-fpm
sudo a2disconf $phpVersion-fpm
sudo systemctl disable $phpVersion-fpm

Install new PHP version as per previous section install script with your desired phpVersion value, e.g. php8.4. After installing, run the following command to verify that the desired version is set as default

sudo update-alternatives --config php

Install MariaDB database server

This section will install MariaDB database, and mariadb-backup. Alternatively, you can consider using Amazon RDS

sudo apt install -y mariadb-server
sudo apt install -y mariadb-backup
sudo systemctl enable --now mariadb

Create database and user

Create a database and user. Modify DB_USER_PW value below with a strong password and execute the script block

DB=wordpress
DB_USER=wordpress
DB_USER_PW=change2Str@ongPass0rd

sudo mysql -u root -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_USER_PW';
CREATE DATABASE $DB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON $DB.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;"

Download WordPress installation files

Download latest WordPress version. If you need an earlier version, get download link from Release Archive page.

cd /tmp
curl -L -O https://wordpress.org/latest.tar.gz
tar -xf latest.tar.gz

sed -i "/Add any custom value/a \$_SERVER['HTTPS'] = 'on';" /tmp/wordpress/wp-config-sample.php

tee /tmp/wordpress/.htaccess > /dev/null << EOF
<IfModule mod_headers.c>
  <FilesMatch "\.(css|gif|ico|js|jpg|jpeg|png|svg|ttf|txt|webp|woff|woff2)$">
  Header set Cache-Control "public, max-age=31536000"
  </FilesMatch>
</IfModule>
EOF

sudo rsync -r /tmp/wordpress/ /var/www/html/
sudo chown www-data:www-data -R /var/www/html

WordPress installation

If you have installed a certificate, open a browser to your domain name, e.g. https://<wordpress.example.com>/. Else open a browser to your EC2 instance IP address, eg https://<EC2-IP-ADDRESS>

WordPress install

Run the install script. You will need to enter the database credentials that was configured previously to setup configuration file.

Optional: remote graphical desktop (GUI) access

If you need a graphical desktop environment, refer to Install GUI (graphical desktop) on Amazon EC2 instances running Ubuntu Linux

Optional: data protection

To protect your data, you may want to use AWS Backup to create EC2 backup job

Method 2: CloudFormation

This method uses CloudFormation IaC (infrastructure as code) template to automate previous method install steps, and include additional functional, security and performance features.

Go to ec2-lamp-server GitHub repository, Ubuntu Linux template page, and click Download raw file. Login to CloudFormation console. Create a stack using the downloaded file.

In LAMP section, select WordPress under Application stack to install, and desired PHP version

WordPress selection

To use CloudFront, select Yes for Create Amazon CloudFront distribution, and choose between Custom Origin or VPC origin for origin type

Create CloudFront

Remote graphical desktop access with Amazon DCV is available as an option

Remote GUI

To protect EC2 data, select Yes for Backup EC2 instance, and adjust backup schedule, time zone and retention settings.

AWS Backup

Refer to site for parameter options and deployment instructions, including WordPress install video and HTTPS certificate request

Using WordPress

Do refer to WordPress documentation

Optional: WP-CLI

To install WP-CLI

cd /tmp
curl -s -L -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

To check version

wp --version

Output should be similar to below

WP-CLI 2.12.0

To update

sudo wp cli update

WordPress with Amazon CloudFront

If you have created a CloudFront distribution, you can login to WordPress admin panel and change your WordPress Address (URL) and Site Address (URL) to your CloudFront distribution or alternate domain name.

CloudFront WordPress Site URL

To further secure and accelerate your WordPress site, refer to blog Secure and accelerate your WordPress CMS with Amazon CloudFront, AWS WAF, and edge functions

WordPress Optimization

This article implements many measures to improve performance, including Graviton processors, browser caching, PHP OPcache, and CloudFront CDN for content offloading. You can install a caching plugin to cache your WordPress posts and pages as static files.

Do refer to WordPress Optimization page for more details.

Optional: Vinyl / Varnish HTTP cache

The article installs OPcache server caching to improve PHP performance. You can fine tune the settings to further improve performance.

To install Vinyl (formerly know as Varnish) HTTP cache

sudo apt install -y varnish

Do refer to Varnish Developer Portal for configuration details

Other Install Options

Other options include:

AWS
EXPERT

published 7 months ago824 views