Install WordPress on Ubuntu (16.04–24.04) with LAMP – Full Setup Guide

Install WordPress on Ubuntu 16.04–24.04 with LAMP (Apache, MySQL, PHP)

This guide shows you how to install WordPress on a LAMP stack (Linux, Apache, MySQL/MariaDB and PHP) on Ubuntu 16.04, 18.04, 20.04, 22.04 and 24.04 using SSH and the command line.

Although you can spin up a WordPress droplet with a one-click app, I prefer to install it manually. That way I can run multiple WordPress sites on a single server, tune the stack exactly how I like, and reuse the same setup for new projects or sites I sell on Flippa.

Most of my WordPress installs run on small Ubuntu LAMP droplets (Apache + PHP + MySQL/MariaDB). This page is the updated version of my original Ubuntu 16.04 guide, expanded to cover Ubuntu 16.04 all the way through to 24.04.

What you'll do in this guide
1) Install the LAMP stack (per Ubuntu version)
2) Enable Apache rewrite and configure a virtual host
3) Download and copy WordPress into place
4) Create a MySQL/MariaDB database and user
5) Fix file permissions and configure wp-config.php
6) Run the WordPress installer in your browser
7) Repeat for more domains on the same server

Step 0 – Install the LAMP stack (per Ubuntu version)

If your server already has Apache, MySQL/MariaDB and PHP installed and working, you can skip this step. Otherwise choose your Ubuntu version below and run each command line by line.

Ubuntu 16.04 – LAMP

sudo apt-get update
sudo apt-get install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-curl php-gd php-mbstring php-zip unzip -y
sudo mysql_secure_installation

Ubuntu 18.04 – LAMP

sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-curl php-gd php-mbstring php-zip unzip -y
sudo mysql_secure_installation

Ubuntu 20.04 – LAMP

sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-curl php-gd php-mbstring php-zip unzip -y
sudo mysql_secure_installation

Ubuntu 22.04 – LAMP

sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-curl php-gd php-mbstring php-zip unzip -y
sudo mysql_secure_installation

Ubuntu 24.04 – LAMP

sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql php-xml php-curl php-gd php-mbstring php-zip unzip -y
sudo mysql_secure_installation
These commands use the default PHP and MySQL/MariaDB versions shipped with each Ubuntu release. That keeps things simple and well-supported for WordPress.

Step 1 – Enable Apache rewrite & .htaccess support

WordPress needs "pretty permalinks" which rely on .htaccess and Apache's rewrite module. Edit /etc/apache2/apache2.conf and allow overrides in the web root.

sudo nano /etc/apache2/apache2.conf

Find the block and make sure it looks like this:


    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted

Now enable the rewrite module and restart Apache:

sudo a2enmod rewrite
sudo systemctl restart apache2

Step 2 – Create an Apache virtual host for your domain

In this guide we'll use example.com. Replace it with your own domain name everywhere you see it.

Create the document root and set ownership:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data:www-data /var/www/example.com

Create a new virtual host config file:

sudo nano /etc/apache2/sites-available/example.com.conf

Paste this configuration:


    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/public_html

    
        AllowOverride All
        Require all granted
    

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

Enable the site and disable the default site, then reload Apache:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Make sure DNS for example.com points to your server's IP. Once you can load http://example.com and see either a blank page or Apache test page in that directory, you're ready to install WordPress.

Step 3 – Download & prepare WordPress

We'll download WordPress into /tmp, prepare permissions, then copy it into the site root.

cd /tmp
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
touch /tmp/wordpress/.htaccess
chmod 660 /tmp/wordpress/.htaccess
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
mkdir /tmp/wordpress/wp-content/upgrade
sudo cp -a /tmp/wordpress/. /var/www/example.com/public_html

Step 4 – Create the WordPress database & user

Log into MySQL or MariaDB as the root user:

mysql -u root -p

Inside the MySQL prompt, create a database and user (change the values):

CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Make a note of the database name, username and password – you'll need them for wp-config.php and the WordPress installer.

Step 5 – Fix file permissions

Give the web server user (www-data) ownership of your site's files:

sudo chown -R www-data:www-data /var/www/example.com

Optional extra hardening similar to your original guide:

sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \;
sudo chmod g+w /var/www/example.com/public_html/wp-content
sudo chmod -R g+w /var/www/example.com/public_html/wp-content/themes
sudo chmod -R g+w /var/www/example.com/public_html/wp-content/plugins

Step 6 – Configure wp-config.php

Edit your wp-config.php file in the site root:

sudo nano /var/www/example.com/public_html/wp-config.php

Update the database settings:

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'strongpassword' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', '' );

Next, generate fresh security keys (salts) from WordPress.org in your terminal:

curl -s https://api.wordpress.org/secret-key/1.1/salt/

Copy the output and paste it over the existing AUTH_KEY, SECURE_AUTH_KEY, etc. values in wp-config.php, then save and exit.

Step 7 – Run the WordPress installer

Open your domain in a browser:

http://example.com (or https://example.com if you've already added SSL)

You should see the WordPress installation wizard. Follow the prompts, choose a site title, admin username and password, and finish the installation.

That's it – you now have a working WordPress site running on Ubuntu with a LAMP stack.

Hosting multiple WordPress sites on one LAMP server

This setup is ideal for hosting multiple low-traffic WordPress sites on a single droplet or VPS. For each new site you want to add:

  1. Create a new document root, e.g. /var/www/newdomain.com/public_html
  2. Create a new Apache virtual host for that domain
  3. Point DNS A records for the domain to your server
  4. Download and copy a fresh WordPress install into that directory
  5. Create a new database and user for that site
  6. Run the WordPress installer for the new domain

For a full walkthrough of the multi-site idea, see my original guide: Host Multiple Websites on Ubuntu 16.04 with LAMP .

Get $200 Free DigitalOcean Credit

Remember, if you don't already have a DigitalOcean account, you can get $200 free credit using my referral link below:

👉 Click here to claim $200 DigitalOcean credit

I use DigitalOcean for nearly all of my WordPress and Nginx servers, including the exact setup shown in this guide. It's fast, reliable, and perfect for running Postfix email forwarding.

Scroll to Top