How to: Deploy a basic wordpress on Ubuntu 16.04 LTS on Azure

How to deploy a simple Wordpress instance using Ubuntu 16.04 LTS, Nginx, MariaDB and Memcache in Azure.

This article is destined to help understand a basic Wordpress deployment on Azure using Ubuntu 16.04 LTS, Nginx with PHP-fpm (for PHP support), MariaDB and Memcache

In this example, we are deploying a Standard_F2S VM with premium disk, 2 CPU’s, 4GB of memory and also a local 7GB SSD (Ephemeral disk) which we will use for swap configuration (4GB).

Considering you already have  your VM deployed with the latest Ubuntu 16.04 LTS, the steps we will describe are:

1) Installing Nginx
2) Installing MariaDB
3) Installing PHP7 / PHP-FPM / Memcache
4) Installing Wordpress
5) Enabling Memcache

1) Installing Nginx

sudo apt-get update
sudo apt-get install nginx

You can verify Nginx is correctly listening on port 80 by issuing:

sudo netstat -anp | grep :80
tcp        0      0    *               LISTEN  XXXX/nginx –g daemo

This should indicate that Nginx is listening on port 80 on all IP’s

2) Installing MariaDB

sudo apt-get install mariadb-server mariadb-client

You will be asked to provide a password for the mariadb root account, please make sure you note that down.

NOTE: We highly recommend using the command “sudo mysql_secure_installation” but keep in mind that doing that will add a validation plugin for MySQL passwords which require certain levels of security where you can choose from LOW to STRONG.

3) Installing PHP / PHP-FPM / Memcached

sudo apt-get install php7.0-fpm
sudo apt-get install php-memcache memcached
sudo apt-get install php7.0-mysql php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-xml php7.0-xmlrpc

Turn off cgi.fix_pathinfo in php7.0-fpm:

sudo sed -i.bak 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g'  /etc/php/7.0/fpm/php.ini
sudo systemctl restart php7.0-fpm.service

4) Installing Wordpress

  1. Connect to MySQL and create a database and a user:
    mysql -u root -p
    CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    GRANT ALL ON wordpress.* TO ‘wpuser’@’localhost’ IDENTIFIED BY ‘password’;

  2. sudo –i
    cd /var/www/html
    curl –O
    tar xzvf latest.tar.gz
    cd wordpress
    mv * ..
    rm -rf latest.tar.gz wordpress
    chown –R www-data:www-data /var/www/html
    chmod g+w /var/www/html/wp-content
    chmod g+w /var/www/html/wp-content/themes
    chmod g+w /var/www/html/wp-content/plugins

  3. Setup the Wordpress configuration file
    curl –s
    copy the content from the output
    vi /var/www/html/wp-config-sample.php

  4. Replace the lines below with the output from the output above: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');

  5. Rename the config file:
    mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php

  6. Edit the file again to configure MySQL and replace the variables with the corresponding values:define('DB_NAME', 'wordpress' );
    /** MySQL database username */
    define('DB_USER', 'wpuser' );
    /** MySQL database password */
    define('DB_PASSWORD', 'password' );

  7. Edit Nginx default website configuration:
    vi /etc/nginx/sites-available/default

  8. Replace the location / area in the file with the content below, including the other block:

location / {               
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;

location ~ \.php$ {               
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Check to see if nginx configuration is correct and if it is, restart the service:

nginx –t
systemctl restart nginx

Assuming you have a Network Security Group with a rule to allow port 80 inbound to the VM, you can then access Wordpress from the external IP of the VM and finish the setup:

5) Enabling MemCache

sudo -i
cd /tmp
apt-get install unzip
cd memcached
cp object-cache.php /var/www/html/wp-content
chown www-data:www-data /var/www/html/wp-content/object-cache.php

To check stats in memcached you can use:
watch "echo stats | nc 11211"

After finishing the setup through the web interface in Wordpress you should be able to access the website at the https://IP address or the management interface at https://IP/wp-admin using the user/password that you configured during the Wordpress installation.

Happy blogging!