I Want to Set Up My WordPress Site On My Own Servers With Ubuntu & NGINX

 

WordPress Logo Dark Mode
WordPress

While you can always pay someone else a monthly fee to manage your WordPress instance, self hosting makes a lot of sense for many people who have existing hardware to take advantage of, or just want a fun project setting up their blog. We’ll show how to set up and configure a basic WordPress site on Ubuntu Linux using NGINX.

Installing Dependencies

First, we’ll need to install NGINX. Update apt and install it:

sudo apt update
sudo apt install nginx

If you have a firewall enabled, like UFW, you’ll need to open it up:

sudo ufw allow 'Nginx HTTP'

We’ll configure NGINX once WordPress is installed. In the meantime, we’ll need to create a MySQL database for WordPress to use.

If you don’t have MySQL installed, you can get it from apt. Make sure to run mysql_secure_installation once it’s installed to lock it down. You can read our guide on general database security for more info, but as long as MySQL is running on localhost, you should be fine.

sudo apt install mysql-server
sudo mysql_secure_installation

Create a database for WordPress to use. You don’t need to set up tables or anything, just a blank database will do:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Then create a user, called wordpressuser, and give it access to the database. Make sure to change the password.

CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;

WordPress runs on PHP, so it’ll need PHP installed to function. Install it alongside the following extensions:

sudo apt install php-fpm php-mysql
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip

And restart the PHP service to make sure it’s up to date with the new extensions. Note that you may need to change the version number here.

sudo systemctl restart php7.4-fpm.service

Installing WordPress

Download the latest build of WordPress from their site:

cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

Then copy the sample configuration to the location WordPress actually reads from:

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

Move the temp directory to the proper install location, and chown the data:

sudo cp -a /tmp/wordpress/. /var/www/wordpress
sudo chown -R www-data:www-data /var/www/wordpress

You’ll need to set the salts that are used for security and password management. You can fetch a new set of salts from WordPress’s API:

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

Then, open up WordPress’s config file, and paste the values in:

sudo nano /var/www/wordpress/wp-config.php

While you’re in this file, you’ll need to configure the database settings.

define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

define('FS_METHOD', 'direct');

Once that’s done, WordPress should be configured, and we’ll need to configure NGINX to serve the content.

Configuring NGINX

NGINX uses config files in /etc/nginx/sites-available/ to configure different endpoints. Create a new one for your site in this folder, and paste in the following:

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/var/run/php/php7.4-fpm.sock;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ .php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

Note that you will need to put in your info for the hostname and other settings, as well as updating the PHP version if you are using a newer one. You will need to save this in sites-available, then symlink it to sites-enabled to turn it on.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Once it’s enabled, restart NGINX, and you’ll see WordPress if you visit the site in your browser. You’ll need to do the first time setup walkthrough.

sudo service nginx restart

You’ll now be able to view your dashboard at any time at https://example.com/wp-admin/.

Next, you’ll need to configure SSL to secure connections on your site. You can do this for free using certbot, a CLI frontend to LetsEncrypt’s free certificates.

sudo apt-get install certbot

sudo apt install python3-certbot-nginx

Then, run certbot:

sudo certbot nginx -certonly

After that, you will need to restart NGINX.

sudo service nginx restart

Setting Up Your Site

Congratulations! You have a running WordPress site. It probably looks terrible with the default theme. The first thing you’ll want to do is change the theme, which you can do from the dashboard:

Whatever theme you choose, you’ll need to activate it from the appearance tab.

Then, you can configure the settings and style by visiting the site and clicking “Customize” in the header. One of the best things you can do is add completely custom CSS to the site, which can change any element on the page.

If you want to edit something in the theme, and you don’t mind messing around in PHP, you can edit your theme files directly. For example, I edited my theme file to layout a grid with two posts side, rather than just a list of posts.

cd /var/www/wordpress/wp-content/themes/dark-press/
sudo nano index.php

 

About MehdiTech

Check Also

Easy methods to Get well a Deleted File From Microsoft Groups

  Teams is great for storing, sharing, and collaborating on files. One thing it lacks …