GNU/Linux {docs}

LEMP + Wordpress

LEMP Installation

Install Nginx

The short way:

$ sudo apt install nginx
$ sudo systemctl start nginx
$ sudo systemctl status nginx
$ lsb_release -a
$ sudo wget https://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
$ sudo vi /etc/apt/sources.list

add these lines:

deb https://nginx.org/packages/mainline/debian/ <CODENAME> nginx
deb-src https://nginx.org/packages/mainline/debian/ <CODENAME> nginx
$ sudo apt update
$ sudo apt install nginx
$ sudo systemctl start nginx
$ sudo systemctl status nginx
  • Comment or delete the following line in /etc/nginx/nginx.conf:

include /etc/nginx/sites-enabled/*;

To test, go to http://localhost

Install PHP

# apt install php-fpm php-mysql

  • Check that the information below appears in /etc/php/7.2/fpm/pool.d/www.conf :
...
user = www-data
group = www-data
...
listen.owner = www-data
listen.group = www-data
...

Install MariaDB

$ sudo apt install mariadb-server

The MariaDB service will start automatically. You can verify it by typing:

$ sudo systemctl status mariadb

Set MySQL/MariaDB root user password:

$ sudo mysql_secure_installation
Enter current password for root (enter for none): **## Press Enter**
Set root password? [Y/n]**## Press Enter**
New password:## Enter password**
Re-enter new password: **## Re-enter password**
Remove anonymous users? [Y/n]**## Press Enter**
Disallow root login remotely? [Y/n]**## Press Enter**
Remove test database and access to it? [Y/n]**## Press Enter**
Reload privilege tables now? [Y/n]**## Press Enter**

Setting up Nginx Server Block

  • Create the directory structure to host Wordpress:
$ sudo mkdir -p /var/www/example.com/html
$ chown -R $USER:$USER /var/www/$domain/public_html
$ chmod -R 755 /var/www
  • Create example.com.conf file in /etc/nginx/conf.d/ :

$ sudo nano /etc/nginx/conf.d/example.com.conf :

server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.nginx-debian.html;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
  • Check the Nginx configuration file for any syntax errors:

# nginx -t

  • Create file /var/www/example.com/html/index.php with the following content:
<?php
phpinfo();
?>
  • Check your ip and append the ip and domain in /etc/hosts:

$ sudo echo "192.168.1.56 example.com" >> /etc/hosts

  • Start php and nginx:

$ sudo systemctl start nginx php7.2-fpm

If the page does not load, or you have an error, find out with this command:

$ tail -10 /var/log/nginx/error.log

If the php page loads, congratulations ! PHP is working. Now you can delete the file index.php.

Important files and directories:

  • /var/www/
  • /etc/nginx/conf.d/
  • /etc/hosts
  • /etc/php/7.2/fpm/php.ini
  • /etc/php/7.2/fpm/pool.d/www.conf

Wordpress

Downloading Wordpress

$ cd /tmp
$ wget https://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz
$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

Wordpress directory set up

Now, we can copy the entire contents of the directory into our document root. We are using the -a flag to make sure our permissions are maintained. We are using a dot at the end of our source directory to indicate that everything within the directory should be copied, including hidden files (like the .htaccess file we created):

$ sudo cp -a /tmp/wordpress/. /var/www/example.com/html
$ sudo chown -R www-data:www-data /var/www/example.com/html

Next, we will set the setgid bit on each of the directories within the document root. This causes new files created within these directories to inherit the group of the parent directory (which we just set to www-data) instead of the creating user's primary group. This just makes sure that whenever we create a file in the directory on the command line, the web server will still have group ownership over it.

We can set the setgid bit on every directory in our WordPress installation by typing:

$ sudo find /var/www/example.com/html -type d -exec chmod g+s {} \;

There are a few other fine-grained permissions we'll adjust. First, we'll give group write access to the wp-content directory so that the web interface can make theme and plugin changes:

$ sudo chmod g+w /var/www/example.com/html/wp-content

As part of this process, we will give the web server write access to all of the content in these two directories:

$ sudo chmod -R g+w /var/www/example.com/html/wp-content/themes
$ sudo chmod -R g+w /var/www/example.com/html/wp-content/plugins

This should be a reasonable permissions set to start with. Some plugins and procedures might require additional tweaks.

Setting up WordPress Configuration File

$ sudo nano /var/www/example.com/html/wp-config.php

edit:

. . .
define('DB_NAME', 'example_database');
/** MySQL database username */
define('DB_USER', 'wpuser');
/** MySQL database password */
define('DB_PASSWORD', 'mypassword');
. . .

and append:

define('FS_METHOD', 'direct');

The above line will allow us to install Plugins and Themes in Wordpress.

Additional step:

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

Copy the generated values and replace the dummy values at /var/www/html/example.com/wp-config.php

Create Wordpress database

$ sudo mariadb -u root -p
MariaDB> CREATE DATABASE example;
MariaDB> GRANT ALL PRIVILEGES ON example.* TO "wpuser"@"localhost" IDENTIFIED BY "mypassword";
MariaDB> FLUSH PRIVILEGES;
MariaDB> EXIT

Wordpress installation

  • Go to http://example.com and follow instrucions (be sure to type wpuser in user's field in the form)

Updating Wordpress

If you need to update Wordpress, follow these instructions:

$ sudo chown -R www-data /var/www/example.com/public_html

Now, go back the WordPress administration panel and apply the update.

When you are finished, lock the permissions down again for security:

$ sudo chown -R $USER /var/www/example.com/public_html

Initial Set update

Installing Plugins and Themes

In order to install Plugins and Themes, you must edit wp-config file, if you haven't already:

sudo nano /var/www/example.com/html/wp-config.php

Append define('FS_METHOD', 'direct');

and save. Then restart nginx:

sudo systemctl restart nginx

  • Dashboard -> Settings -> Permalinks -> Post name
  • Edit /etc/nginx/conf.d/example.com.conf file and add the following lines if you haven't already:
location /example.com/ {
try_files $uri $uri/example.com/ /example.com/index.php?$args;
}
  • Restart Nginx:

sudo systemctl restart nginx

Home Page setup

Settings -> Reading -> Your homepage displays -> A static page -> Homepage: <select your page>

Prevent Search Engine indexation

When initially creating the website, it is recommended to turn off Search Engine indexation:

Settings -> Reading -> Search Engine Visibility -> Discourage search engines from indexing this site

Anchors

Clickeable Component -> Content -> Link -> #target_anchor

Target anchor component -> Advanced -> CSS ID -> target_anchor (without hash symbol)

Useful Themes and Plugins

  • A good starter theme is OceanWP
  • A must have plugin for OceanWP theme is Ocean Extra
  • A must have plugin for editing pages in general is Elementor
  • Hummingbird Page Speed Optimization
  • Smush Image Compression and Optimization
  • Yoast SEO

Removing OceanWP Top Bar

Appearance -> Customize -> Top Bar -> General -> Enable Top Bar (uncheck)

Pages: How to clean default screen layout for OceanWP theme

  1. Main -> Content Layout -> 100% Full Width
  2. Title -> Display Page Title -> Disable
  3. Click on "Update" button on the right

Prevent Search Engine indexation

When initially creating the website, it is recommended to turn off Search Engine indexation:

Settings -> Reading -> Search Engine Visibility -> Discourage search engines from indexing this site

Yoast SEO - RSS

Content to put before each post in the feed: %%POSTLINK%%

Content to put after each post in the feed: <p>Original article at <a href="https://mydomain.com/" rel="nofollow">My Site</a></p>

Appearance -> Menus -> Menu Structure -> Affected Page -> Disable link

Astra Theme and Plugins

Note: For the Astra Them and Plugins to work, php-xml extension must be installed in the server.

Themes -> Add New -> search for 'Astra' -> Install Now -> Activate

Plugins -> Add New -> search for 'Astra Starter Sites' -> Install Now

Appearance -> Astra Sites -> Elementor -> select Site -> Install Plugins -> Import This Site

Bash Script server management

If you don't want LEMP to be enabled on start up, you can disable it:

  • sudo systemctl disable nginx php7.2-fpm mariadb

You can create the following bash script to manage the LEMP services:

#!/bin/bash
if [ "$1" == "" ]
then
systemctl start nginx
systemctl start mariadb
systemctl start php7.2-fpm
elif [ "$1" == "-s" ]
then
systemctl stop nginx
systemctl stop mariadb
systemctl stop php7.2-fpm
elif [ "$1" == "-r" ]
then
systemctl restart nginx
elif [ "$1" == "-status" ]
then
echo "nginx:"
systemctl status nginx | grep "Active"
echo "mariadb:"
systemctl status mariadb | grep "Active"
echo "php-fpm:"
systemctl status php7.2-fpm | grep "Active"
elif [ "$1" == "--help" ] || [ "$1" == "-h" ]
then
echo "Usage:"
echo "'sudo ./server' will launch nginx, mariadb and php-fpm"
echo "'sudo ./server -s' will stop nginx, mariadb and php-fpm"
echo "'sudo ./server -r' will restart nginx"
echo "'./server -status' will show the status of nginx, mariadb and php-fpm"
else
echo "Wrong argument supplied"
fi
  1. Save the file
  2. chmod +x file_name
  3. Usage:
  • Start LEMP: sudo ./file_name
  • Stop LEMP: sudo ./file_name -s
  • Restart Nginx: sudo ./file_name -r
  • Help: sudo ./filename -h