In this tutorial we’ll learn how to install LAMP (Linux Apache, MySQL, PHP) on Ubuntu. A basic web server installation should include these three packages as they are important to serve both static and dynamic pages.
Apache web server serves the static web page content. MySQL is Open Source Relational SQL database management system and PHP is a scripting languages and widely used to process the dynamic content. phpMyAdmin is a web based application written in PHP and it is used to manage the databases in MySQL server using graphical user interface.
You must have access to the terminal of ubuntu before you install any packages. If you are logged into a non root account then you will have to use
sudo
command before executing any administrative command. If you are logged into root account then you can omit usingsudo
before the commands. In this tutorial we’ll use a non root account hence we’ll usesudo
before the commands.
We will use Ubuntu’s default repository to install the packages which is apt
. It is a best practice to update the apt
repository before proceeding to any installation by issuing the following command.
sudo apt-get update
Installing Apache
You can install Apache Web Server by issuing the command
sudo apt-get install apache2
It will ask you if you want to continue with installation, press y and then enter. Once the server is installed you can start the server by issuing the command
sudo service apache2 start
You can check if the Apache web server is working by going to the browser of your computer and accessing your website through the public IP address of your VPS. Which will look like
http://
For example if IP address of your machine is 164.31.0.40
then go to the following web address.
http://164.31.0.40
This will show you the default apache web page with some instructions written on it like the one shown below.
Configuring Apache
You can remove the default Apache page or put any HTML page in the web root directory which is located at
/var/www/html/
To remove the default page run this command
sudo rm /var/www/html/index.html
You can make changes to the apache global configuration file which is located at /etc/apache2/apache2.conf
This file holds main configuration files of Apache2 web server. There are three sections in the global configuration file which are Apache server processes, configuration for the default server and configuration for virtual hosts. We will look at some global configuration here.
You can edit the global configuration file using any editor, in this tutorial we will be using VIM editor. To edit the global configuration file use the following command
sudo vim /etc/apache2/apache2.conf
You can specify a Timeout for your web server, default is 300 seconds, you can keep it as much as you wish, ideal is 30 seconds, Just replace
Timeout 300
with
Timeout 30
You can configure KeepAlive
On or Off, KeepAlive
specifies that, if Apache should allow more than one request per connection. Default is On
. You can also specify MaxKeepAliveRequests
, this configuration specifies that how many requests are allowed during a single connection. Default is 100
but recommended is 0
to set this option to handle unlimited requests. KeepAliveTimeout
specifies how many seconds to wait for the next request from same connection. Default is 5
, you can leave it as it is. If it does not receive the request within the specified time period then Apache will disconnect the KeepAlive
session.
Installing MySQL
Installing MySQL server will give you capability to store your data in organized manner. MySQL is a relational database management system. To installing MySQL run the following command.
sudo apt-get install mysql-server php5-mysql
It will ask you if you want to continue the installation, hit y and enter. Then it will also ask you to set a root password for MySQL, Enter your desired password.
After installing the MySQL server you can start the database server a by issuing the following commands.
sudo service mysql start
This will run your MySQL server.
You must not confuse with root passwords of Ubuntu server and MySQL server. Both passwords are different. When you are asked for
root
password of Ubuntu, you must enter the password assigned to root account of your server. When you are asked for root password of MySQL server, you must enter the password you created when installing MySQL server.
It is also strongly advised that after installing your MySQL You should run mysql_secure_installation
script once to secure your MySQL installation. You can start running this script by executing the following command.
sudo mysql_secure_installation
This will ask you for the root password for your MySQL server, if you have provided any password during MySQL installation you will need to enter that password, if not then you can leave this field blank.
If you have no root password for MySQL server then it will ask you to create a password for root user. If you have an existing password then it will ask you, if you wish to change your password, choose the option accordingly.
Then it will ask you if you want to remove anonymous users, by default the MySQL installation creates an anonymous user who can login without any username or password. Press y
and then enter key to remove anonymous users and proceed further.
Then It will ask you if you wish to disallow the remote root login, enter y
to enable this, it will protect your root password from bruteforcing. It will again ask you if you wish to remove the test database and its privileges, enter y
to remove the database.
Finally it will ask you if you want to reload privilege tables, press y
to reload the privilege tables, this will write the changes you have just made and MySQL will implement them immediately. You should now see the success message like shown below.
All done! If you've completed all of the above steps, your MySQL installation should now be secure.
Thanks for using MySQL!
Now you can login into your MySQL command line using the following command
mysql -u root -p
It will ask you for your root password, once you enter your password for the root user, it will show you the following screen.
This confirms that your MySQL installation is working properly.
Installing PHP
Apache can only serve static pages, PHP is used to enhance web pages. PHP is most widely used scripting language. Installing PHP will make Apache to process the PHP code with PHP compiler before serving them.
You can install PHP by issuing following command.
sudo apt-get install php5 libapache2-mod-php5
This command will install PHP. libapache2-mod-php5
package provides php for Apache2. In order to use PHP with Apache server we will need to restart the server by issuing the following command.
sudo service apache2 restart
You can also install PHP modules by issuing following command.
sudo apt-get install
For example if you want to install three modules at a time which are php5-curl, php5-fpm and php5-cli, you can run the following command.
sudo apt-get install php5-curl php5-fpm php5-cli
This should install the PHP packages without any error. After installing PHP modules you will again need to restart your server.
You can verify PHP installation by creating a php file with phpinfo();
function. This function will display all the information about PHP processor and all the installed modules of PHP. Execute the following command to create a PHP file using VIM editor inside the Web root directory.
vim sudo rm /var/www/html/php.php
This command will open VIM editor and you can insert the following lines of code into the file.
After putting these lines quit from VIM editor with writing the changes into php.php
file.
Finally you can view the PHP information by browsing this file through web browser, if Public IP of your VPS is 164.31.0.40
, then go to the following address.
http://164.31.0.40/php.php
It should show you all the information about PHP like the screenshot attached.
Installing phpMyAdmin
phpMyAdmin provides a graphical user interface to manage MySQL databases and data inside them. You can also run a custom query through phpMyAdmin. This also gives you capability to create, drop or manage privileges of any databases. You can also export or import the data through phpMyAdmin interface.
To install phpMyAdmin use the following command.
sudo apt-get install phpmyadmin
It will ask you if you want to continue with the installation, press Y and hit enter to proceed with the installation.
After that it will ask you to select your webserver so that it can configure the server automatically. Select Apache2 by pressing spacebar key and then pressing enter key to proceed. Then It will ask you to use dbconfig-common
to setup the database, select yes. Next it will ask you provide a password for phpMyAdmin, you can create any password. Finally it will ask you database administrator password, please enter your database’s root password here, which we had created during MySQL installation.
This will successfully install phpMyAdmin, finally restart the Apache web server by issuing the following command.
sudo service apache2 restart
Finally you can access your phpMyAdmin at the following address.
http:///phpMyAdmin
For example if you have a VPS with IP address 164.31.0.40
then go to the following address to access your phpMyAdmin.
http://164.31.0.40/phpMyAdmin
Now you can login to phpMyAdmin using the username root
password you have created at MySQL Installation. Once you are logged in you will see a screen like shown below.
Summary
In this tutorial we learned how to install Apache, PHP and MySQL onto Ubuntu. You now have a fully working web stack which can be used for hosting a single or multiple PHP based websites.