Blog

Migrating a WordPress Site to VDS with NGINX

Learn to migrate your WordPress site to a VDS with NGINX for improved performance and customization.

Dmytro
VDS migration NGINX WordPress Linux server PHP-FPM database management web server configuration

Migrating a WordPress Site to VDS with NGINX

Despite the existence of commercial and free server control panels, in certain cases it makes sense to use a custom solution and host websites on a manually configured server or VDS. A control panel requires resources to function and does not always allow for fine-tuning.

If you can use the Unix command line and text editors for configuration files, let’s migrate a WordPress blog to a VDS or dedicated server while speeding up our website.

Setting Up the VDS

First, order an SSD VDS with CentOS as the OS. In about 5 minutes, the server will be ready for configuration.

Update installed packages and remove default iptables rules:

iptables -F
service iptables save
yum -y update

Change the iptables settings and restart the SSH server:

sed -e 's/^#Port 22$/Port XXXX/g' -ibak /etc/ssh/sshd_config
service sshd restart

Verify access using the new SSH port before rebooting the VDS:

reboot

Installing Necessary Components

Install PHP-FPM, NGINX, and MySQL:

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum --enablerepo=remi install nginx mysql mysql-server php-fpm php-mysql php-gd
service mysqld start

Set the MySQL root password and secure the installation:

/usr/bin/mysql_secure_installation

MySQL Secure Installation Steps

  1. Enter current password for root (press enter for none)
  2. Set root password
  3. Remove anonymous users
  4. Disallow root login remotely
  5. Remove the test database
  6. Reload privilege tables

Create Website Directory

Create the directory for the website:

mkdir -p /var/www/blog.net

Create the virtual host configuration file for NGINX:

server {
    listen 80;
    server_name blog.net www.blog.net;

    root /var/www/blog.net;
    index index.php index.html index.htm;

    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 4;
    gzip_http_version 1.0;
    gzip_min_length 1280;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp;
    gzip_vary on;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        root /var/www/blog.net;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        include fastcgi_params;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 30d;
        log_not_found off;
    }
}

Configure PHP-FPM

Change the user in PHP-FPM configuration:

sed -e 's/ = apache$/ = nginx/g' -ibak /etc/php-fpm.d/www.conf

Enable auto-start for necessary components:

chkconfig --levels 235 mysqld on
chkconfig --levels 235 nginx on
chkconfig --levels 235 php-fpm on
service php-fpm start
service nginx start
echo "<?php phpinfo(); ?>" > /var/www/blog.net/phpinfo.php

Access http://ip-address-of-server/phpinfo.php to verify PHP setup.

Create and Configure Database

Create the database (replace XXXX with the MySQL password):

mysqladmin -uroot -pXXXX create blogdb

Add a new user with privileges:

CREATE USER 'bloguser'@'localhost' IDENTIFIED BY 'PaSsWoRd';
GRANT ALL PRIVILEGES ON blogdb.* TO 'bloguser'@'localhost';
FLUSH PRIVILEGES;

Set File Permissions

Set the correct permissions:

chown -R nginx:nginx /var/www/

Check website functionality and point your domain to the IP. Enjoy the improved speed!

Addendum

Consider disabling certain PHP functions for security:

php_admin_value[disable_functions] = dl,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

Set security.limit_extensions = .php .php3 .php4 .php5 in PHP-FPM configuration.

Need Help?

Our support team is available 24/7 to assist you with any questions or issues.

Contact Support