This guide shows how to install WordPress with Nginx on Ubuntu 14.04 with step by step commands and explanation of the steps of what we are doing. We usually publish a video guide separately which are available on YouTube and iTunes as Podcast. The reader should know minimum about nginx HTTP Server. This guide is intended for new nginx users, therefore we have not underwent complex steps or further tweak for optimization. One can use either an one GB server or even a 512 MB server to run WordPress with nginx – it is impossible with Apache to run a bigger site with lower RAM, but ngnix can handle efficiently.
Install WordPress with Nginx : Forewords
Nginx can be used for reverse proxying too. In this guide we are assuming that the reader has a WordPress website running on Apache server. So, we need to install WordPress with Nginx on Rackspace cloud server freshly. If one need to migrate, he/she have to upload the uploads and other contents on nginx server, import the posts, category and pages with WordPress default XML export-import feature. This will fully avoid any problem with permalink structure. We have kept the settings of nginx to default. There is no reason to make it complex by changing the default settings.
Install WordPress with Nginx on Rackspace Cloud Server
First, spin a server instance of your desired size. Save the password in some text editor. Copy the public IP address of the server. After the server is up and running; SSH to the server as root :
---
1 | ssh root@your_IP_address |
1 2 3 | The authenticity of host '169.55.55.2 (169.55.55.2)' can't be established. ECDSA key fingerprint is 89:95:46:1b:ab:37:11:8e:86:54:36:38:bb:3c:fa:c0. Are you sure you want to continue connecting (yes/no)? |
Save the key by hitting y as prompted. Update and upgrade the instance :
1 | apt-get update && apt-get upgrade |
For 512 MB instance, you might face a text based interface for boot configuration prompting you to select which one to keep. Simply hit the Return / Enter key – we will keep the things as suggested, to default.
Now, compare the installation method we do for installing WordPress with Apache, on Ubuntu 14.04 on Rackspace Cloud Server. Here we will install latest version of nginx first :
1 2 3 4 | apt-get install python-software-properties add-apt-repository ppa:nginx/stable apt-get update apt-get install nginx |
and then all the needed components of PHP to run WordPress :
1 | sudo apt-get install php5-fpm |
Now we need to configure nginx. Basically we need not to touch the config file :
1 2 | /etc/nginx/nginx.conf # read http://wiki.nginx.org/NginxFullExample |
We only need to edit the /etc/nginx/sites-available/default
file, which is somewhat equivalent to Apache+Ubuntu’s /etc/apache2/sites-available/000-default.conf
file. Open it :
1 2 3 | nano /etc/nginx/sites-available/default # write out with ^ + O # exit with ^ + X |
There will be lot of commented out lines, you must have active file looking like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; # Make site accessible from http://localhost/ # use a full domain name server_name localhost; location / { try_files $uri $uri/ /index.php /index.php?q=$uri&$args; } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { # With php5-fpm: try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root had # location ~ /\.ht { deny all; } } |
Carefully compare what I have changed. Give a bit time, it is worthy.
do a reload of nginx :
1 | service nginx reload |
Install everything we will need for running WordPress plus xCache for caching :
1 | apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xcache |
Make sure that we have lib ssh :
1 | apt-get install php5-gd libssh2-php |
restrat both ngnix and php-fpm :
1 2 | service php5-fpm restart service nginx restart |
Now, compared to Apache, our document root is at /usr/share/nginx/html
; this is for a philosophical matter of Deb based distro. However, we think, we should not use www as the path anymore, because that was out of a mistake and copy of mistake. The permission at /usr/share/nginx/html
is almost perfect and secure.
Now install MySQL server and Client (client is optional) :
1 | apt-get install mysql-server mysql-client |
type a password for MySQL root user and remember it. Read the MySQL
1 2 3 4 5 6 7 8 9 | mysql -u root -p # change wordpress, it is an example CREATE DATABASE wordpress; # wordpressuser should be changed, it is example, so is password CREATE USER wordpressuser@localhost IDENTIFIED BY 'password'; # we will use localhost GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost'; FLUSH PRIVILEGES; exit |
Now delete the html file at root :
1 | rm /usr/share/nginx/html/index.html |
You can test PHP by creating an index.php file at root :
1 | nano /usr/share/nginx/html/index.php |
Fill up with :
If you point to your IP address, you’ll get the PHP info page. Delete it before installing WordPress :
1 | rm /usr/share/nginx/html/index.php |
Now, go to html directory :
1 | cd /usr/share/nginx/html/ |
Download WordPress and untar :
1 2 3 4 5 6 | wget http://wordpress.org/latest.tar.gz tar -xzvf latest.tar.gz cd wordpress mv * .. && cd .. rm -r wordpress latest.tar.gz && ls # see the files |
Edit wp-config :
1 | cp wp-config-sample.php wp-config.php && nano wp-config.php |
You will need to edit these :
1 2 3 4 5 6 7 8 9 | // ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpressuser'); /** MySQL database password */ define('DB_PASSWORD', 'password'); |
Run the WordPress installation script by visiting the IP or domain name. We need one more component to install, otherwise, you will lot of error if you run php5-fpm -v command.
1 2 3 | sudo apt-get install snmp # check php5-fpm -v |
Obviously, tweaking is required, but that is a separate chapter!