How to Install WordPress on LAMP with Ubuntu 16.04 Server, Apache, PHP 7, MySQL and Webmin

Work in Progress…

This tutorial is about installing LAMP + NGINX as a reverse proxy + WORDPRESS on a new remote server (VPS or dedicated). This guide does not cover the Linux-installation itself: Linux should already be installed on the server. Here is our “LAEMP” stack:

  • Linux (Ubuntu 16.04 LTS minimal)
  • Apache
  • (E)NGINX as a reverse proxy
  • MySQL
  • PHP7
  • WordPress

Content

  1. Connect to the server with SSH
  2. Change root password, create new user with sudo privileges
  3. Update packages
  4. Install and set up Apache
  5. Install a firewall
  6. Install MySQL
  7. Install PHP 7
  8. Install PhpMyAdmin
  9. Install Webmin
  10. Install WordPress
  11. Migrate multisite to standalone site
  12. Install a Let’s Encrypt certificate
  13. Install Apache and php modules for Total Cache
  14. Install VSFTPD

1. Connect to server via SSH

On Windows, download the free Putty utility. On Linux, use a terminal:
ssh root@your.server.ip.address -p22

Check your hoster for your servers IP-address and the port to connect.

Alternatively, use ssh keys instead of password:
SSH login without password using SSH keys

2. Change root password, create new user with sudo privileges

Change the root password:
passwd

This command changes the password for the current user, which is fine now as you are probably logged in as root.

Create a new user:
adduser username

Add sudo priviledges:
usermod -aG sudo username

3. Update packages

Starting with Ubuntu 16.04, no need to use apt-get anymore, apt is enough.

Update the list of available updates:
sudo apt update

Upgrade the current packages:
sudo apt upgrade

Or combine both in one line:
sudo apt update && sudo apt upgrade

Possible problem with locales (perl warning)

If you run into the following warning (frequent with Hetzner hosting):

perl: warning: Setting locale failed.
[...]
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory

Check the following post for a solution:
Ubuntu: resolve the “perl: warning: Setting locale failed” problem

Now the server is ready to install LAMP. Two reference articles:

DigitalOcean: How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04
Tecmint: How to Install LAMP with Apache, PHP 7 and MariaDB 10 on Ubuntu 16.04 Server

4. Install Apache

sudo apt update
sudo apt install apache2

Or combine both in one line:
sudo apt update && sudo apt install apache2

Test if Apache is running:
sudo systemctl status apache2.service

Use the –no-pager and –full options to avoid horizontal scrolling
sudo systemctl status apache2.service --no-pager --full

Use systemctl to check if Apache is running on a LAMP stack.

 

Hit Q to exit systemctl.

Solution for the following error:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name

The digital Ocean guide has a solution, see “Set Global ServerName to Suppress Syntax Warnings”

Test Apache in browser:
http://your.server.IP.address

If you need to, restart the Apache server using the following command

sudo systemctl restart apache2

Apache SSL modules

Check Tecmint’s guide to enable Apache SSL modules.

5. Install ufw firewall

Install a firewall:
sudo apt install ufw

Make sure the firewall will let through SSH connections (important!) and Apache traffic. Ufw has presets for both. Display the presets for these applications:
sudo ufw app list

To get more information on a profile, use:
sudo ufw app info "Apache Full"

Allow OpenSSH and Apache connections:
sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"

Check if everything is ok:

sudo ufw status

Now that SSH is allowed, enable the Firewall (confirm with “y”):
sudo ufw enable

Activating Ufw Firewall on remote server

6. Install MySQL

Install MySQL with the following command:

sudo apt-get install mysql-server

The installation will ask you to set a “root” password – don’t use the same as your servers root password! Continue with the following command:

mysql_secure_installation

This command is explained in details in the excellent Digital Ocean guide by Brennen Bearnes:
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

Check whether MySQL server is running with the following command:

sudo service mysql status

Stop and start the MySQL server with:

sudo service mysql stop
sudo service mysql start

MySQL is installed, now it’s php’s turn.

7. Install PHP

Make sure which version of php you want to install. Google differences between version (i.e. 5.6, 7.0, 7.1, etc) and check compatibility with your WordPress addons.

Following command will install Ubuntus default php version (7.0):

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

Install the following php modules:

php-gettext php-curl php-zip mcrypt php-gd php-mbstring php-mcrypt php-xmlrpc

One-in-all command: here is the command to install php 7.0 with all modules:

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-gettext php-curl php-zip mcrypt php-gd libgd-tools php-mbstring php-mcrypt php-xmlrpc

Check the DigitalOcean article to change the order in which php reads extensions in /etc/apache2/mods-enabled/dir.conf

In the same article, read how to check php’s functionality.

You’re now done installing LAMP.

8. Install PhpMyAdmin in Ubuntu 16.04

Follow this guide to install PhpMyAdmin securely:
Digital Ocean: How To Install and Secure phpMyAdmin on Ubuntu 16.04

9. Install Webmin

Follow this guide to install webmin:
Digital Ocean: How To Install Webmin on Ubuntu 16.04

Don’t forget to allow the 1000 port with ufw:
sudo ufw allow webmin

10. Install WordPress

Use this Digital Oceans guide to install WordPress:
How To Install WordPress with LAMP on Ubuntu 16.04

However, the following method for creating MySQL users and privileges is deprecated:

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

User the following two lines instead:

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';

Check priviledges with:

show grants for 'jeffrey'@'%';

Extract Multisite to standalone WordPress

Of course you should do a full backup of your old website. But there is more than that:

On the website you want to move:

  • Install the Widget Importer & Exporter Plugin to backup all widgets (else they won’t be migrated)
  • Deactivate Jetpack photon
  • Deactivate other plugins?
  • Deactivate permalinks (?)
  • Deactivate caching plugins

Follow the guide:
Envato tuts+: Moving WordPress: Moving a Site Out of a Multisite Network

Export SQL tables – Fix path and prefixes

Export SQL Files with phpMyAdmin, as shown in guide.

Once exported, replace occurences of subsite table prefixes and path in exported file:

/sites/4 remplacer par RIEN
eva_4_ remplacer par eva_

For big SQL dumps, use the command line sed tool:

sed -i 's/eva_4_/wp_/g' "dbvyeaia(3).sql"

For the path, use another sed separator:

sed -i 's_/sites/4__g' "dbvyeaia(3).sql"

Recover admin rights for any user:
wpbeginner: How to Add an Admin User to the WordPress Database via MySQL

Import SQL tables – Problems with too big files

Import SQL Files with phpMyAdmin.

If the SQL file is too big, modify the php.ini located in:
/etc/php/7.0/apache2/php.ini

sudo nano /etc/php/7.0/apache2/php.ini

Change the following lines:

post_max_size = 8M
upload_max_filesize = 8M

Then restart Apache:

sudo /etc/init.d/apache2 restart
sudo service apache2 restart

Redirect domain name to IP to finish installation

Before the site goes live, you can change it locally through its ip address without being redirected to the old site.

Edit the hosts file on local computer (NOT remote server):

sudo nano /etc/hosts

Add the following line:

88.198.159.174 ethische-banken.de

Redirect www to non-www domain

Edit the .htaccess file

sudo nano /var/www/html/.htaccess

Add the following block:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# BEGIN WordPress

Install a Let’sEncrypt certificate

Add the Server Name in Virtual host

List existing virtual hosts with:

apache2ctl -S

Add the server name in the defaut Apache virtual server (or set up a new virtual server for that purpose). Check the DigitalOcean guide for more info.

nano /etc/apache2/sites-enabled/000-default.conf

Restart Apache to apply changes:

sudo systemctl restart apache2

Add Servername in Apache2conf

sudo nano /etc/apache2/apache2.conf

Add following line at the end:

ServeName example.com

Install Let’s encrypt

To be able to use add-apt-repository, install the following packages:

sudo apt-get install software-properties-common

Reference article:
Digital Ocean: How To Secure Apache with Let’s Encrypt on Ubuntu 16.04

To set up the cron job, first choose nano as standard editor: else you’ll have to edit with vi!

select-editor

Then you can set up the cron job:

sudo crontab -e

43 4 * * 2 /usr/bin/certbot renew --quiet

Example: every Tuesday on 4:43

Before the site goes live

  • Change back hosts file edit on local computer (NOT remote server):

nano /etc/hosts

Remove the following line:

88.198.159.174 ethische-banken.de

  • Change back php.ini, disallow too big files
  • Reactivate permalinks, photon, plugins

Install Apache modules for Total Cache

Install the apache modules mod_expires, mod_headers, mod_ext_filter

sudo a2enmod expires

sudo a2enmod headers

sudo a2enmod ext_filter

The restart Apache service:

sudo service apache2 restart

Install PHP modules for W3 Total Cache

DON’T do this:

sudo apt install memcached php-memcache

sudo service memcached restart

sudo update-rc.d memcached enable

Because it does not work? (or slows up your site!)

Install VSFTPD

sudo apt-get install vsftp

sudo nano /etc/vsftpd.conf

Add or uncomment all these lines  to vsftpd.conf:

anonymous_enable=NO

local_enable=YES

write_enable=YES

ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO

pasv_min_port=4242
pasv_max_port=4243

tcp_wrappers=YES
local_root=/var/www/html

Then allow these ports for ufw:

sudo ufw allow ftp-data

sudo ufw allow ftp

sudo ufw allow 2121:2142/tcp

Restart deamon:

sudo /etc/init.d/vsftpd restart

Try this it it does not work:

sudo ufw allow 990/tcp

Useful commands

sudo netstat -tlpn

Posted in Linux, Wordpress.

Post a comment