How to Install Wordpress with Nginx on an Ubuntu 12.04 VPS

Overview

The following list of tasks is a general outline of what needs to be done to get WordPress up and running on your VPS:

  • Set up an Ubuntu 12.04 VPS (virtual private server)
  • Install and configure Nginx as your web server
  • Install and configure PHP5-FPM as PHP5 support
  • Install and configure MySQL as a database to store your WordPress content
  • Install and configure WordPress as your blogging platform

Set up an Ubuntu 12.04 VPS

  1. Go to Digital Ocean and sign up for a new VPS.

  2. Select an Ubuntu 12.04 droplet.Digital Ocean will email you the IP address and password to your new VPS. When you receive those, sign into your new server using the following command:
    ssh root@ip-address with the password provided.Once you access the server, you need to configure a number of settings.

  3. Change the root password.
    To set a new password for the root user, enter the passwd command and follow the instructions on screen.

  4. Create a new user with sudo permissions.
    You need to create a user with sudo permissions because running all services as root is a security risk, and bad practice. To create a new user with sudo permissions and a home directory, use the following example: * Create a new user with a home directory and bash shell:


useradd -m -s /bin/bash -d /home/user -U username

  • Give the user sudo permissions:
    sudo usermod -a -G sudo username * Set the new user password:
    passwd username

Updating your VPS and installing WordPress, Nginx, MySQL, and PHP

Now that you have set up a VPS with a new user, you need to run the following commands to begin setting up your system for a WordPress installation.

  • Download the latest version of system packages and install them on your VPS:
    sudo apt-get update&&sudo apt-get dist-upgrade

  • Install Nginx:
    sudo apt-get install nginx* Install WordPress: - Change to the home directory of the new user:
    cd /home/user - Install and extract the latest WordPress release:
    sudo wget http://wordpress.org/latest.tar.gz && sudo tar -xzvf latest.tar.gz* Install PHP5-FPM:
    sudo apt-get install php5-common php5-mysqlnd php5-xmlrpc php5-curl php5-gd php5-cli php5-fpm php-pear php5-dev php5-imap php5-mcrypt* Install MySQL:
    sudo apt-get install mysql-server php5-mysql * You will be asked to set the root password. Don't forget it, you will need it soon. Now that you have all the necessary packages installed, you need to configure them so that you can get WordPress running. #### Configure PHP5-FPM##### Increase the security of PHP5-FPM.To prevent PHP-FHM from serving close matches when requested files cannot be found, set the value of the cgi.fixpathinfo parameter to 1. This parameter is set in the following file: /etc/php5/fpm/php.ini ##### Tip: php.ini is a big file. To help find this parameter, use the following command to show the line of the file on which it is set:
    cat /etc/php5/fpm/php.ini | grep -nie cgi.fix_pathinfo -n: shows line number | -i: make search case insensitive | -e: search for following stringTo change the value of the cgi.fix
    pathinfo property, use the following steps:1. Open the php.ini file and change the value of cgi.fix_pathinfo=0 to cgi.fix_pathinfo=1:
    sudo nano -c /etc/php5/fpm/php.ini2. Save and exit.##### Configure PHP5-FHM to use UNIX sockets instead of TCP sockets.UNIX domain sockets should be faster than communication by loopback localhost connections because you have less TCP overhead.To set PHP5-FPM to use UNIX sockets, use the following steps:1. Open the www.conf file:
    sudo nano /etc/php5/fpm/pool.d/www.conf2. Change the listen parameter line: * From : listen = 127.0.0.1:9000 * To : listen = /var/run/php5-fpm.sock3. Save and exit.#### Configure a MySQL databaseTo create and configure a database to store your WordPress content, use the following steps:1. Activate the MySQL install:
    sudo mysql_install_db2. Secure and optimize your MySQL database.
    To do this, use the following command:
    sudo /usr/bin/mysql_secure_installation * After answering the password prompts, you can enter "yes" to all the options. This secures your MySQL installation against some vulnerabilities such as anonymous users and someone guessing your root login.Now that you have activated your MySQL installation you need to set up a MySQL database. To do this, use the following steps:1. Login to the MySQL shell:
    mysql -u root -pNote: All SQL commands end with a semi-colon ;2. Create a database for your WordPress installation:
    CREATE DATABASE wordpress; * where wordpress is the name of the database created.3. Create the database user:
    CREATE USER wpuser@localhost; * Where wpuser can be any user name you wish.4. Set a password for the new user:
    SET PASSWORD FOR 'wpuser'@'localhost'= PASSWORD("user_password");5. Grant all privileges to the new user:
    GRANT ALL PRIVILEGES ON wordpress.* TO wpuser@localhost IDENTIFIED BY 'user_password';6. Refresh MySQL:
    FLUSH PRIVILEGES;7. Exit MySQL
    EXIT#### Configure WordPress To configure WordPress, change to the WordPress install directory.* su username : switches to the new user * cd ~/wordpress
    In this directory you will see a file called wp-config-sample.php. You can use this file to create the wp-config.php file that WordPress needs to run. Once you create the wp-config.php file, you must configure it to access the MySQL database you just created. To do this, use the following commands:1. Copy the sample file to create the configuration file:
    sudo cp wp-config-sample.php wp-config.php2. Open the wp-config.php file:
    sudo nano wp-config.php3. Within this file set the following parameters: * Set the database_name_here variable to the name of the MySQL database you created. * Set the username_here variable to your database user name. * Set the password_here variable to your user database password4. Reduce the size of your WordPress database.
    WordPress autosaves posts while writing, which remain even after the post is saved in the wordpress database. To disable this function add the following line to the wp-config.php file:
    define('WP_POST_REVISIONS', false );5. Save and exitNow that you have configured WordPress, you need to allow Nginx access the files. Nginx runs on the VPS with the user name www-data, therefore to allow Nginx to serve files to the internet you must change the ownership of those files to www-data. To change your WordPress file owner settings, use the following steps: 1. Change directory to one level above your WordPress installation:
    cd ~2. Use the following command to change the ownership of all files and subdirectories in the wordpress directory:
    sudo chown -R www-data:www-data wordpress#### Configure NginxConfiguring Nginx is two separate stages.* Configuring Nginx to be more efficient * Configuring Nginx to serve your WordPress content##### Configuring Nginx to be more effecientTo configure Nginx to work optimally with your VPS, you need to make changes to the nginx.conf file located in /etc/nginx.Open the nginx.conf file with the following command:
    sudo nano /etc/nginx/nginx.confOnce opened, change or enable the parameter settings within the file as outlined in the following steps. Some of these settings are already in the file but are commented out. To enable these parameters, remove the '#' preceeding the parameter.1. Adjust the number of worker_processes to match the number of CPUs that you have available. This allows Nginx to use all available CPUs. Check the number of CPUs available with the following command: * cat /proc/cpuinfo | grep processor
    Using a server with 16 CPUs as an example:
    worker_processes 16;2. Increase the number of worker_connections if your site has high traffic. Theoretically, Nginx can handle
    max clients = worker_processes * worker_connections * worker_connections 10240;3. Change the default keepalive_timeout setting so that inactive connections are closed. * keepalive_timeout 30 15;
    These settings are in seconds. The first parameter closes connections after 30 seconds of inactivity. The optional second parameter assigns the time value in the header Keep-Alive: timeout=time of the response. This header can convince some browsers to close the connection, so that the server does not have to. 4. Enable compression using gzip on;, this is a filter that compresses responses using the gzip method. To enable compression, uncomment or configure the following parameters:gzip on;
    gzip_disable "msie6";
    gzip_comp_level 2;
    gzip_buffers 4 8k;
    gzip_http_version 1.1;``gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;5. Disable server_tokens. This prevents nginx from displaying server information. * server_tokens off;6. If you have a very long domain name, set the server_names_hash_bucket_size to a multiple of 64. * server_names_hash_bucket_size 256;##### Configuring Nginx to serve your WordPress contentTo configure Nginx to serve your content to the web, you need to get an Nginx virtual hosts file to point to your content. In this installation the WordPress content is located in ~/wordpress, where ~ represents the home directory of the user created at the beginning of the installation.To configure Nginx to serve your content, use the following steps:1. Change to the Nginx sites-available directory:
    cd /etc/nginx/sites-available2. To create the new file use the following command:
    sudo nano wordpress.conf3. Use the sample virtual hosts file below to create your own file. This example already contains the configuration for browser cache functionality, and FastCGI with PHP FPM.
    Ensure that you set the following parameters in it: * Set the root file path to your WordPress directory location. * Set the server_name address to you domain name or IP address.4. When you have set the parameters, save and exit the file.

server {# Set Nginx to listen for incoming HTTP requests      listen   80; root /home/user/wordpress;index index.html index.php index.htm;servername your.ip.address your.website.com;location / {tryfiles $uri $uri/ /index.html;}location /doc/ {alias /usr/share/doc/;autoindex on;allow 127.0.0.1;deny all;}errorpage 404 /404.html;    errorpage 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/www;}    # Set up browser caching for the following files    # .php .jpg .jpeg .png .css .ico .js .giflocation ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {    accesslog off;    expires max;  }    location ~* .php$ {    tryfiles $uri =404;    fastcgisplitpathinfo ^(.+.php)(/.+)$;fastcgipass unix:/var/run/php5-fpm.sock;fastcgiindex index.php;fastcgiparam SCRIPTFILENAME $documentroot$fastcgiscriptname;include fastcgi_params;}}

To enable Nginx to access this virtual hosts file, you must add a symbolic link in the sites-enabled directory. You should also remove the default symbolic link.1. Change to the sites-enabled directory:
cd /etc/nginx/sites-enabled2. Remove the default symbolic link:
sudo rm default3. Create a symbolic link to the wordpress.conf file you just created.
sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/wordpress.confThis means that to remove your content from the web, you need only delete the symbolic link.Nginx is now configured to serve your web content. To get your site up and running, use the following commands:* Restart PHP5-FPM:
sudo service php5-fpm restart* Restart nginx:
sudo service nginx restartTo access your WordPress site, use this link exampleyour.website.com/wp-admin/install.php"

Comments