Share this page:

Install Wordpress on Apache

Wordpress is a great tool for building web sites.  It's been around for a long time and has a lot of useful features.  Wordpress is very popular, so there are a huge number of plugins and themes available for it.  Many of them are free, but some of the more sophisticated add-ons are commercial.  The range of plugins covers everything from performance optimization to social networking and e-commerce.

To install Wordpress, set up your server as a LAMP server and install Apache, MySQL and PHP.

Set up MySQL

Log into the MySQL server with the following command:

$ mysql -u root -p

Enter these commands to set up the Wordpress database:

mysql> create database wp;
Query OK, 1 row affected (0.00 sec)

mysql> create user wpuser@localhost identified by 'wppassword';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on wp.* to wpuser@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

The first command creates the database, and the second one creates a user account that can be used by Wordpress.  The new account is granted access privileges on the new database. The privileges table needs to be flushed, and then you can exit from the MySQL client shell.

Install Wordpress

Download and unpack the latest version of Wordpress into your home directory:

$ cd ~
$ wget http://wordpress.org/latest.tar.gz
$ tar xzf latest.tar.gz 

Change to the newly created wordpress directory:

$ cd wordpress/

Create a configuration file by copying the sample configuration, and edit the new config file in nano:

$ cp wp-config-sample.php wp-config.php 
$ nano wp-config.php 

Near the top of this file you'll see definitions of the database name, database user name, and the database password.  Change these values to match the database name, user name and password that you stored in the database earlier.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wp');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'wppassword');

Type control-o and press return to save changes.  Type control-x to exit nano.

Copy the Wordpress files to the Apache web root directory:

$ sudo cp -r ./* /var/www

Next, remove the default Apache page:

$ sudo rm /var/www/index.html 

Now visit your server's IP address in a web browser.  You should see this set up page:

Wordpress installation page

Once you've filled in these details, login to Wordpress:

Wordpress login page

Now you should be able to see the Wordpress dashboard:

Wordpress dashboard

Take some time to look around the dashboard.  You can change your site's theme in the Appearance menu, and adjust options in the settings menu.

Consider adding plugins to your Wordpress installation to improve performance.  Wordpress is a large piece of software, so it's worth adding a caching plugin to speed up page load times.  WP Super Cache is a popular choice. The Google XML Sitemap plugin generates an XML sitemap which you can register with Google. Visit the Wordpress site to see some of the most popular plugins.

Setting directory permissions

When you add posts to your site, you may want to add images. You will need to upload image files to your site, so you need to set up a directory where Wordpress can store uploads. Create the uploads directory with this command:

sudo mkdir /var/www/wp-content/uploads

Apache needs to be able to write to this directory in order to save files in it.  This command changes the ownership of the uploads directory:

sudo chown -R www-data:www-data /var/www/html/wp-content/uploads

This command sets the directory's permissions so that Apache can read and write the directory:

sudo chmod 0644 -R /var/www/wp-content/uploads

Install plugins in your site

For security reasons it's bad practice for Apache to have permission to write to files and directories in the web root directory. If Apache is hijacked by hackers, and it has write access to the web root directory, then hackers will be able to modify content on the site. Instead, files should be owned by root, and be readable by other users, but not writable.

This can be a problem when you're setting up a Wordpress site, and you need to install plugins and themes since Apache needs to be able to store files in the web root directory. A simple solution is to change the owner of Wordpress's files to www-data during development, and then change the owner back to root when the site is live.

While you're stilll working on setting up your site, use this command to make Apache the owner of the web root directory:

sudo chown -R www-data:www-data /var/www/

Once the site is set up, change the owner of Wordpress files back to root:

sudo chown -R root:root /var/www/

Don't forget that the uploads directory needs to be owned by www-data:

sudo chown -R www-data:www-data /var/www/wp-content/uploads

Comments