LAMP setup
LAMP setup for Ubuntu 8.04 (Hardy Heron)
Introduction
Here is how I set up my LAMP server (Linux, Apache, MySQL, PHP) on my Ubuntu 8.04 (Hardy Heron) computer. I’ve also installed Python and phpMyAdmin.
It’s worth noting that I’m only using my LAMP server to test website stuff that will run on other people’s servers; I’ve not even thought about the security issues that might concern internet-facing servers.
Package Installation
To start off, I used Synaptic Package Manager to install:
- php5
- apache2
- mysql5
- phpMyAdmin (a nice GUI for managing MySQL tables)
- php5-cli (only needed if you want to run PHP from the command line)
- libapache2-mod-python (if you want to use Python)
and associated packages.
Synaptic Package Manager can be found in the drop-down menu under System -> Administration -> Synaptic Package Manager
. The packages can be found by clicking the Search
icon and typing in the package names.
When a package name is selected, Synaptic Package Manager kindly prompts one to install all other files required.
The MySQL installation prompts one to set a password for the mysql root user; I did this. More on this below.
Where important things are
The Apache configuration file is located at:
/etc/apache2/apache2.conf
The default web folder is here:
/var/www
Apache error logs are here:
/var/log/apache2/error.log
If anything goes horribly wrong, reading these can often be very helpful. (Even if what’s written in the file makes no sense, it’s often quite easy to pop the error message or warning into Google. Very often, there’s someone talking about the problem using real words in the first page of hits.)
Apache access logs live here:
/var/log/apache2/access.log
NB: Both etc
and var
can be found two levels ‘up’ from the home folder.
Test to make sure PHP works
Make file called test.php with one line:
# test.php <? phpinfo(); ?>
Using emacs or vi is cool, but gedit is easy to use.
Save the file in the var/www/
folder. (See below if you encounter permissions problems when saving the file!)
Open a browser (like, say, Firefox) and type
http://localhost/test.php
and you should get a page come up with all sorts of useful and interesting information about your Apache server!
If it doesn’t, try restarting your apache server, as described below.
Saving files in folders where you don’t have permission
If you get an error like this from the command line:
cannot create regular file `test.php’: Permission denied
or like this from gedit:
You do not have the necessary permissions to save the file. Please, check that you typed the location correctly and try again.
you don’t have permission to save the file in the place you’re trying to save it.
You can open gedit from the command line like this:
sudo gedit &
This should prompt you for the root password. (This is the linux root password, not the mysql root password from above!) This allows you to act as if you’re root and save your file in the var/www
folder.
Setting up phpMyAdmin
Open this file:
/etc/apache2/apache2.conf
and add this line:
Include /etc/phpmyadmin/apache.conf
(If you have permissions problems, try the same trick as described above.)
Restart Apache. (See below.)
Check to make sure phpMyAdmin works
Open your favorite web browser (say, Firefox) and navigate to:
http://localhost/phpmyadmin/
This should bring up a login screen. You can log in to phpMyAdmin by using ‘root’ as the username and the mysql root password set during installation.
The mysql root password can be changed in phpMyAdmin by going to the privilege page and clicking the pencil icon next to each root account. Enter a password in appropriate field of the newly-loaded page.
Once logged in, you can use phpMyAdmin to manage your MySQL databases.
Restarting Apache
From the command line, type:
sudo apache2ctl restart
The first time I did this, I received this warning:
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
This went away when I added this line:
ServerName localhost
to:
/etc/apache2/apache2.conf
and restarted Apache. After a restart, there were no further complaints.
Change the default root for Apache
By default, Apache2 will look at /var/www
for all website files. That’s why entering
http://localhost/test.php
in a browser displayed the document saved in /var/www
above.
A different folder can be made the default. Make a folder for the website in the desired location. In this example, I’m calling this directory public_html
and I’ll call my website latin
(since it’s going to be a hyper cool site all about Latin language stuff).
All the sites available to Apache are stored here:
/etc/apache2/sites-available/
Each site needs a config file. On installation, Apache has a default file config file here:
/etc/apache2/sites-available/default
which points to a default website here:
var/www/index.html
which can be seen when this:
http://localhost/index.html
or this:
http://localhost
is entered into a browser.
To change the default to point to your new folder, first copy the configuration file for the default site, giving it the name of the new site. This can be done from the command line as such:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/latin
Open the new file (e.g. /etc/apache2/sites-available/latin
in the above example) and make the following changes.
Change the line:
DocumentRoot /var/www/
to point to wherever your folder is. I put mine in my folder (myfolder) in the home directory:
DocumentRoot /home/myfolder/public_html
Similarly, change:
Directory /var/www/
To:
Directory /home/myfolder/public_html
Disable the ‘default’ site:
sudo a2dissite default
Enable the ‘new’ site:
sudo a2ensite latin
Restart Apache:
sudo apache2ctl restart
Check to make sure the new default location works
Make a simple HTML file, like for example:
<html> <body> Hello world! Oh happy day - all is working! </body> </html>
and save it as index.html
in the new folder. In my case, this was here:
/home/myfolder/public_html/index.html
and put
http://localhost/index.html
or this:
http://localhost
is entered into a browser.
This should now point to your index.html
file in the folder you specified, not the default index.html
file in var/www/
.
Rohan
October 14, 2010
Thanks a lot. Was searching for this for long. Awesome article.
grid
March 8, 2011
No problem – I’m so glad it was useful! : )