Ubuntu 18.04 Install WordPress in Home partition

­Install LAMP

For the purposes of this exercise the Apache document root will be moved to /home/mike/www/ and MySQL data directory moved to /home/mysql and a virtual host created called pcdocgo.co.uk

Use Tasksel to install LAMP services

$ sudo apt install tasksel

Install latest Apache, MySQL and PHP servers

$ sudo tasksel install lamp-server

Configure Apache

Add user to Apache www-data group

$ sudo useradd -G www-data mike

Prepare new document root

$ mkdir /home/mike/www
$ sudo rsync -av /var/www/html /home/mike/www

Note: rsync – be sure there is no trailing slash on the source directory. When there’s a trailing slash, rsync will dump the contents of the directory into the destination directory instead of transferring it into a containing directory of the same name as the source, e.g. html.

Add Virtual Host

$ cd /etc/apache2/sites-available
$ sudo cp 000-default.conf pcdocgo.co.uk.conf
$ sudo nano /etc/apaches2/sites-available/pcdocgo.co.uk.conf

Replace

DocumentRoot /var/www/html

with

DocumentRoot /home/mike/www
<Directory /home/mike/www/wordpress/>
 Options Indexes FollowSymLinks
 AllowOverride All
 Require all granted
</Directory>

Save file.

Note: Directory directives explanation

  1. Options
    • Indexes – if a URL which maps to a directory is requested and there is no DirectoryIndex (e.g., index.html) in that directory, then mod_autoindex will returns a formatted listing of the directory WordPress .htaccess code redirects this to index.php
    • FollowSymLinks – the server will follow symbolic links in this directory, e.g. Permalinks
  2. AllowOverride All – allow the use of .htaccess to override Apache global settings for this directory
  3. Require all granted – access to directory is allowed unconditionally to avoid permission errors

Use a2ensite to enable site – creates software symbolic link in /etc/apache2/sites-enabled

$ sudo a2ensite pcdocgo.co.uk.conf

Disable the Apache default site (/var/www/html)

$ sudo a2dissite 000-default.conf

Enable Apache2 mod_rewrite module to allow modification of URLs

$ sudo a2enmod rewrite

Create .htaccess file and add WordPress rewrite code support

Now that Apache is configured to allow rewrites through .htaccess files, we need to create it in the site root folder.

Create empty .htaccess file

$ touch /home/mike/www/wordpress/.htaccess

Add web server (www-data) group ownership:

$ sudo chown :www-data /home/mike/www/wordpress/.htaccess

$ nano /home/mike/www/.htaccess

Add the following code:

# BEGIN WordPress

RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]

# END WordPress

Note: WordPress adds/updates the above .htaccess code when Permalink Settings > Post aliases are changed. e.g, name.

Once modified save and exit.

Note: If WordPress were installed directly in the document root directory the rewrite codes would be:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

For increased security set .htaccess permission to read only.

$ chmod 644 /home/mike/www/.htaccess

644 is normally required and recommended for .htaccess.

Test Apache2 configuration changes

$ sudo apachectl -S

To suppress AH00558 error
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message

Add Servername directive to Apache2 global configuration file

$ sudo nano /etc/apache2/apache2.conf

Add

ServerName 127.0.0.1

Save and exit and test again

$ sudo apachectl configtest

Reports “Syntax OK” when no errors found

Restart Apache2 to effect changes

$ sudo systemctl restart apache2

Configure MySQL

Move MySQL data directory to /home/mike/mysql

Check current root is /var/lib/mysql/

$ mysql -u root -p
mysql> select @@datadir;

Output

+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)

mysql> exit;

Stop MySQL

$ sudo systemctl stop mysql

Copy data directory to /home/mysql

$ sudo rsynch -av /var/lib/mysql /home

Backup original mysql folder by renaming it

$ sudo mv /var/lib/mysql /var/lib/mysql.bak

Point mysql data directory to new home folder location

Edit the MySQL configuration file

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find “datadir” and change the path form /var/lib/mysql to /home/mysql

Edit AppArmor alias file to let MySQL write to the new directory by creating an alias between the default directory and the new location.

$ sudo nano /etc/apparmor.d/tunables/alias

Add the following alias rule to the end of the alias file

$ alias /var/lib/mysql/ -> /home/mysql/,

Restart AppArmor

$ sudo systemctl restart apparmor

Restart MySQL

$ sudo systemctl restart mysql

Verify status

$ sudo systemctl status mysql

Create WordPress Database and Admin User

$ mysql -u root -p
mysql> CREATE DATABASE pcdocgo_db;
mysql> CREATE USER mike@localhost IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON pcdocgo_db.* TO mike@localhost;
mysql> FLUSH PRIVILEGES;
mysql> EXIT;

Install WordPress

Download latest WordPress and extract to ~/wordpress

$ cd ~
$ wget http://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz

Configure WordPress

$ cd ~/wordpress
$ cp wp-config-sample.php wp-config.php

Download WordPress Secure Keys and add to wp-config.php

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

Copy output to Paste buffer

$ nano wp-config.php

Overwrite dummy values section below with Paste buffer

. . .
#define('AUTH_KEY', 'put your unique phrase here');
#define('SECURE_AUTH_KEY', 'put your unique phrase here');
#define('LOGGED_IN_KEY', 'put your unique phrase here');
#define('NONCE_KEY', 'put your unique phrase here');
#define('AUTH_SALT', 'put your unique phrase here');
#define('SECURE_AUTH_SALT', 'put your unique phrase here');
#define('LOGGED_IN_SALT', 'put your unique phrase here');
#define('NONCE_SALT', 'put your unique phrase here');
. . .

Replace DB_NAME, DB_USER, and DB_PASSWORD values with database, user and password values:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'pcdocgo');

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

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

Once complete, save and close wp-config.php

Prepare  WordPress directory

Copy content

$ sudo rsync -avP ~/wordpress /home/mike/www

Configure Permissions

$ cd /home/mike/www

Assign ownership values

$ sudo chown -R mike:www-data *

Create Uploads directory

$ mkdir /home/mike/www/wp-content/uploads

Allow Apache to write to this directory

$ sudo chown -R :www-data /var/www/html/wp-content/uploads

Complete WordPress Installation via Web Interface

In web browser, navigate to localhost

http://localhost

For new installs you will see the WordPress initial configuration page, where you will create an administrator account. Fill out the information for the site and the administrative account information. When finished, click on the Install WordPress button. WordPress will confirm the installation, and then ask you to log in with the account just created

Change the Permalink Settings to Post name

Adjust the permalink settings through the WordPress administration interface. On the left-hand side, under the Settings menu, select Permalinks and click the Post name radio button. Click “Save Changes” to generate the .htaccess rewrite rules.

References:

https://linuxconfig.org/how-to-install-lamp-ubuntu-18-04-bionic-beaver-linux-apache-mysql-php

www.ubuntugeek.com/how-to-change-the-mysql-data-default-directory.html

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04

https://dzone.com/articles/install-wordpress-on-ubuntu-1804

NPM Rebuild

Delete node_modules folder and execute npm install

Leave a Reply

Your email address will not be published. Required fields are marked *