Install WordPress on Ubuntu

wp_logo

WordPress is one of (if not) the most popular Content Management System (CMS) in the world today. Using WordPress, it is quite easy to get started on blogging and since it has an active community around it, you will get regular updates and improvements as well.

You can start a blog using several hosted options like WordPress.com or Blogger/Blogspot for free. However, this comes at the cost of losing full control over your blog and most often prevents you from adding your own advertising in your blog in case you want to monetize it. Having your own WordPress installation provides total control on all aspects of your blog, like look and feel, plugins, and sometimes even better security.

This article assumes that you have already set up your server. We will also set up WordPress to run on the nginx web server instead of Apache.

Install MySQL

WordPress uses MySQL as its database. In Ubuntu, its pretty straightforward to install MySQL:

sudo apt-get install mysql-server

During the installation process, it will ask for the root password. Take note of this root password as you will need it to set up your application later on.

The configuration file is located at:

/etc/mysql/my.cnf

By default no configuration is needed and the MySQL service should be up and running. If you edited the configuration file or just need to restart the service in case of an issue:

sudo service mysql restart

In case you misplaced the root password, you can re-set the password using this command:

sudo dpkg-reconfigure mysql-server-5.5

Just replace the version number with the one installed in your system.

Install Nginx

Nginx is one of the most popular web servers today and is used by many high performance websites and web applications.

Note: If you are deploying Ruby on Rails applications in your server, you can skip this step and instead just use the installed Nginx via the passenger gem. More details can be found in this article.

The Ubuntu/Debian package manager provides a simple way of installing Nginx:

sudo apt-get update
sudo apt-get install nginx

If you are using Red Hat or CentOS, a similar package can be installed using this command:

sudo yum install nginx

If you want the maximum flexibility on installation options, then you can install Nginx from source using this guide from the official website.

Install PHP

PHP is the scripting language that WordPress is built upon, as well as many more web applications including Facebook. We will install PHP via the PHP-FPM module:

sudo apt-get install php5-fpm

We also need to install the PHP wrapper for the MySQL database that WordPress uses:

sudo apt-get install php5-mysql

Configure PHP

After that, we update some of the settings in our PHP installation.

First, we disable the feature that allows URLs to be “guessed” and return the closest match. While this can be useful for some cases, this can be a security hole allowing malicious users to execute arbitrary files in your system.

Open the configuration file and set cgi.fix_pathinfo to false:

sudo nano /etc/php5/fpm/php.ini

cgi.fix_pathinfo=0

Then we also set the correct path where the web server will listen to:

sudo nano /etc/php5/fpm/pool.d/www.conf

listen = /var/run/php5-fpm.sock

We also need to set the appropriate permissions so that PHP can read our WordPress files:

sudo nano /etc/php5/fpm/php-fpm.conf

Add the following at the end of the php-fpm.conf file and replace deploy with the user account you are using in the server:

listen.owner = deploy
listen.group = deploy

If you fail to do this, you may encounter this error when running the web server:

connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream

Restart the php-fpm program to apply the changes:

sudo service php5-fpm restart

Set up the WordPress server block

In the main Nginx configuration file, we can see this line:

include /etc/nginx/sites-enabled/*;

This means that Nginx also looks at the /etc/nginx/sites-enabled directory for additional configuration and includes files inside that directory. This is helpful in organizing server block configurations in the system especially if you are running multiple applications.

We then create the server block for WordPress under this directory:

sudo nano /etc/nginx/sites-enabled/wordpress.conf

In the wordpress.conf file, put in the following:

server { listen 80; server_name mydomain.com www.mydomain.com; root /var/www/blog; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/blog; } # pass the PHP scripts to FastCGI server listening on the php-fpm socket location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } read more