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

Setting up pretty links in Wordpress

Wordpress URLs can be quite cryptic and hard to remember.  For example, the URL of the sample post in a fresh Wordpress installation is http://192.168.1.50/?p=1, and the URL of the sample page is http://192.168.1.50/?page_id=2. These permalinks can be formatted to make them more human readable using Wordpress's permalink settings so that the sample page's URL is http://192.168.1.50/sample-page/ and the sample post's URL is http://192.168.1.50/hello-world/. These modified URLs are often referred to as pretty links. 

Mod Rewrite

Mod re-write must be enabled in Apache so that pretty links can be used. You can do that by typing this command in a terminal:

sudo a2enmod rewrite

Get the rewrite rules

Apache's configuration must be modified by adding a set of directives that tell Apache how to rewrite URLs for Wordpress. When Apache receives a request with a pretty URL, it must redirect the request to Wordpress's index.php using mod rewrite. When you enable pretty links, Wordpress generates code containing rewrite rules that configures Apache to do this redirection. Login to the Wordpress dashboard, and go to the Permalink page in the Settings menu. Select the format you want, and click on the save changes button. 

Wordpress permalink settings

The rewrite rules will appear at the bottom of the page:

Wordpress rewrite rules

These are the rewrite rules that were generated with the settings I selected:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Note that Wordpress displays a message saying that it would write the .htaccess file if it was writeable. For security reasons, it's best not to allow Apache to modify .htaccess files. It's safer to update the file yourself.  

Virtual hosts vs .htaccess files

The rewrite rules can be stored in two different places: your site's Apache virtual host file, or an .htaccess file.

If you enable .htaccess files, then Apache checks to see if a .htaccess file is available every time it receives a request. If there is a .htaccess file, Apache must read the rules that it contains and then parse them. The .htaccess file must be read on every single request handled by the server, which isn't a much of an overhead when a single page is served, but becomes a significant overhead on busy sites. 

You may need to use .htaccess files if you don't have access to the virtual host file, but it's best to avoid using them if possible.  If you can access your server's virtual host file, it's best to save the rewrite code there instead of in .htaccess files. When the rewrite code is placed in the virtual host file, it will be loaded just once when Apache starts up. 

Saving rewrite rules in a virtual host file

To save the code in your virtual host file, open it in an editor with this command:

sudo nano /etc/apache2/sites-available/default

Save the code generated by Wordpress in the Directory directive that defines /var/www:


<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None

        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
        </IfModule>

        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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

Saving rewrite rules in a .htaccess file

If you don't want to put the code in your virtual file, you can put it in a .htaccess file instead. The virtual host file still needs to be edited to enable the use of .htaccess files. If you can access the virtual host, then open it in nano as described above, and look for the section with the Directory directive that defines /var/www. Change the line 

AllowOverride None

to 

AllowOverride All

The complete virtual host file should look something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you're using shared hosting, you probably won't have access to the virtual host file, and AllowOverride should already be set to All (if not, talk to your hosting provider).

Now create the .htaccess file in the directory where Wordpress was installed (if you followed this guide on setting up Wordpress on Apache, this will be /var/www/):

sudo nano /var/www/.htaccess

Save the configuration code in the .htaccess file. Type control-o and press return to save changes, and type control-x to exit nano. Make the .htaccess file readable by apache:

sudo chmod 0644 /var/www/.htaccess

Restart Apache

Whether you saved the code in a .htaccess file or in a virtual host file, you need to restart Apache:

sudo service apache2 restart

Now try browsing your site. You should see user friendly URLs in your browser's address bar.


Comments