In this tutorial, we are going to provide you with step-by-step instructions on how to install WordPress with Nginx and W3 Total Cache on an Ubuntu 16.04 VPS.
WordPress is the most popular open source CMS written in PHP that allows web developers to create everything from simple websites to complex e-commerce web stores, news portals, magazines etc.
This tutorial was tested and written for an Ubuntu VPS, but it should work on any Debian based Linux distribution.
At the time of writing this tutorial, the latest stable version of WordPress is 4.7.2 and we recommend to install and use:
- PHP version 7 or higher with the GD graphics library version 2.0.x+ and MySQLi PHP extensions enabled, with memory_limit PHP variable set to 64 MB or higher (128 MB or higher is preferred)
- Apache or nginx web server;
- MySQL version 5.6 or higher or MariaDB version 10.0 or higher.
Let’s start with the installation.
Update OS packages:
Make sure your package list and the OS packages are up to date by running the following commands:
sudo apt-get update sudo apt-get upgrade
To install the latest nginx version on your virtual server from the official nginx repository, edit the ‘/etc/apt/sources.list’ file:
sudo vi /etc/apt/sources.list
Add the following lines to it:
deb http://nginx.org/packages/ubuntu/ xenial nginx deb-src http://nginx.org/packages/ubuntu/ xenial nginx
Add the nginx GPG key:
wget http://nginx.org/packages/keys/nginx_signing.key cat nginx_signing.key | apt-key add -
Stop the Apache service and remove it:
sudo service apache2 stop sudo apt-get remove apache2
Install nginx on your virtual server using the following commands:
sudo apt-get update sudo apt-get install nginx
Configure the nginx service to automatically start on boot:
sudo /lib/systemd/systemd-sysv-install enable nginx
Install PHP and enable required PHP modules:
sudo apt-get install php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-imap php7.0-json php7.0-mbstring php7.0-mysql php7.0-opcache php7.0-pspell php7.0-readline php7.0-xml php7.0-mcrypt php7.0-zip sudo phpenmod mcrypt sudo phpenmod imap
Then, start with the WordPress installation procedure.
Download WordPress
Get the latest version of WordPress available at https://wordpress.org/download. Download it to a directory of your virtual server and extract it using the following commands:
cd /opt/ wget https://wordpress.org/latest.zip unzip latest.zip -d /var/www/html/
Create a new nginx configuration file and add the following virtual block for your domain name:
vi /etc/nginx/sites-available/your-domain.com.conf
Add the following lines:
server { listen 80; server_name your-domain.com www.your-domain.com; root /var/www/html/wordpress; index index.php index.html; access_log /var/log/nginx/your-domain.com-access.log; error_log /var/log/nginx/your-domain.com-error.log; charset en_us.UTF-8; location / { try_files $uri $uri/ /index.php?$args; } location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires max; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Then, edit the main nginx configuration file (/etc/nginx/nginx.conf) and add the following line before the end of the http block:
include /etc/nginx/sites-enabled/*.conf
Activate the new nginx block by creating a symbolic link between the sites-available directory and the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/your-domain.com.conf
Do not forget to replace your-domain.com with your actual domain name. Then, delete the ‘default’ nginx configuration file:
rm /etc/nginx/sites-enabled/default
Open the ‘/etc/php/7.0/fpm/pool.d/www.conf’ PHP configuration file and change the ‘listen’ variable:
change:
listen = /run/php/php7.0-fpm.sock
to
listen = 127.0.0.1:9000
Edit the ‘/etc/php/7.0/fpm/php.ini’ PHP configuration file:
vi /etc/php/7.0/fpm/php.ini
Add/modify the following settings:
max_execution_time = 300 max_input_time = 300 memory_limit = -1 post_max_size = 64M upload_max_filesize = 64M
The web server user (www-data) needs to be able to write to files and directories inside the ‘/var/www/html/wordpress’ directory, so it can easily be accomplished by executing the following command:
sudo chown www-data:www-data -R /var/www/html/wordpress/
Test the nginx configuration:
# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If the test is successful, restart php7.0-fpm and Nginx services for the changes to take effect:
sudo service php7.0-fpm restart sudo service nginx restart
Create new MySQL user and database:
WordPress requires a database to work as this is where data is saved, so create a new MySQL database on your server:
mysql -u root -p mysql> SET GLOBAL sql_mode=''; mysql> CREATE DATABASE wpdb; mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'y0ur-pAssW0RD'; mysql> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost'; mysql> FLUSH PRIVILEGES; mysql> quit
Copy the WordPress sample config file (wp-config-sample.php):
cd /var/www/html/wordpress/ cp wp-config-sample.php wp-config.php
Set up the MySQL database information:
vi wp-config.php
define('DB_NAME', 'wpdb'); define('DB_USER', 'wpuser'); define('DB_PASSWORD', 'y0ur-pAssW0RD');
Do not forget to replace ‘y0ur-pAssW0RD’ with a strong password.
To install WordPress, open http://your-domain.com using a web browser and follow the easy instructions: select a language, create an administrator user account, enter your email address and click ‘Install WordPress’.
Log in to the WordPress administrator backend, go to plugins, search for ‘W3 Total Cache’ and install the W3 Total Cache plugin. Then, install a theme and other plugins of your choice, and configure WordPress according to your needs.
That is it. The WordPress installation is now complete.
Of course, you don’t have to do any of this if you use our Software Installation Service, in which case you can simply ask our expert Linux admins to install and configure WordPress with nginx, install W3 total cache plugin etc. for you. They are available 24×7 and will take care of your request immediately
PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a comment. Thanks.