Step 1: Start from a LAMP Server
Apache, MySQL, and PHP must already be installed; see how to install a LAMP stack on Ubuntu. Then add the PHP extensions WordPress uses and reload Apache so mod_php loads them:
sudo apt install php-curl php-gd php-intl php-mbstring php-xml php-zip
sudo apachectl configtest
sudo systemctl reload apache2
Step 2: Create the Database and User
sudo mysql
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
EXIT;
Type the real password in the SQL shell, never on a shell command line. The account can reach only the wordpress database. Accounts and grants are explained in how to install MySQL on Ubuntu.
Step 3: Download and Verify WordPress
cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
curl -LO https://wordpress.org/latest.tar.gz.sha1
echo "$(cat latest.tar.gz.sha1) latest.tar.gz" | sha1sum -c -
Continue only if the check prints latest.tar.gz: OK. Then unpack the archive and move it into place:
tar -xzf latest.tar.gz
sudo mv wordpress /var/www/example.com
Step 4: Ownership by www-data
sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
Apache and mod_php run as www-data, so WordPress can write its uploads and its own .htaccess file. Never use 777. See chown recursive.
Step 5: Create wp-config.php
cd /var/www/example.com
sudo -u www-data cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Fill in DB_NAME (wordpress), DB_USER (wp_user), and DB_PASSWORD, and keep DB_HOST as localhost. Replace the eight placeholder lines of keys and salts with fresh ones from the official WordPress secret key generator; its address is in the comment above those lines. Then restrict the file:
sudo chmod 640 wp-config.php
Step 6: The Virtual Host
Create /etc/apache2/sites-available/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/example.com/wp-content/uploads>
AllowOverride None
<FilesMatch "\.php$">
Require all denied
</FilesMatch>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
- AllowOverride All: lets Apache read the .htaccess file that WordPress writes for permalinks. With the Debian and Ubuntu default of None, pretty links return 404. See what is .htaccess.
- Uploads block: refuses to run PHP files in the uploads folder, where a script uploaded through a weak plugin would land.
Step 7: Enable the Site and mod_rewrite
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo apachectl configtest
sudo systemctl restart apache2
A restart is used here because a new module is loaded and a2enmod asks for one; later changes need only a reload. mod_rewrite is explained in how to enable mod_rewrite in Apache.
Step 8: HTTPS, Then the Installer
sudo certbot --apache -d example.com -d www.example.com
Get the certificate first, so the admin password never travels over plain HTTP; see Certbot Apache. Then open https://example.com and enter a site title, an admin user name other than admin, and a strong password.
Troubleshooting
- The Apache default page appears: the site is not enabled or the name does not match; check with
sudo apache2ctl -S. - Posts return 404 but the home page works: AllowOverride or mod_rewrite is missing.
- Error establishing a database connection: the values in wp-config.php do not match the database user.
- Uploads fail: check ownership and the limits in how to increase the PHP upload limit.
Do not leave test files such as a phpinfo() page in the web root.