LAMP, Linux Server, Vagrant

Setup LAMP stack in Vagrant for rapid development

If you read my previous article on how to run vagrant now you might want to install some development stuff. In this article, I’ll guide you how to install LAMP stack in Vagrant.

LAMP stack stands for Linux, Apache, MySql, and PHP. I’ve already written an article on how to install Linux inside Vagrant. So this time I’m going to write about how to install the AMP part. If you’re a PHP developer and want to run WordPress, Laravel things like that you can do it with LAMP stack.

The following is the general procedure to install LAMP stack in Vagrant.

  1. Install & Configure Apache
  2. Install MySql
  3. Install & Configure PHP

Before you begin, make sure to ssh on your Vagrant machine and make sure it’s running Linux (any Debian variant). Then run the following command:

sudo apt-get update

1. Install & Configure Apache

Apache is the most widely used web server software. It will handle all the web request and serve user your website resources. Run the following command to install Apache:

sudo apt-get install apache2

Press Y and hit Enter to continue, and the installation will proceed.

Set Global ServerName to Suppress Syntax Warnings

Apache requires a global server name inside the conf. It’s not required, but if you do not set ServerName globally, you will receive a warning when checking your Apache configuration for syntax errors:

sudo apache2ctl configtest AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message
Syntax OK

To fix this warning, open up the Apache configuration file with text edit:

sudo vim /etc/apache2/apache2.conf

Go to the very bottom of the file and add a ServerName directive, pointing to your Vagrant private_network IP address.

ServerName 192.168.33.10

Save and close the file when you are finished.

If you don’t know your VM IP address, open the Vagrantfile and look for private_network. You can also check the IP configuration part in my previous article.

Now, check for syntax errors by typing:

sudo apache2ctl configtest

You shouldn’t see any warning this time. Now restart apache to implement the changes.

sudo systemctl restart apache2

Now use your VM IP address to check Apache2 default page on your browser.

http://192.168.33.10/

Apache Ubuntu Default Page

2. Install MySql

After you’ve successfully installed Apache, now let’s see how to install MySql. You can use MySql database to store your website information. To Install MySql run the following command:

sudo apt-get install mysql-server

Press Y and hit Enter to continue, and the installation will proceed.

During the installation process, it will ask you for root user password. Basically, root user has all the privileges to manage the MySql databases and it’s configuration. Input your password and let it proceed.

At this point, your MySql server should be up and running. To test your MySql server run the following command:

mysql -u root -p

You’ll be asked for database root password, input the given password and you should be able to connect with the MySql process.

MySql Terminal Information

Run the command below to exit from MySql process:

exit

3. Install & Configure PHP

PHP can serve dynamic content, run scripts and connect with your MySql database to use dynamic information.
In this last step, we will install and test PHP. But with PHP we will also install some PHP-Apache-MySQL library to create a connection between PHP, MySQL, and Apache. Run the following command to install PHP:

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

Press Y and hit Enter to continue, and the installation will proceed. Within a minute the installation process will be finished and you should’ve PHP running on your machine.

Restart Apache to make sure the LAMP stack synchronization.

sudo systemctl restart apache2

Test PHP Processing on the Apache Web Server

We can create a very basic PHP script to test PHP processing with Apache server. This script will simply output phpinfo() in your browser.

Create a phpinfo.php and place it inside the /var/www/html/ directory. This is the default root directory for Apache server.

sudo vim /var/www/html/phpinfo.php

Save and close the file.

Now visit: http://your_server_IP_address/phpinfo.php and you should see the PHP info page.

php information page

That’s it! You’ve successfully installed the LAMP stack inside your Vagrant machine. Enjoy your dev environment 🙂

Tagged , ,

About Robin

A learner who loves technology very much. Dreaming to become a programmer but also enjoying the life.
View all posts by Robin →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.