How to install WordPress locally on Ubuntu/Debian?

WordPress is a content management system originally developed for blogging, but has been greatly expanded through widgets, themes and plugins. Complete the following steps to install WordPress on a Ubuntu 12.04 workstation (“server”) so that you can try different plugins and theme, before you get your blog online.

1. Pre-reqs (Apache, PHP, and MySQL)

sudo apt-get update
sudo apt-get install apache2 php5 php5-mysql mysql-server

When MySQL is installed, it will ask for a password for the MySQL root user.

You may now test if your Apache server is working using a web browser – just type in your IP address.

2. Test if PHP scripting module is loaded in Apache.

sudo sh -c ‘echo “<?php phpinfo(); ?>” >/var/www/phptest.php’

This command creates a new file called phptest.php in the /var/www/ directory. This file tell PHP to create an information page for diagnostic purpose. To visit it in a browser by typing http://ipaddress/phptest.php or on the local computer itself by http://localhost/phptest.php.

3. Go to http://www.wordpress.org and download the latest zip file. Do NOT install the WordPress in the Ubuntu repository, like below.

    sudo apt-get install wordpress

Put the zip file in your home directory and unzip it.

4. Create the WordPress Database and MySQL User

    Log into the MySQL Shell and create a new MySQL directory for WordPress.

    mysql -u root –p

    After logging in using you’re MySQL root password, create a WordPress database:

    Note : Keep in mind that all MySQL commands must end with semi-colon.

   (For example, we will use the database name WordPressdb.)

    CREATE DATABASE wpdatabase;

    Create the new user. You can replace the database, name, and password, with whatever you prefer:

    CREATE USER wpuser@localhost;    

    Set the password for your new user:

    SET PASSWORD FOR wpuser@localhost=PASSWORD(“wp_password“);

    Grant all privileges to the new user. Without this command, the wordpress installer will not be able to start up:

    GRANT ALL PRIVILEGES ON wpdatabase.* TO wpuser@localhost IDENTIFIED BY “wp_password“;

    Refresh MySQL:

    FLUSH PRIVILEGES;

    Exit out of the MySQL shell:

    exit

   Configure WordPress

    Copy the sample wordpress configuration file from the wordpress directory, into a new file. Edit the new file to create a new usable wordpress config file:

    cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php

    Open the wordpress config:

    sudo gedit ~/wordpress/wp-config.php

    Find the section that contains the field below and substitute in the correct name for your database, username, and password:

    // ** MySQL settings – You can get this info from your web host ** // /** The name of the database for WordPress */ define(‘DB_NAME’, ‘wpdatabase‘);
    /** MySQL database username */ define(‘DB_USER’, ‘wpuser‘); /** MySQL database password */ define(‘DB_PASSWORD’, ‘wp_password‘);

    Save and Exit.

    Copy the Files

    Transfer the unzipped WordPress files to the website’s root directory (/var/www/wordpress/).

    sudo rsync -avP ~/wordpress/ /var/www/wordpress/

or copy the WordPress directory into /usr/share/wordpress/

sudo rsync -avP ~/wordpress/ /usr/share/wordpress/

And,  then create a link in /var/www/

sudo ln -s  /usr/share/wordpress /var/www/wordpress

    Set the permissions on the installation. First, switch in to the web directory:

    cd /var/www/

5. Give ownership of the directory to the Apache user.

    sudo chown [USERNAME]:www-data /var/www -R
    sudo chmod g+w /var/www -R

    Install the following php module on your server, to enable the WordPress installation form online. Download and install php-gd:

    sudo apt-get install php5-gd

    Make sure the group is www-data on ‘/var/www’.    

    sudo chgrp www-data /var/www

    Make ‘/var/www’ writable for the group.

    sudo chmod 775 /var/www

    Set the GID for www-data for all sub-folders.

    sudo chmod g+s /var/www

    Your directory should look like this on an ‘ls -l’ output.
    drwxrwsr-x

    Last, add your user name to the www-data group (secondary group).

(For a new user)

    sudo useradd -G www-data [USERNAME]

(For an existing user)

    sudo usermod -a -G www-data [USERNAME]

(Change file owner and group)

    sudo chown [USERNAME] /var/www/

    chown www-data:www-data /path/to/wp-content/ -R

6. What if you are asked to input server FTP info when enabling upgrade.

Add define(‘FS_METHOD’,’direct’); to wp-config.php and it will work on localhost.