This tutorial takes you through installing LAMP (Linux, Apache, MySQL, and PHP) on Ubuntu and installing WordPress, a blogging engine, on the new server. You should be doing all of this on your local computer (your virtual machine running Linux). The second half of the tutorial will teach you how to setup a EC2 or Rackspace server and connect it with a domain name that you can purchase on Namecheap.
Ubuntu LAMP Installation
LAMP stands for Linux, Apache, MySQL, and PHP. The LAMP stack is what typical websites use to serve pages. Linux, as we know, is the operating system; Apache is the web server; MySQL is the database; and PHP is the dynamic web programming language that generates pages as requests come in.
Each piece is interchangeable. For example, some low traffic sites use SQLite which uses a flat file as opposed to a full database. Other sites may exchange PHP for Python to use the popular Django framework. And some sites even use a different operating system such as Windows IIS.
Learning how each of the pieces in the LAMP stack interact is important to understanding how to use other tools in their place and I encourage you to peruse the configuration files and packages beyond what we cover here.
In this part of the tutorial, I am assuming you are installing LAMP on top of an Ubuntu VM or actual hardware installation.
To begin we update our package lists:
sudo apt-get update
Next we install Apache and PHP 5:
sudo apt-get install apache2 php5 libapache2-mod-php5
To test that our installation is working correctly, create a file using this command and containing the text below it:
sudo vim /var/www/test.php
# test.php
<?php phpinfo(); ?>
Now, using a browser inside the VM, open this address: http://localhost/test.php
“Localhost” points to the local server running on the machine and is very useful for beta testing before deploying to a public web server.
The web page you see should display the configuration information of PHP on the Apache server. If your web browser tries to download test.php rather than executing it, that could mean that the file has read, but not execute, permissions, in which case you should perform the following command:
sudo chmod 755 test.php
It could also indicate that apache doesn’t know to execute php filesbecause php or the apache php module isn’t running. Even if you installed it, you may need to restart apache (as described at the bottom) or, simply, restart your computer.
As long as the page displays correctly, you can move on to installing MySQL:
sudo apt-get install mysql-server mysql-client php5-mysql
During the installation, MySQL will prompt you for a root password. Choose something secure and write it down if you need to. You will need to use it during the WordPress installation later.
Installing WordPress
WordPress is one of the most popular blogging platforms and a great way to test our new server. To start we will be downloading the WordPress software and creating a MySQL database to store the blog posts and user data.
Download WordPress here: http://wordpress.org/download/
Save it on the server preferably. To do that, you can use wget:
cd ~
mkdir Downloads
cd Downloads
wget http://wordpress.org/latest.tar.gz
Now we need to create the MySQL database. To do this, we will use the MySQL shell by logging in as the root user:
sudo mysql –password
Note that you shouldn’t use –password=your_password because when you run a command, the full text of the command you typed will be visible to anyone who uses the ps command to look at all processes running on the computer (which is a problem if you have a server that lets pepole ssh in, for instance). In almost all commands that take a password, if you say –password and leave the password blank, it will prompt you for a password in a way that the world can’t see.
You will see something like the text below:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 436
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
At the prompt type the following commands:
mysql> create database wordpress;
mysql> grant all on wordpress.* to ‘wordpress’@’localhost’ identified by ‘new_password’;
What the last comand does is: give permissions for the database called wordpress to the user called wordpress on the server called localhost as long as that user uses the password called new_password. In other words, if you want to use a different database, change the first part; if you want to use a different user, change the second part; if you want a password that is more secure than “new_password”, change the last part.
After each command you should get a message like this:
Query OK, 1 row affected (0.07 sec)
Don’t worry about the number of rows affected, as long as similar message displays and there are no errors.
These two commands are fairly straightforward. The first creates a database called ‘wordpress’ and the second grants a new user ‘wordpress’ all rights to the database with the password ‘new_password’. If this is your first foray into SQL syntax, take a look at the reference manual for more information.
Now that the database is setup, we need to unpack the WordPress tarball into the www root folder:
sudo cp ~/path/to/latest.tar.gz /var/www
cd /var/www
sudo tar xvf latest.tar.gz
The first three commands are self-explanatory. You shouldn’t need to change the default permissions, but if you do, check out http://codex.wordpress.org/Changing_File_Permissions.
You are ready to open the site in your browser now. Open: http://localhost/wordpress
You should see a screen prompting you to begin entering configuration data. This usually doesn’t work for me so I generally edit the file by hand:
cd wordpress
sudo mv wp-config-sample.php wp-config.php
sudo vim wp-config.php
The variable names are self-explanatory. Make sure to use the MySQL database, username and password you created or WordPress will not be able to correctly connect to the MySQL database. Also, to increase the security of your passwords, you should go down to the section about authentication keys and salts , go the to specified URL, and paste in the blob of text that it generates in place of the default lines there.
Refer to the WordPress installation guide for more information on the configuration file.
Again open your browser to: http://localhost/wordpress
You should be able to input information for a new blog and be able to create an admin account. Once you are done with this, your new blog is setup!
Pretty URLs for WordPress
Now that you have working LAMP and WordPress installations, you are ready for a challenge. Most blogs have pretty permalinks so that when I go to “LAMP Installation” posted on May 16, 2011 I have a URL like this: http://jmvldz.com/2011/05/16/lamp-and-wordpress-installation/
However, a default WordPress installation will have permalinks like this: http://jmvldz.com/?p=17
To get this working correctly, you will need to setup Apache with the ‘rewrite’ module, create a ‘.htaccess’ file, and edit the WordPress settings.
You can find a great tutorial here. Make sure you read thoroughly and follow all the steps. The hardest part will be getting the Apache configuration right and I’ll help you a bit with that.
The file it asks you modify will be ‘/etc/apache2/sites-available/default’. Make sure you modifty the ‘Overwrite None’ line in that file to ‘Overwrite All’.
This directive allows WordPress to overwrite Apache’s normal behavior with links so that it can do some magic behind the scenes.
Also, be sure to enable the ‘rewrite’ module and restart Apache:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
The ‘/etc/init.d/’ directory contains a host of other services including MySQL. These services are called daemons and will be covered in a later module.