Jump to Content

Andrew M McCall

WordPress Self Hosted A Guided Tour To Running Wordpress Independent Of Big Hosting

Self hosting wordpress can save you money, but more importantly, give you peace of mind owning your own data. You will learn a lot about Linux on the way.

What Is The Meaning Of Self-Hosted WordPress?

Large WordPress hosting companies need to justify themselves to their corporate investing firms. Many of these companies receive large injections of cash into their business. In exchange, they must prove that they can generate a profit, else they risk de-funding from their corporate overlords.

When viewed in this context, it is easy to see how VC Funding / corporate investment groups are the actual customers for these platforms, and the sites they host are a means to extract value for continued financial support.

Self-Hosted WordPress is a way to reclaim some of this value for yourself. In its simplest form, Self-Hosted WordPress is the process of managing the server that WordPress is hosted on yourself. This involves using something like a virtual private server. This is a virtual computer that is provided to an end user which is allocated server resources. This is on a physical server, but ofte times resources are shared among users.

The advantage of this is that it offers users a greater level of control over their server, but helps keep costs under control since resources are being divided. A downside to this is that some users can use more resources than others, causing a disparity in performance.

Self hosted WordPress can give you finer control over your website data, and has a ton of advantages that large hosting firms like WP Engine, Pluggable, Bluehost, Hostinger, etc., only offer at additional cost.

What Are WordPress VPS Requirements?

If you head on over to WordPress Requirements page, you will find out some very basic information:

They also recommend Apache or Nginx, and they also have a link to a “detailed” PHP Extension recommendation list.

Unfortunately, WordPress falls short on recommending hardware requirements. For this, I will just provide my own estimates based on experience:

I have successfully installed WordPress on a 1 core, 512MB ram virtual private server, but it was memory constrained even at low traffic. Mariadb/MYSQL takes about around 200MB of ram, so that doesn’t leave a lot of overhead left after you take away system memory usage, additional services.

I would say at the very least you need a 1 core / 1GB Ram VPS, but I recommend at least 2 core / 2 GB ram.

This, in my experience, has provided more than enough overhead for small sites to grow and can allow additional flexibility for 1 or 2 more services installed on your vps later.

Get A Virtual Private Server For WordPress

Head over to Hetzner Cloud and get yourself signed up for an account. There is a short verification process that entails giving them a photo of your personal information. If you do not feel comfortable with that, Contabo, Linode, or Vultr are also additional VPS hosting options, which are acceptable. I chose Hetzner Cloud because they don’t seem to stuff as many customers on one server giving a bit more performance in my opinion.

Once you have selected a vps provider, it is time to start configuring our server to run WordPress.

VPS Security Guide

I have a full article on configuring a virtual private server and hardening.

If you need additional information on this subject, I would recommend checking there first.

This section assumes that you have some familiarity with SSH. If you need to learn about that, or get a refresher, here is a link to a Digital Ocean Article: SSH Essentials: Working with SSH Servers, Clients, and Keys.

To get started, you need to know your IP Address.

from your computer, open up your favorite terminal and run ssh root@your_ip_address.

Install Uncomplicated Firewall (UFW) For Linux

This will configure ufw firewall to allow ssh connections, but lock down others.

Add A User

We can add a user so that later we can lock down root, and help prevent brute force attacks on our server.

Making sure we can access our server via ssh only

On your local computer:

On your VPS:

Before you go any further, open up a new terminal on your local computer, and verify you can access the VPS via ssh.

Installing Fail2Ban To Prevent Brute Force Attacks, DDoS, and Dictionary

Attacks

jail.local file config

[sshd]
enable = true
port = 22
filter = ssh
logpath = /var/log/auth.log
maxretry = 3

Open Up UFW For Web Traffic So Visitors Can Access WordPress Site

Set Server Time

Point Your DNS To Server For Wordpress

This will often be DNS Provider specific, but in general you now want to point your DNS to your server.

This typically involves setting up an A record with a value of @ that points to your ip address. Additionally, you may also want to configure a cname such as www, and configure an ipv6 if your vps provides you an adddress. Here is an example:

Record TypeHostnameValue/AddressTTLDescription
Aexample.comyour_server_ip4_address3600Points to an IPv4 address
AAAAexample.com2001:0db8:85a3:0000:0000:8a2e:0370:73343600Points to an IPv6 address
CNAMEwwwexample.com.3600Alias for another domain name

Install PHP, NGINX, MariaDB

Verify Installation of PHP

Create A Wordpress Table

Install WP-CLI For Managing Wordpress

Install WordPress With WP-CLI

Add Yourself to the www-data group, Configure WordPress File & Directory

Permissions

Note, these are just recommended Directory and File Permissions for WordPress.

Configure NGINX To Serve WordPress

Now that we have WordPress installed, we need to configure NGINX to serve our WordPress site.

Add the following configuration:

server {
    listen 80;
    listen [::]:80;
    server_name yourwebsite.com www.yourwebsite.com;
    root /var/www/yourwebsite.com;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    }
}

Install SSL Certificate Using Let’s Encrypt

To secure our WordPress site, we’ll install an SSL certificate using Let’s Encrypt and Certbot.

Follow the prompts to obtain and install the SSL certificate.

By default, WordPress uses a plain permalink structure. Let’s change it to a more SEO-friendly structure.

Set Up Server-Side Caching with NGINX

To improve the performance of your WordPress site, you can configure server-side caching using NGINX’s FastCGI cache module. This will cache the dynamic PHP content and serve it directly from NGINX, reducing the load on your server.

fastcgi_cache_path /var/cache/nginx/wordpress levels=1:2 keys_zone=wordpress:10m max_size=10g inactive=60m use_temp_path=off;

server {

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;

        # FastCGI cache settings
        fastcgi_cache wordpress;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        add_header X-FastCGI-Cache $upstream_cache_status;
    }
}

Don’t forget to cache static assets

GTMetrix will most definitely have issue if you don’t do this in your nginx config:

 location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|eot)$ {
        expires 28d;
        add_header Cache-Control "public, no-transform";
        # Optionally set:
         add_header Pragma "public";
         add_header Vary "Accept-Encoding";
    }

Updating PHP.INI For Wordpress File Uploads & Post Requests

Add PHP workers

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Note: these can be adjusted as needed. I would say mind your actual cpu core/thread count for this. These are just example values.

Automatic Updates with WP-CLI and Crontab

WP Engine charges $7 usd per site for this :-)

Conclusion

Congratulations! You have successfully set up a self-hosted WordPress site on your own virtual private server. By following this tutorial, you have gained control over your website, improved security, and learned valuable Linux skills along the way.

Remember to keep your WordPress site, plugins, and themes up to date to ensure optimal performance and security. Regularly backup your site to protect your data in case of any unforeseen issues.

Self-hosting WordPress may require more effort compared to using managed hosting solutions, but the benefits of ownership, flexibility, and cost savings make it a worthwhile endeavor for many website owners.

Happy self-hosting!

Tags: