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; } }
This means that when we go to our domain (http://mydomain.com), Nginx will look under the /var/www/blog directory and run it using PHP-FPM.
Download and install WordPress
Now we are ready to get the WordPress source files and are getting closer to running our own blog. Get the latest release to make sure you get the latest security updates and features:
wget https://wordpress.org/latest.tar.gz
Extract the files after the download is complete:
tar xvzf latest.tar.gz
Then copy the entire wordpress directory to /var/www/blog, where the Nginx server block root is located in the previous section:
sudo mv wordpress /var/www/blog
Create the MySQL database and user
WordPress needs a database to store all your posts, comments, and users. We need to create the MySQL database that wordpress will use, then create the database user and password. In this example we will use the following values (make sure you use a strong password instead of the example below!):
- Database Name: wordpress
- Database User: wordpressuser
- Database Password: wordpresspassword
Open the MySQL console using this command:
mysql -u root -p
Inside the console, create the database:
mysql> CREATE DATABASE wordpress;
Create the user:
mysql> CREATE USER wordpressuser@localhost;
Set the user’s password:
mysql> SET PASSWORD FOR wordpressuser@localhost= PASSWORD("wordpresspassword");
Allow the user we just created to have full access to the wordpress database:
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'wordpresspassword';
mysql> FLUSH PRIVILEGES;
Update the WordPress database configuration
Now that we have created the WordPress database and user, we now set this in the WordPress configuration file:
Copy the sample generated config file into the one we will use:
sudo cp /var/www/blog/wp-config-sample.php /var/www/blog/wp-config.php
Edit the wp-config.php file and update the values for the database name, user, and password:
sudo nano /var/www/blog/wp-config.php
/** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpressuser'); /** MySQL database password */ define('DB_PASSWORD', 'wordpresspassword');
Initial WordPress Settings
Now when you go to your domain http://mydomain.com, it should show your empty WordPress blog. Log in to WordPress using this URL:
http://mydomain.com/wp-login.php
After logging in, you will proceed to the Dashboard, where you can add new blog posts and pages, manage comments, and configure your blog.
On the sidebar, go to the Settings page to update your blog name, description, and URL:
Enter your domain name in the WordPress Address (URL) field. It is important that you initially set it to an http:// URL instead of https://, as it can cause you problems logging in if you set it immediately to https without properly configuring your server to support SSL/TLS.
Using an HTTPS domain
If you set your WordPress Address to an HTTPS URL and your server is not set up to accept HTTPS requests, it can cause redirect loops and other issues, preventing you from logging in to the Dashboard and updating the URL back to an HTTP URL. In case you get locked out of your own blog, you can make the URL change directly in the MySQL database.
Open the MySQL console using this command:
mysql -u root -p
When you get in the console, load the wordpress database we have created earlier:
mysql> use wordpress;
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
Then we look the current setting WordPress URL:
mysql> select * from wp_options where option_name = 'siteurl';
+-----------+-------------+-------------------------+----------+ | option_id | option_name | option_value | autoload | +-----------+-------------+-------------------------+----------+ | 1 | siteurl | https://mydomain.com | yes | +-----------+-------------+-------------------------+----------+ 1 row in set (0.01 sec)
Then we update that field with the HTTP URL:
mysql> update wp_options set option_value='http://mydomain.com' where option_id='1';
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
If you are using CloudFlare to manage your domain and provide HTTPS support, you need to install the CloudFlare Flexible SSL plugin for HTTPS to work. To do this, go to the Plugins section of the Dashboard and search for “CloudFlare Flexible SSL”.
Now you have a fully-functional WordPress blog that you can completely customize and control. Happy blogging!
One thought on “Install WordPress on Ubuntu”