قالب وردپرس درنا توس
Home / Tips and Tricks / How to set up a WordPress site on your own servers (with Ubuntu and NGINX) – CloudSavvy IT

How to set up a WordPress site on your own servers (with Ubuntu and NGINX) – CloudSavvy IT



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 who just want a fun project to set up their blog. We will show how to set up and configure a simple WordPress site on Ubuntu Linux with NGINX.

Install dependencies

First we need to install NGINX. Updating apt and install it:

sudo apt update
sudo apt install nginx

If you have a firewall enabled, such as UFW, you need to open it:

sudo ufw allow 'Nginx HTTP'

We will configure NGINX once WordPress is installed. In the meantime, we need to create a MySQL database that WordPress can use.

If you don̵

7;t have MySQL installed, you can download it from apt. Make sure you run mysql_secure_installation once installed to lock it. You can read our guide to general database security for more information, as long as MySQL is running localhost, you should be okay.

sudo apt install mysql-server
sudo mysql_secure_installation

Create a database that WordPress can use. You don’t need to set up tables or anything, just an empty database is enough:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Then create a user named 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 PHP is 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 is up to date with the new extensions. Keep in mind that you may need to change the version number here.

sudo systemctl restart php7.4-fpm.service

Install 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 where WordPress actually reads:

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

Move the temp folder to the correct installation location, and chown the data:

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

You must set the salts used for security and password management. You can get a new set of salts from the WordPress API:

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

Then open the WordPress configuration file and paste the values ​​in:

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

While in this file, you 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 will need to be configured and we will need to configure NGINX to display the content.

Configure NGINX

NGINX uses configuration 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 enter your hostname information and other settings, as well as update the PHP version if you are using a newer one. You must save this in sites-availableand then a symlink to sites-enabled to enable it.

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

Once enabled, restart NGINX and you will see WordPress when you visit the site in your browser. You must perform the initial installation procedure.

sudo service nginx restart

You can now view your dashboard at any time at https://example.com/wp-admin/.

Next, you need to configure SSL to secure connections on your site. You can do this for free with 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 have to restart NGINX.

sudo service nginx restart

Setting up your site

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

Whichever theme you choose, you must activate it from the Appearance tab.

You can then 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, allowing for every element on the page to be changed.

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 format a grid with two message sides instead of just a list of messages.

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

Source link