How To Install Linux, Apache, Mysql, PHP (Lamp) Stack On Ubuntu
How To Install Linux, Apache, Mysql, PHP (Lamp) Stack On Ubuntu
Set Up
The steps in this tutorial require the user to have root privileges on your VPS. You can see how to set
that up in the Initial Server Setup in steps 3 and 4.
Thats it. To check if Apache is installed, direct your browser to your servers IP address (eg.
http://12.34.56.789). The page should display the words It works!" like this.
During the installation, MySQL will ask you to set a root password. If you miss the chance to set the
password while the program is installing, it is very easy to set the password later from within the
MySQL shell.
Once you have installed MySQL, we should activate it with this command:
sudo mysql_install_db
The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Then the prompt will ask you if you want to change the root password. Go ahead and choose N and
move on to the next steps.
Its easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new
changes.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
Once you're done with that you can finish up by installing PHP.
After you answer yes to the prompt twice, PHP will install itself.
It may also be useful to add php to the directory index, to serve the relevant php index files:
sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php
index.xhtml index.htm
</IfModule>
PHP Modules
PHP also has a variety of useful libraries and modules that you can add onto your virtual server. You
can see the libraries that are available.
apt-cache search php5-
Terminal will then display the list of possible modules. The beginning looks like this:
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
You can install multiple libraries at once by separating the name of each module with a space.
Congratulations! You now have LAMP stack on your droplet!
Finish up by visiting your php info page (make sure you replace the example ip address with your
correct one): http://12.34.56.789/info.php
What is Apache?
Apache is the most popular web server on the internet. It is used to serve more than half of all active
websites.
Although there are many viable web servers that will serve your content, it is helpful to understand how
Apache works because of its ubiquity.
In this article, we will examine some general configuration files and options that can be controlled
within them. This article will follow the Ubuntu/Debian layout of Apache files, which is different from
how other distributions build the configuration hierarchy.
This is all that is necessary to have a working web server. If you visit your VPS's IP address in a web
browser, you will get the default Apache index page:
your_domain_name_or_ip_address
It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.
On Ubuntu and Debian, Apache keeps its main configuration files within the "/etc/apache2" folder:
cd /etc/apache2
ls -F
apache2.conf
conf.d/
envvars
httpd.conf
magic
mods-available/
mods-enabled/
ports.conf
sites-available/
sites-enabled/
There are a number of plain text files and some sub-directories in this directory. These are some of the
more useful locations to be familiar with:
apache2.conf: This is the main configuration file for the server. Almost all configuration can be
done from within this file, although it is recommended to use separate, designated files for
simplicity. This file will configure defaults and be the central point of access for the server to
read configuration details.
ports.conf: This file is used to specify the ports that virtual hosts should listen on. Be sure to
check that this file is correct if you are configuring SSL.
conf.d/: This directory is used for controlling specific aspects of the Apache configuration. For
example, it is often used to define SSL configuration and default security choices.
sites-available/: This directory contains all of the virtual host files that define different web
sites. These will establish which content gets served for which requests. These are available
configurations, not active configurations.
sites-enabled/: This directory establishes which virtual host definitions are actually being used.
Usually, this directory consists of symbolic links to files defined in the "sites-available"
directory.
mods-[enabled,available]/: These directories are similar in function to the sites directories, but
they define modules that can be optionally loaded instead.
As you can see, Apache configuration does not take place in a single monolithic file, but instead
happens through a modular design where new files can be added and modified as needed.
We will focus on the first part of the file to learn how Apache defines its global settings.
Timeout
By default, this parameter is set to "300", which means that the server has a maximum of 300 seconds
to fulfill each request.
This is probably too high for most set ups and can safely be dropped to something between 30 and 60
seconds.
KeepAlive
This option, if set to "On", will allow each connection to remain open to handle multiple requests from
the same client.
If this is set to "Off", each request will have to establish a new connection, which can result in
significant overhead depending on your setup and traffic situation.
MaxKeepAliveRequests
This controls how many separate request each connection will handle before dying. Keeping this
number high will allow Apache to serve content to each client more effectively.
Setting this value to 0 will allow Apache to serve an unlimited amount of request for each connection.
KeepAliveTimeout
This setting specifies how long to wait for the next request after finishing the last one. If the timeout
threshold is reached, then the connection will die.
This just means that the next time content is requested, the server will establish a new connection to
handle the request for the content that make up the page the client is visiting.
MPM Configuration
The next section specifies the configuration of the MPM (Multi-Processing Module) options. You can
cross-reference which section your Apache installation was compiled with by exiting into the terminal
and typing:
apache2 -l
Compiled in modules:
core.c
mod_log_config.c
mod_logio.c
prefork.c
http_core.c
mod_so.c
As you can see, in this server, "prefork.c" is a module that was compiled in and is also in the
"apache2.conf" file. Your installation may have multiple to choose from, but only one can be selected.
You can adjust the configuration of the prefork MPM in the appropriate section.
. . .
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
The default Virtual Host is configured to handle any request on port 80, the standard http port. This is
defined in the declaration header where it says "*:80", meaning port 80 on any interface.
This does not mean that it will necessarily handle each request to the server on this port however.
Apache uses the most specific Virtual Host definition that matches the request. This means that if there
was a more specific definition, it could supersede this definition.
Directory Definitions
Within the Virtual Host definition, there are definitions for how the server handles different directories
within the file system. Apache will apply all of these directions in order from shortest to longest, so
there is again a chance to override previous options.
The first directory definition applies rules for the "/", or root, directory. This will provide the baseline
configuration for your Virtual Host, as it applies to all files served on the file system.
By default, Ubuntu does not set up any access restrictions to the filesystem. Apache recommends that
you add some default access restrictions. You can modify this like so:
<Directory />
Options FollowSymLinks
AllowOverride None
Order Deny,Allow
Deny from All
<Directory>
This will deny access to all content unless specified otherwise in subsequent directory definitions.
The next directory definition is for the document root, so it specifies the "allow from all" option that
overrides the "/" option for this directory.
The "AllowOverride" option is used to decide whether an ".htaccess" file can override settings if it is
placed in the content directory. This is not allowed by default, but can be useful to enable in a variety of
circumstances.
Following the alias, you should remember to define the directory with access privileges as discussed in
the previous section.
After enabling a site, issue the following command to tell Apache to re-read its configuration files,
allowing the change to propagate:
sudo service apache2 reload
There is also a companion command for disabling a Virtual Host. It operates by removing the symbolic
link from the "sites-enabled" directory:
sudo a2dissite virtual_host_file_name
Modules can be enabled or disabled by using the "a2enmod" and "a2dismod" commands respectively.
They work in the same way as the "site" versions of these commands.
Remember to reload your configuration changes after modules have been enabled or disabled as well.
Conclusion
We have gone over some basic Apache configuration files. Apache is versatile and very modular, so
configuration needs will be different depending on your setup.
You should have a good understanding of what the main configuration files are used for and how they
interact with each other. If you need to know about specific configuration options, the provided files are
well commented and Apache provides excellent documentation.
Hopefully, the configuration files will not be as intimidating now, and you feel more confortable
experimenting and modifying to suit your needs.