We now have many web stacks to develop web applications rapidly on different operating systems. For instance, we have wamp, xamp, and Lamp stacks. All these stacks consist of some technologies. Our focus in this article is Lamp stack, which is a popular open source web platform used and endorsed by millions of developers to develop dynamic web applications on Linux servers. Lamp is a group of four open source technologies which are:
- Linux operating system
- Apache web server
- MySQL relational database
- PHP scripting language
In this tutorial, I will be setting up the Lamp stack on a Debian server. If you are a SysAdmin, and you are confused about how to set up Lamp stack, then you can follow the steps in this tutorial. I assume you have an empty Debian server with root access.
Note: If you do not know about sysadmin process and creating the LAMP stack, you can use Cloudways Managed PHP Hosting. The complete server containing Lamp stack and PHP app will be built in few min and you don’t need to worry about it.
Let's take a deep dive and start setting up Lamp stack on Debian server.
But first, you need to update all the packages present on your server by running the following command in terminal
$ apt-get update
This command will update all your existing packages. You also need to know the server’s IP address. Run the following command in terminal to find out your server IP address
$ ifconfig eth0 | grep inet | awk '{ print $2 }'
Yay! You can see your IP address on screen. Note it down. You will need it in subsequent installation steps. So ninjas! We are ready to install other grouped technologies which are Apache, MySQL and PHP. Let's start with Apache.
Install Apache on Debian Server
Apache is an open source HTTP server. Around 50% of the web applications are running on Apache with an aim to provide high-quality configurations to web apps. You can install Apache on Debian by running the following terminal command
$ apt-get install apache2
When you run this, you will see a message for extra space. Just type "y" and hit enter.
After finishing installation, run Server IP address in a browser which you have noted earlier.
Our first installation is complete. Now you can access your server and add files in var/www/html
folder but many developers are familiar with public_html folder -- so maybe you wish to change this folder to public_html
. You need to edit 000-default.conf file exist in apache2/sites-enabled. Move to the folder by running the following command
$ cd /etc/apache2/sites-enabled
Next, run the vim command with file name to view this in terminal
$ vim 000-default.conf
Find “Document root” and edit its path by pressing “i” key. Remove html and replace it with public_html. Press ESC key then type :wq to save the changes in terminal.
We also need to change the folder name from html to public_html. For this, move to the folder and use move command to change the folder name.
$ cd /var/www
$ mv html public_html
Restart your web server by typing the following command
$ service apache2 restart
So now we have installed Apache in our Debian server. Enter your IP address in the browser and check it.
Installing MySQL on Debian
The second main installation in this stack is MySQL relational database. To install MySQL, run the following command in terminal
$ apt-get install mysql-server
After the installation finishes, a prompt will appear to set root password
Enter the password and select ok. Now complete rest of the installation by typing the following command
$ mysql_secure_installation
Press enter. You will be asked to enter root password, which you just setup. Type password and hit enter.
-Type ‘n’ in order to not change root password and hit enter
-Type ‘y’ to remove anonymous users and hit enter
-Type ‘n’ if you want to disallow root login remotely and hit enter
-Now type ‘y’ to remove test tables and databases and hit enter, then type ‘y’ again and hit enter
You may also need to install your favourite MySQL GUI client PHPMyAdmin.
PHPMyAdmin is an open source GUI which helps in Managing MySQL databases. Let’s first setup a SSL with Apache so that our password isn’t sent in plain text form. Type the following to install a SSL with Apache:
$ apt-get install mcrypt
Restart Apache server by service command and type the following command to install PHPMyAdmin.
$ apt-get install phpmyadmin
During the installation, a prompt will open which will ask you about the web server you are using. As we have installed Apache, so select Apache and press enter. After that, it will ask for the root user password.
Enter the password and press enter.
PHPMyAdmin will be installed in /usr/share/phpmyadmin
. We will create it’s symlink inside the public_html folder. Type the following commands to route to public_html folder and create a symlink:
$ cd /var/www/public_html
$ ln -s /usr/share/phpmyadmin
Now type ls
to verify whether the symlink has been created or not. Open this in browser through [server ip address]/phpmyadmin. You will see that it is installed. Now let’s secure it. Create a .htaccess file using vim inside PHPMyAdmin folder and allowing only your server IP to access it.
order allow,deny
allow from <your server ip>
Installing PHP on Debian
We have set up Apache and MySQL on Linux. Now the next and last stack member is PHP language to write web applications. PHP can be installed by running the following command
$ apt-get install php5 php-pear php5-mysql
After the installation of PHP, restart your Apache server by running
$ service apache2 restart
Move to your public_html folder by typing
$ cd /var/www/public_html
Create a PHP file in this folder by typing the following command
$ vim index.php
Add the following code line in it
<?php echo phpinfo();?>
Save the file by pressing ESC and then :wq. Run your IP address in the browser, you will see the PHP info page, which means PHP is installed on server.
Final Words
In this tutorial, we have successfully set up Lamp stack on our server and we are ready to develop applications. If you are SysAdmin or server side developer, you can easily set up LAMP stack by following the tutorial step by step. This is not the end. You can also do more configurations and installations according to requirement by following the same road map.
Top comments (7)
Nice post!
My recomendation will be to use php7 instead of 5.6, I suppose that you are using Debian 8 and by default is the 5.6, here there is a tutorial from nixCraft that explains how to update it if someone is interested (cyberciti.biz/faq/installing-php-7...). Anyway with Debian 9 will be shipped by default with php7 package instead of the php 5.6.
Hi, Thanks Actually I'm giving a basic flow so That's my bad that I used PHP 5.6. PHP 7 is recommended :)
"why no nginx?" because that would be lnmp stack :)
phpmyadmin makes me all eye-twitchy. It's a sure-fire attack surface. It's totally fine to deploy on a local dev environment, but not so chill to have on a publicly-access/open internet server. It's funny, one of the tells your box has been compromised is finding a copy of phpmyadmin running on it.
I've installed many LAMP servers in the past, but this is a good step by step for anyone looking for help. But I have to ask why you would use php5 over php7?
Hey David, Thanks For kind words. I thought about the beginners and they always opt for PHP 5.6 But yes as I said above that PHP 7 is recommended now so I'll Update the steps :)
Nice post :)
Why Apache though? I'd go with Nginx or haproxy.
Dude you make my day Nginx is <3 I will be sharing another tut with Nginx soon :)