Installing WordPress on a VPS

WordPress runs a large share of the web, and a VPS gives it its own memory and CPU instead of a slice of a crowded shared server. In return, you set up and look after everything underneath it: the web server, PHP, the database, security and backups.

This guide installs WordPress on Ubuntu or Debian step by step, with HTTPS from the start, the site running as its own user, and the settings that keep it fast and safe afterwards. Allow about an hour. If you would rather not manage a server at all, shared hosting with a one-click installer is a fine choice, and shared hosting or a VPS helps you decide.

2 GB VPS plans in stock, a comfortable size for WordPress

In stock when this page loaded. See every plan

What is in this guide

A few words you will see

TermWhat it means
ApacheThe web server that receives visits and serves your site.
PHP-FPMThe program that runs WordPress's PHP code for the web server.
PoolA group of PHP workers with its own user and settings.
MariaDBThe database that stores your posts, pages, users and settings. A drop-in replacement for MySQL.
wp-config.phpWordPress's main settings file, including the database password.
CertbotA free tool that gets and renews HTTPS certificates.
WP-CronWordPress's built-in task scheduler, for scheduled posts, updates and plugin jobs.

What size to pick

Rough guidance. Plugins and traffic make a big difference.

PlanSuits
1 GBA small blog with few plugins. Workable, but tight once the database grows
2 GBMost personal and small business sites. A comfortable start
4 GBBusy sites, online shops, membership sites or several WordPress sites on one server

A small swap file helps on 1 GB and 2 GB plans. It is a safety net, not extra RAM. See what you can do with a 2 GB VPS for other jobs that fit alongside.

The setup this guide uses

This guide uses Apache, PHP-FPM and MariaDB. Apache is chosen over nginx because many WordPress plugins, such as caching and security plugins, write their rules to .htaccess files, which Apache reads and nginx ignores. nginx works well with WordPress too, but needs those rules written by hand. Web servers on a VPS compares them.

The site runs as its own Linux user through its own PHP pool. That means WordPress can update itself and install plugins without making files writable by the web server, and keeps the site apart from anything else on the server.

Before you start

In the commands, replace example.com with your domain, mysite with a short name for the site, and PHPVER with your PHP version, which ls /etc/php shows, for example 8.3.

1. Install the web server, PHP and database

sudo apt update
sudo apt install apache2 mariadb-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl

These PHP extensions cover WordPress and most plugins. If a plugin later asks for another, install it with sudo apt install php-NAME. Image tools such as php-imagick are optional but help with image quality.

Turn on the Apache modules this guide uses, and open the web ports in the firewall:

sudo a2enmod proxy_fcgi setenvif rewrite headers
sudo systemctl restart apache2
sudo ufw allow "Apache Full"

If the PHP-FPM package suggests enabling a global PHP config for Apache, skip it. The site will use its own pool from step 3.

Secure MariaDB with its setup script. On older systems it is called mysql_secure_installation:

sudo mariadb-secure-installation

Accept the defaults that remove anonymous users, remote root login and the test database.

2. Create the database

Open the database shell with sudo mariadb and type the following, using a long random password of your own:

CREATE DATABASE mysite_wp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'mysite_wp'@'localhost' IDENTIFIED BY 'A_LONG_RANDOM_PASSWORD';
GRANT ALL PRIVILEGES ON mysite_wp.* TO 'mysite_wp'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Typing the password inside the database shell keeps it out of your shell history. Save it in your password manager. You will need it in step 7.

3. Create a user and PHP pool for the site

sudo adduser --disabled-password --gecos "" mysite
sudo mkdir -p /home/mysite/public_html /home/mysite/logs /home/mysite/tmp
sudo chown -R mysite:mysite /home/mysite
sudo chmod 750 /home/mysite
sudo usermod -aG mysite www-data

The last line lets Apache read the site's files, such as images and CSS, through the site's group. Now create the pool:

sudo nano /etc/php/PHPVER/fpm/pool.d/mysite.conf

Put this in it:

[mysite]
user = mysite
group = mysite
listen = /run/php/mysite.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

php_admin_value[error_log] = /home/mysite/logs/php-error.log
php_admin_value[upload_tmp_dir] = /home/mysite/tmp
php_admin_value[session.save_path] = /home/mysite/tmp
php_value[memory_limit] = 256M
php_value[upload_max_filesize] = 64M
php_value[post_max_size] = 64M

pm.max_children is the most PHP workers that can run at once. Eight is a cautious start for a 2 GB plan. Each worker often uses somewhere around 40 to 100 MB, so raise it carefully on bigger plans. The upload settings let you upload files up to 64 MB. Check the pool and restart PHP:

sudo php-fpmPHPVER -t
sudo systemctl restart phpPHPVER-fpm

With PHP 8.3, for example, those are php-fpm8.3 -t and php8.3-fpm.

4. Set up the site in Apache

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

Put this in it:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /home/mysite/public_html

    <Directory /home/mysite/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/mysite.sock|fcgi://localhost"
    </FilesMatch>

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

Enable the site, turn off the default site, check and restart:

sudo a2ensite example.com
sudo a2dissite 000-default
sudo apache2ctl configtest
sudo systemctl restart apache2

A full restart, rather than a reload, makes sure Apache picks up its new membership of the site's group.

5. Turn on HTTPS

Get the certificate before installing WordPress, so the site is set up with secure addresses from the start:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

Certbot asks for an email address for renewal notices, then creates the HTTPS version of your site config. Choose to redirect HTTP to HTTPS if it asks. Certificates renew automatically. Check renewal works with sudo certbot renew --dry-run.

If Certbot fails, your domain probably does not point at the server yet. Check with dig example.com +short and wait for DNS to update.

6. Download WordPress

Download WordPress from its official site, wordpress.org. On the download page, copy the link for the .tar.gz file, then run these commands, pasting that link in place of THE_LINK_YOU_COPIED:

cd /home/mysite
sudo -u mysite wget -O wordpress.tar.gz THE_LINK_YOU_COPIED
sudo -u mysite tar xzf wordpress.tar.gz
sudo -u mysite cp -a wordpress/. public_html/
sudo rm -rf wordpress wordpress.tar.gz

Running the commands as mysite means the files belong to the site's user from the start. Set sensible permissions:

sudo find /home/mysite/public_html -type d -exec chmod 750 {} +
sudo find /home/mysite/public_html -type f -exec chmod 640 {} +

The site user can read and write everything, Apache can read through the group, and other users are kept out.

7. Run the installer

Open your domain in a browser. You should see the WordPress setup screen. Do this straight away: until setup is finished, anyone who finds the page could complete it.

  1. Choose your language.
  2. Enter the database details from step 2. The database name and user are mysite_wp, the host is localhost, and you can keep the default table prefix.
  3. WordPress writes wp-config.php for you, with fresh security keys. It can do this because PHP runs as the site's own user.
  4. Enter your site title and create the admin account. Do not use "admin" as the username, and use a long, unique password from your password manager.

Log in and check the dashboard. Under Settings, then Permalinks, choose "Post name" for clean addresses, and save. WordPress writes the rules to .htaccess, which Apache reads.

4 GB VPS plans in stock, for busy sites and online shops

In stock when this page loaded. See every plan

8. Lock it down

  • Protect wp-config.php. It holds the database password. PHP runs as the site user, so it can still read the file after this:
    sudo chmod 600 /home/mysite/public_html/wp-config.php
  • Turn on automatic updates. In the dashboard, turn on automatic updates for WordPress, plugins and themes, or check for updates every week. Old plugins are the most common way into WordPress sites.
  • Use few, well-maintained plugins. Delete plugins and themes you do not use, as inactive ones can still be attacked.
  • Add two-factor login with a reputable plugin, for every admin account.
  • Limit login attempts. Bots try passwords on the login page constantly. A login-limiting plugin or fail2ban stops them. See installing fail2ban.
  • Turn off open registration under Settings, then General, unless you need it.
  • Set up outgoing email. A new VPS cannot send mail reliably on its own, so password resets and contact forms may never arrive. Use an SMTP plugin with an email or transactional email service, and add the SPF and DKIM records it gives you.

For the server underneath, see a beginner's guide to securing your VPS.

9. Make it fast

  • Use a page caching plugin. It saves finished pages so most visits skip PHP and the database entirely. This is the biggest speed gain for most sites.
  • Run WP-Cron from the system. By default, WordPress runs scheduled tasks during visits, which slows some page loads and misses tasks on quiet sites. Turn it off by adding this line to wp-config.php, above the line that says to stop editing:
    define( 'DISABLE_WP_CRON', true );
    Then run it every five minutes from the site user's crontab, opened with sudo crontab -u mysite -e:
    */5 * * * * cd /home/mysite/public_html && php wp-cron.php > /dev/null 2>&1
  • Check OPcache is on. It keeps compiled PHP in memory and is usually enabled by default. Site Health, under Tools in the dashboard, shows PHP details.
  • Shrink images before uploading, or use an image optimisation plugin.
  • Watch memory. Run free -h now and then. If swap is used most of the time, lower pm.max_children or step up a size.

10. Back it up

A WordPress backup has two parts: the files in public_html, especially wp-content, and the database. Export the database with:

sudo mysqldump --single-transaction mysite_wp > ~/mysite_wp.sql

Keep backups off the server, run them daily, and test a restore once. A backup plugin can do this for you, or you can script it. Using a storage VPS for backups has a script that handles files and databases together.

Troubleshooting

SymptomLikely causeFix
Browser downloads PHP files or shows codeThe PHP lines in the site config are missing, or the pool is not runningCheck the FilesMatch lines in step 4 and run sudo systemctl status phpPHPVER-fpm
503 Service UnavailablePool not running, or socket path does not matchCheck the socket path is the same in the pool file and the site config
403 ForbiddenApache cannot read the folderCheck www-data is in the site's group, then restart Apache
"Error establishing a database connection"Wrong database name, user or passwordCheck the details in wp-config.php against step 2
Installer asks for FTP detailsWordPress cannot write its filesCheck the files belong to mysite and the pool runs as that user
Permalinks give 404 errorsRewrite module off, or AllowOverride missingRun sudo a2enmod rewrite, check AllowOverride All, restart Apache
"The uploaded file exceeds" errorsUpload limits too lowRaise the upload settings in the pool file and restart PHP-FPM
Password reset emails never arriveNo outgoing mail set upSet up an SMTP plugin as in step 8
Site slow, server swappingToo many PHP workers or heavy pluginsLower pm.max_children, add page caching, or step up a size

Limits

  • You are the administrator. A RackNerd VPS is unmanaged. Updates, security and backups are yours to do, every week.
  • Email is separate. This guide does not set up mailboxes for your domain. Use an email service for those.
  • Online shops need more care. Taking payments brings extra security duties. Use your payment provider's hosted checkout or official plugin, and keep everything updated.
  • Licences apply. WordPress is free software, but premium themes and plugins have their own licence terms.
  • Fair use applies. Unmetered does not mean unlimited. Very large media libraries or downloads use bandwidth, and RackNerd's acceptable use policy covers your site.
  • Figures here are starting points. Worker counts and memory use vary with your plugins and traffic.

When to step up

Step up when caching is on and the server still runs short of memory, when you add an online shop or membership features, or when you want several WordPress sites on one server. A 4 GB plan suits most of those, and 6 GB or 8 GB gives room for several busy sites. If you are moving an existing site from shared hosting, moving an overgrown cPanel website to a VPS covers the move.

6 GB and 8 GB VPS plans in stock, for several busy sites

In stock when this page loaded. See all 6 matching plans

What next

To compare what is available now, start with the cheapest RackNerd VPS guide and check the sales page.

More RackNerd guides