|
|
Tip: You can save time by using our LAMP StackScript to deploy this software automatically. |
This guide provides step by step instructions for installing a full featured LAMP stack on an Ubuntu 9.10 (Karmic) system. You will be instructed on setting up Apache, MySQL, and PHP. If you don't feel that you will need MySQL or PHP, please don't feel obligated to install them.
These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a Linux VPS and get started today.
It is important to make sure that your system is properly configured before installing Apache. In particular, you need to make sure that your system is up to date and that you have set the correct timezone, hostname, and hosts in your hosts file. If you haven't configured these, you should follow the directions in the getting started guide. This guide assumes that you are logged in as the root superuser on your Linode.
The Apache Web Server is a very popular choice for serving web pages. While many alternatives have appeared in the last few years, Apache remains a powerful option that we recommend for most uses.
Make sure your package repositories and installed programs are up to date by issuing the following commands:
apt-get update apt-get upgrade --show-upgraded
To install the current version of the Apache web server (in the 2.x series) on an Ubuntu system use the following command:
apt-get install apache2
Now we'll configure virtual hosting so that we can host multiple domains (or subdomains) with the server. These websites can be controlled by different users, or by a single user, as you prefer.
There are different ways to set up Virtual Hosts, however we recommend the method below.
By default, Apache listens on all IP addresses available to it. We must configure it to listen only on addresses we specify. Even if you only have one IP, it is still a good idea to tell Apache what IP address to listen on in case you decide to add more.
Begin by modifying the NameVirtualHost entry in /etc/apache2/ports.conf as follows:
File excerpt: /etc/apache2/ports.conf
NameVirtualHost 12.34.56.78:80
Be sure to replace "12.34.56.78" with your Linode's public IP address.
Now, modify the default site's virtual hosting in the file /etc/apache2/sites-available/default so that the <VirtualHost > entry reads:
File excerpt: /etc/apache2/sites-available/default
<VirtualHost 12.34.56.78:80>
First, create a file in the /etc/apache2/sites-available/ directory for each virtual host that you want to set up. Name each file with the domain for which you want to provide virtual hosting. See the following example configurations for the hypothetical "ducklington.org" and "bucknell.net" domains.
File: /etc/apache2/sites-available/duckington.org
<VirtualHost 12.34.56.78:80>
ServerAdmin squire@ducklington.org
ServerName ducklington.org
ServerAlias www.ducklington.org
DocumentRoot /srv/www/ducklington.org/public_html/
ErrorLog /srv/www/ducklington.org/logs/error.log
CustomLog /srv/www/ducklington.org/logs/access.log combined
</VirtualHost>
File: /etc/apache2/sites-available/bucknell.net
<VirtualHost 12.34.56.78:80>
ServerAdmin squire@bucknell.net
ServerName bucknell.net
ServerAlias www.bucknell.net
DocumentRoot /srv/www/bucknell.net/public_html/
ErrorLog /srv/www/bucknell.net/logs/error.log
CustomLog /srv/www/bucknell.net/logs/access.log combined
</VirtualHost>
Notes regarding this example configuration:
Before you can use the above configuration you'll need to create the specified directories. For the above configuration, you can do this with the following commands:
mkdir -p /srv/www/ducklington.org/public_html mkdir /srv/www/ducklington.org/logs mkdir -p /srv/www/bucknell.net/public_html mkdir /srv/www/bucknell.net/logs
After you've set up your virtual hosts, issue the following commands:
a2ensite ducklington.org a2ensite bucknell.net
This command symbolically links your virtual host file from sites-available to the sites-enabled directory. Finally, before you can access your sites you must reload Apache with the following command:
/etc/init.d/apache2 reload
Assuming that you have configured the DNS for your domain to point to your Linode's IP address, Virtual hosting for your domain should now work.
If you wanted to disable the duckington.org site, for example issue the following command:
a2dissite ducklington.org
The a2dissite command is the inverse of a2ensite. After enabling, disabling, or modifying any part of your Apache configuration you will need to reload the Apache configuration again with the /etc/init.d/apache2 reload command. You can create as many virtual hosting files as you need to support the domains that you want to host with your Linode.
MySQL is a relational database management system (RDBMS) and is a popular component in contemporary web development tool-chains. It is used to store data for many popular applications, including WordPress and Drupal.
The first step is to install the mysql-server package, which is accomplished by the following command:
apt-get install mysql-server
During the installation you will be prompted for a password. Choose something secure (use letters, numbers, and non-alphanumeric characters) and record it for future reference.
At this point MySQL should be ready to configure and run. While you shouldn't need to change the configuration file, note that it is located at /etc/mysql/my.cnf for future reference.
After installing MySQL, it's recommended that you run mysql_secure_installation, a program that helps secure MySQL. While running mysql_secure_installation, you will be presented with the opportunity to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options. If you are prompted to reload the privilege tables, select yes. Run the following command to execute the program:
mysql_secure_installation
Next, we'll create a database and grant your users permissions to use databases. First, log in to MySQL:
mysql -u root -p
Enter MySQL's root password, and you'll be presented with a MySQL prompt where you can issue SQL statements to interact with the database.
To create a database and grant your users permissions on it, issue the following command. Note, the semi-colons (;) at the end of the lines are crucial for ending the commands. Your command should look like this:
create database lollipop; grant all on lollipop.* to 'foreman' identified by '5t1ck';
In the example above, lollipop is the name of the database, foreman is the username, and 5t1ck password. Note that database user names and passwords are only used by scripts connecting to the database, and that database user account names need not (and perhaps should not) represent actual user accounts on the system.
With that completed you've successfully configured MySQL and you may now pass these database credentials on to your users. To exit the MySQL database administration utility issue the following command:
quit
With Apache and MySQL installed you are now ready to move on to installing PHP to provide scripting support for your web pages.
PHP makes it possible to produce dynamic and interactive pages using your own scripts and popular web development frameworks. Furthermore, many popular web applications like WordPress are written in PHP. If you want to be able to develop your websites using PHP, you must first install it.
Ubuntu includes packages for installing PHP from the terminal. Issue the following command:
apt-get install php5 php-pear
Once PHP5 is installed we'll need to tune the configuration file located in /etc/php5/apache2/php.ini to enable more descriptive errors, logging, and better performance. These modifications provide a good starting point if you're unfamiliar with PHP configuration.
Make sure that the following values are set, and relevant lines are uncommented (comments are lines beginning with a semi-colon (;)):
File excerpt: /etc/php5/apache2/php.ini
max_execution_time = 30 memory_limit = 64M error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR display_errors = Off log_errors = On error_log = /var/log/php.log register_globals = Off
If you need support for MySQL in PHP, then you must install the php5-mysql package with the following command:
apt-get install php5-mysql
To install the php5-suhosin package, which provides additional security for PHP 5 applications (recommended), you must add the "universe" software repositories. Uncomment the following lines from /etc/apt/sources.list:
File excerpt: /etc/apt/sources.list
deb http://us.archive.ubuntu.com/ubuntu/ karmic universe deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe deb http://security.ubuntu.com/ubuntu karmic-security universe deb-src http://security.ubuntu.com/ubuntu karmic-security universe
Now run the following command to update the package archive:
apt-get update
Finally, install the suhosin package by issuing:
apt-get install php5-suhosin
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License
.
Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.
|
|
Submitted by colin on Wednesday, December 2 2009 at 04:24:55 GMT
Thanks!
|
|
|
Submitted by reroot on Tuesday, December 8 2009 at 05:45:41 GMT
This is the most flawless LAMP guide I have ever seen. Many are full of holes and small mistakes new users overlook and thus require endless comment reading, extra research and guesswork. This one is right on.
|
|
|
Submitted by vinh khoa on Saturday, December 12 2009 at 03:40:30 GMT
This guide is very easy to follow and everything went so smoothly. Thanks for the great guide. All the commands are also very accurate and up to date as well which is much appreciated.
|
|
|
Submitted by Huan on Wednesday, December 16 2009 at 01:49:33 GMT
You rock! I was able to follow your instructions and install the web server within minutes.
|
|
|
Submitted by Chris on Thursday, December 17 2009 at 16:35:12 GMT
Isn't Ubuntu's apache/web directory /var/www, not /srv/www?
|
|
|
Submitted by Anonymous Visitor on Friday, December 18 2009 at 18:38:59 GMT
Fantastic - worked first time....
|
|
|
Submitted by Phil Paradis on Friday, December 18 2009 at 19:19:36 GMT
@Chris - The location website files are served from is completely up to the administrator; we use "/srv" as a personal preference in most of our examples, but you could specify any location you like in your virtual host configuration.
|
|
|
Submitted by Kai on Wednesday, December 23 2009 at 03:33:05 GMT
This was great. I went through it with no prior experience and no problems. Very helpful and clear guide. Thank you.
|
|
|
Submitted by laura on Friday, January 22 2010 at 17:47:17 GMT
"In particular, you need to make sure that your system is up to date and that you have set the correct timezone, hostname, and hosts in your hosts file. If you haven't configured these, you should follow the directions in the getting started guide."
These are not in fact covered in the Beginner's Guide, with the exception of setting the hostname. That info can be found at: http://library.linode.com/using-linux/administration-basics#basic_configuration |
|
|
Submitted by Anonymous Visitor on Saturday, February 20 2010 at 21:18:20 GMT
Excellent guide.
Caveat: Ubuntu does not include GD2. For some reason, it uses the old GD version, which has poorer performance and lacks several features expected in current software packages including Drupal. If you are new to Linux and need the higher quality image tools, you may want to choose another Linux distribution, as with Ubuntu (especially Karmic) you will have to do extra work, including compiling your own PHP5. |
|
|
Submitted by Nail on Sunday, February 21 2010 at 13:44:24 GMT
Thanks!
|
|
|
Submitted by Jessa on Friday, March 5 2010 at 04:41:49 GMT
how would i modify this to make the server into a development server only? would I change this line in ports.conf and subsequent other entries from:
<VirtualHost 12.34.56.78:80> to <VirtualHost 127.0.0.1:80> ?? I don't need (or want) this server to be seen publicly - I just need a development server, and then ftp files to my host once I know my code works. |
|
|
Submitted by Michael on Tuesday, March 16 2010 at 18:31:51 GMT
This had saved my bacon... Thank you for sharing your knowledge with others in need ;)
|
|
|
Submitted by Ray Wenderlich on Sunday, March 28 2010 at 23:10:26 GMT
This is an excellent guide, I just used it to set up my own LAMP server and it works great. Thanks for putting this together.
|
|
|
Submitted by iclass on Thursday, April 1 2010 at 00:38:12 GMT
This is an amazing document. Every step went without a hitch. Thanks a lot.
|
|
|
Submitted by Bonnie Thurber on Tuesday, June 15 2010 at 01:39:14 GMT
nicely done.. easy to follow.
|
|
|
Submitted by Stan Schwertly on Wednesday, June 16 2010 at 12:02:12 GMT
@Bonnie Thurber: Thanks for the kind words!
|
Got a comment?