LAMP Stack with Ansible

Marcelo Schirbel Gomes
Jan 26 · 3 min read

Hello guys! I hope you are all doing fine!

Today, I’ll show you how to set up an LAMP stack using Ansible.

Before we start, please, read this tutorial. It’ll show you how to setup an home lab environment. Every hostname that I use in this tutorial will be based on my home lab.

The only difference, is that I’ll use Ubuntu on the database server. Be sure to change it on the Vagrantfile.

Well, now that your lab is OK, let’s see the application we are going to use.

Click here to see it. And now you are asking yourself:

“Why am I using an laravel app? It’s easier if I create a new project!”

Yes, it is. But this won’t reflect the real world. Where applications are already made.

Now, start your Vagrant machines:

vagrant up control webserver-1 webserver-2 database

Let’s start with the database layer:

First step: download MySQL:

I’ve decided create two different tasks. One for the python package and another one for the mysql server. That’s my way of thinking.

Now that MySQL is installed, we need to create a database and a user for the Laravel’s app:

Ok! Seems nice!

In a production environment you would never give the application full control over the database. But for a guide purpose we can take this.

Now, for the Webserver. We are going to use an Apache Webserver(duur it’s a LAMP).

First of all, download all the packages we are going to need:

After this, we are going to configure our PHP7.2.

There are a few modifications that are necessary:

upload_max_filesize = 64M
short_open_tag = On
memory_limit = 256M

Make sure that you get all this configs in your php.ini.

You can get the default one here. Just get it and modify it!

And now, add a task to copy your php.ini into the server:

- name: replace php.ini  copy:    src: php.ini    dest: /etc/php/7.2/apache2/    owner: root    group: root    mode: 0644

Now we are getting on the tough part: Composer.

Composer is a package manager for PHP. Many times I’ve used. All of them I had some troubleshooting.

But here I’m to guide you through the shadow of death!

First of all. Download Composer and install it on path. Then, go to the directory and run a command to clone the repo app we are using.

Nice! Now, we gotta adjust the env file on our app. Laravel uses a hidden file called .env to map the connection to the database

Mine looks like this:

Now we are going to resolve some permissions issues. Laravel use a 0755 permissions under the user of webserver user and group on its files. Be sure to not miss that. It’s very important.

Only a few things missing!

Once you’ve done this, we are going to configure our Apache to acknowledge our Laravel app.

Crete a virtualhost on Apache:

Just make sure to write the correct path to laravel/public directory under the tag DocumentRoot.

Now we need to upload this file, and create a symlink between the sites-available and sites-enabled on Apache:

Make sure to include a handler in your role!

After this, just reload Apache and curl the url:

curl -I http://webserver-1:80/laravel/public/

Congratulations! Your LAMP stack is up!

Marcelo Schirbel Gomes

Written by

This is just a bit of my life. Writing another line everyday!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade