Install WordPress with Nginx

We will talk about how to configure PHP with Nginx and then we can use WordPress with Nginx. Here we will use PHP-7.0 with fpm. you can modify it according to current PHP version. These commands are tested on an Ubuntu system. You can change them accordingly.

Login to the instance where you want to do this setup.

Install PHP, Mysql and Nginx

$ sudo su
# apt-get update
# apt-get install vim
# apt-get install mysql-server nginx php7.0-fpm php7.0-mysql php-apcu \
    pwgen python-setuptools curl git unzip php7.0-curl php7.0-gd php7.0-intl \
    php-pear php-imagick php7.0-mcrypt php7.0-pspell php7.0-recode \
    php7.0-sqlite php7.0-xmlrpc php7.0-xsl

Tweak your php-fpm settings, and change the cgi.fix_pathinfo setting from 1 to 0

Increase the max upload filesize and post max size to 20MB:

$ sudo sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php/7.0/fpm/php.ini
$ sudo sed -i "s/upload_max_filesize =.*/upload_max_filesize = 20M/g" /etc/php/7.0/fpm/php.ini
$ sudo sed -i "s/post_max_size =.*/post_max_size = 20M/g" /etc/php/7.0/fpm/php.ini

Now configure Nginx’s default site by editing /etc/nginx/sites-available/default:

server {
        listen   80;
        listen   [::]:80 default ipv6only=on;
        root /var/www/wordpress;
        index index.php;
        client_max_body_size 20M;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                include fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

When you make changes to your configuration, you will need to restart the associated services:

$ sudo service nginx restart
$ sudo service php7.0-fpm restart

This configuration will serve php from /var/www/wordpress – drop an index.php file there with following code. Visit your instance’s IP address in your browser to confirm that everything has been configured – make sure you delete that file once you’re done.


Migrate existing WordPress with backup

Take backup of existing WordPress, use plugin BackWPup, to backup the files and database. Run the job manually on your machine, it will create a compressed file in “/wp-content/uploads/backwpup-**-backups]“.

Start a http server in that folder. It is possible that your firewall rules doesn’t allow external traffic on other ports. Better you check Firewall rules or transfer it through “SCP”.

$ python -m SimpleHTTPServer 3389

Goto the new machine where the nginx and php is installed.

$ cd /var/www/wordpress
$ wget http://ip:3389/wordpress.tar.gz
$ tar -xvzf wordpress.tar.gz
$ chown -R www-data:www-data
  1. Extract content of this folder on /var/www/wordpress/
  2. Login to Database with the root password entered during setup.
  3. Create Mysql user for wordpress.
# mysql -uroot -p

mysql> CREATE DATABASE wordpress_db;
mysql> CREATE USER 'wordpress-user'@'localhost' IDENTIFIED BY 'CHOOSSE_STRONG_PASSWORD';
mysql> GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress-user'@'localhost';
mysql> \q

Export sql file

mysql -uwordpress-user -p wordpress_db < wordpress.sql

Check DB credentials in “wp-config.php”.

Now open IP in browser and wordpress should be up and running.