All softwares installed are latest in this guide while publication of this guide. This guide is consistent with WordPress official documentation on Nginx, but tweaks are not included. This Step by Step Guide on How to Install WordPress on Ubuntu 16.06, Nginx, PHP7-FPM, memcached & MySQL on Cloud Server or VPS is Intended For the New Users. The Older How to Install WordPress on Ubuntu 14.04 LTS with Nginx Guide is a Complete Guide and Referenced in WordPress Documentation. But do not follow it right now, instead follow this guide. PHP5 and PHP7 has differences.
W3 Total Cache is enough to handle all the caches. If you cache those Static HTML pages with Nginx Microcache, then you’ll need their WordPress Plugin. Do not copy-paste from here and there without understanding. If you follow our plus WordPress DOT ORG’s codex.wordpress.org
settings for Nginx; you will not face any trouble. Nginx Microcache is not shown to enable in this guide.
How to Install WordPress Ubuntu 16.04 : SSH for 100% New Users
---
Microsoft Windows will use PuTTY and GNU/Linux users and OS X users will use Terminal, Terminal or iTerm2 respectively.
If the command line interface feels bad, during configuration to install WordPress you can use FileZilla or Command Line File Browser to edit or see the files.
We are starting after you login to your server after typing the password, get authenticated upon running the command :
1 2 3 | ssh username@IP_address # Example # ssh root@127.0.0.1 |
We used a Cloud Server instance for This How to Install WordPress on Ubuntu 16.04, Nginx, PHP7-FPM Installation Guide. Dedicated Server’s commands will be same some basic security steps is mandatory. We omitted that part. Basic Philosophy of How to Install WordPress on Ubuntu 16.04 Guide is to :
- Install webserver software Nginx.
- Install MySQL
- Install PHP
WordPress will run top of it.
How to Install WordPress Ubuntu 16.04 : Nginx Extras Webserver Software
We use nginx-extras from Ubuntu’s repository because Nginx Extras has important updated functions needed for WordPress like fastcgi cache purge module. We use the default paths. So, here we go, type these commands one by one :
1 2 3 4 | apt update apt upgrade apt upgrade apt install nginx-extras |
Edit /etc/nginx/nginx.conf
:
1 | nano /etc/nginx/nginx.conf |
modify error_log
to :
1 | error_log /var/log/nginx/error.log warn; |
Open your server IP on a browser, you’ll see the default Nginx webpage. Easy step.
How to Install WordPress Ubuntu 16.04 : Percona MySQL
Heart of WordPress is Database. It is most difficult part. Instead of original MySQL, we use Percona MySQL with innodb Engine. Percona MySQL and normal SQL has no difference in commands, it is a special optimized version. You can easily optimize later. These will not be written by somehow written guides by Digital Ocean or Linode. Percona MySQL is preferred as Percona has free tuning service.
1 | https://www.percona.com/doc/percona-server/5.6/installation/apt_repo.html |
Run these commands :
1 2 3 4 5 6 | gpg --keyserver hkp://keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A gpg -a --export CD2EFD2A | apt-key add - sudo sh -c 'echo "deb http://repo.percona.com/apt xenial main" >> /etc/apt/sources.list.d/percona.list' apt update sudo apt install percona-server-5.7 sudo mysql_secure_installation |
You can optimize it later using Percona’s service and Major Hayden’s MySQL Tuner. We prefer InnoDB engine. Running sudo mysql_install_db
consumes a time, it creates all the needed directories and settings files. sudo mysql_secure_installation
needed to ensure security. Now login to MySQL server and run the commands specific for Percona (you’ll see they are instructed to run) :
1 2 3 4 5 6 7 8 9 | mysql -u root -h localhost -p # run after login CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'; CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'; CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'; # you should exit exit # then restart mysql service mysql restart |
Now, again login and create database, add password etc. yourserver2016
, wordpress2016
, BAD_PASSWORD__change
are my own custom phrases, you should change to yours difficult to guess combinations :
1 2 3 | mysql -u root -h localhost -p CREATE USER 'yourserver2016'@'localhost' IDENTIFIED BY 'BAD_PASSWORD__change'; CREATE DATABASE wordpress2016; |
MySQl server’s root with password while installing MySQL ALWAYS can access the database. We add user with a password to separate the chance of attack, we sometimes never create a user if one MySQL has one database on different server. This step is for higher security, MySQl server’s root with password would work to install WordPress. The extra steps :
1 2 3 | GRANT SELECT,DELETE,INSERT,UPDATE ON wordpress2016.* TO 'yourserver2016'@'localhost'; GRANT ALL PRIVILEGES ON wordpress2016.* TO 'yourserver2016'@'localhost'; FLUSH PRIVILEGES; |
MySQL need no PHPMyAdmin to manage. PHPMyAdmin is good for 100% new users. PHPMyAdmin will eat RAM and increase security risks.
1 2 | mysql -u root -h localhost -p show databases; |
After completion of this step, you can run a cat command on /etc/mysql/my.cnf
. You can optimize it later like using some basic tweaked and ready to my.cnf from our GitHub repo. Percona has no my.cnf by default. Percona MySQL is for the advanced users. Most important to read how to “enable” Auto Restart of MySQL. Otherwise MySQL will not Auto-Restart on failure and throw infamous Error Establishing a Database Connection message.
1 2 3 | echo " " > /etc/mysql/my.cnf cd ~ && wget https://raw.githubusercontent.com/AbhishekGhosh/Nginx-PHP5-FPM/master/percona-mysql-server/fail-safe-my.cnf cat fail-safe-my.cnf > /etc/mysql/my.cnf |
If fails to restart, then check :
1 | cat /var/log/mysql/error.log |
We have finished installing MySQL database part.
How to Install WordPress Ubuntu 16.04 : PHP 7.0.7
We install some PHP version specific packages for full support of all plugins – common, mysqlnd, xmlrpc, curl, gd, cli, fpm, pear, dev, imap, mcrypt, snmp, intl, imagick, ming, ps, pspell, recode, snmp, mysql, sqlite, tidy, xsl, libssh2, memcached.
This is minimum you should run :
1 2 | sudo add-apt-repository ppa:ondrej/php apt install libcurl3 libmcrypt4 libmemcached11 libxmlrpc-epi0 php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-intl php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-readline php7.0-xml php7.0-xmlrpc psmisc libmcrypt-dev mcrypt php-pear php-mysql php-mbstring php-mcrypt php-xml php-intl libmhash2 php-common |
The above command is cross tested. It will not give any warning and start installing.
1 | nano /etc/php/7.0/fpm/php.ini |
Find cgi.fix_pathinfo
. This will be commented out with a semi-colon (;) and set to “1” by default. We will change both of these conditions by uncommenting the line and setting it to “0” like this:
1 | cgi.fix_pathinfo=0 |
Now, edit default nginx file with sudo nano /etc/nginx/sites-available/default
command and make it like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | server { listen 80 default_server; root /usr/share/nginx/html; index index.html index.htm index.php; server_name localhost; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; # fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock; include fastcgi_params; } } |
Then restart PHP and Nginx :
1 2 3 | sudo systemctl restart php7.0-fpm # sudo service php7.0-fpm restart service nginx restart |
Now, delete the default file in /usr/share/nginx/html
and add PHP file :
1 2 3 | cd /usr/share/nginx/html rm -r * nano index.php |
Fill with this content :
Final Steps to Install WordPress
We are doing the next steps logically :
1 2 3 4 5 6 7 | cd /usr/share/nginx/html && rm -r * curl -O https://wordpress.org/latest.tar.gz && tar -xzxf latest.tar.gz rm latest.tar.gz && cd w* mv * .. && cd .. rm -r wordpress cp /usr/share/nginx/html/wp-config-sample.php /usr/share/nginx/html/wp-config.php rm /usr/share/nginx/html/wp-config-sample.php |
Open /usr/share/nginx/html/wp-config.php
and add define('FS_METHOD', 'direct');
after database details paragraph to avoid file uploading issue in WordPress :
1 2 3 4 5 | /** MySQL database password */ define('DB_PASSWORD', 'password'); ... ... define('FS_METHOD', 'direct'); |
Now, we need to set the permissions and ownership properly :
1 2 3 4 5 | sudo chown -R root:www-data /usr/share/nginx/html sudo find /usr/share/nginx/html -type d -exec chmod g+s {} \; sudo chmod g+w /usr/share/nginx/html/wp-content sudo chmod -R g+w /usr/share/nginx/html/wp-content/themes sudo chmod -R g+w /usr/share/nginx/html/wp-content/plugins |
Open your server IP on a browser, you’ll see the default WordPress Web GUI for Installation. Easy steps, written in WordPress official website too.
Example Nginx Configuration is on Official WordPress Docs
Now you need some software for monitoring OpCache and Memcached. These are optional steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 | cd /usr/share/nginx/html rm index.html index.php mkdir mystat && cd mystat mkdir opcache memcache cd opcache wget https://raw.githubusercontent.com/rlerdorf/opcache-status/master/opcache.php cd .. && cd memcache wget https://github.com/AbhishekGhosh/phpmemcacheadmin/archive/1.3.tar.gz tar -xzvf 1.3.tar.gz && rm 1.3.tar.gz cd php* && mv * .. cd .. && rm -r phpmem* chmod +r * chmod 0777 Config/Memcache.php |
You will get the opcache
, memcache
status in this kind of URLs :
1 2 | http://your_IP_or_domain/mystat/opcache/opcache.php http://your_IP_or_domain/mystat/memcache/index.php |
Password protect the directory with nginx. It is security risk to keep them openly available. Obviously it is your next step, not mandatory. Official Nginx configuration including the needed for W3 Total Cache is written here :
1 | https://codex.wordpress.org/Nginx#Better_Performance_for_Static_Files_in_Multisite |
You will again referred to our website from the official listed guides there. Ask on StackExchange or WordPress Forum for help for any issue. You will probably install W3 Total Cache. This guide is compatible with W3 Total Cache, WP Super Cache.
Memcached?
PHP Memcached may fail. Obviously, should not fail. For that reason not added in this guide. Who will install, should be used with memcached, install via apt-get -y install memcached
. You will see this works. Test with :
1 | echo stats | nc 127.0.0.1 11211 |
This is “original” memcached, and wiki is at – https://github.com/memcached/memcached/wiki
.
1 | echo stats | nc 127.0.0.1 11211 |
This is “original” memcached, and wiki is at – https://github.com/memcached/memcached/wiki
.
What is Next?
We have not installed mail server. You can use free tier of Zoho for domain named email. But you need some easy free cloud transactional email service like SendGrid with WordPress Plugin. Otherwise you can not receive email via contact us form. Installing Postfix like self hosted software as mail server not recommended for security. DNSSEC should be configured.
You can install Let’s Encrypt free SSL certificate. You can use KeyCDN or CacheFly CDN, use Dyn DNS for faster DNS resolution.
How to Install WordPress Ubuntu 16.04 With Automation
We can automate this whole guide with primitive bash script to DevOps tools including Anscible. You may read our Ansible tutorial to create WordPress Nginx Playbook for Ubuntu 16.04 or use bash script to install WordPress-nginx on Ubuntu 16.04.
Tagged With wordpress ubuntu 16 04 , wordpress ubuntu 16 , [keywprd] , wordpress installation 16 04 language:en , ubuntu 16 04 nginx php7 , ubuntu 16 04 php5 6-memcached , ubuntu 16 04 install nginx php7 0 , ubuntu 16 04 php7 0-fpm howto , ubuntu wordpress , how to install wordpress to ubuntu for windows