How to install Laravel using Ansible

Sebastian Ravenscroft
5 min readMay 3, 2021

Ansible is a great choice for managing your servers. The code is simple and you don’t need to install any special software agent on your servers.

For this guide we’ll use an AWS EC2 server running Ubuntu. You can launch one from your AWS console by following the guide here. Remember to enable HTTP traffic in the security group.

The Ansible code consists of two parts. The playbook describes a series of steps we want to run, and the inventory defines the servers where we will run the code.

When you run Ansible it will connect to your servers by SSH and run the steps in the playbook.

Installing Ansible

On a Mac we can install Ansible using Homebrew. Then we’ll be able to run Ansible from the terminal.

brew install ansible

The Inventory

The inventory can be written in INI format. In this code we set up a group of servers called ‘webservers’ and then simply listing the DNS addresses.

Make a project folder and save this code in a file called hosts

To find out your EC2 instance DNS open the AWS console and click on the instance to view the summary page:

We also have to specify the ansible_ssh_user. The ssh user for an Ubuntu EC2 instance is usually ubuntu but you can check it by clicking the Connect button at the top right.

The Playbook

The playbook is where most of the work happens. Let’s write a block of YAML code which chooses a server group and then installs the packages using the Linux command line tool apt-get .

Create a main.yml file with the following contents. The indentation is important.

You will need to paste in your AWS access key ID and AWS secret access key as environment variables. For a real project you would need to import these variables securely.

Now open your project folder in the terminal and call this command to run the tasks on your server.

You need to paste a copy of your AWS key file in the folder and provide it using the — private-key argument.

ansible-playbook main.yml -i hosts — private-key=my-aws-key.pem

You will see some feedback as Ansible installs the packages, and a summary at the end. It says changed=1 telling us that changes were made to one server.

Now open a web browser and enter http:// in the address bar, followed by your EC2 instance DNS address.

If everything went well your Apache2 server will be up and running and you will see this welcome page.

Installing Composer

Next let’s download and install Composer on our server. We will use this to install the Laravel dependencies.

Add this code block to the end of your main.yml file. We’re adding to the tasks array so the indentation needs to line up with the first task.

We’re using the get_url module to download the installer. Then we execute a shell command to run the installer.

We use the become: yes option to run the shell command as sudo (superuser).

Finally add this code to rename the composer.phar program, and make executable.

Now let’s run the Ansible playbook again:

ansible-playbook main.yml -i hosts --private-key=my-aws-key.pem

This time Ansible works out it has already installed the packages, so it skips over this step and prints OK.

Ansible runs the new tasks, downloading and installing composer, and outputs changed for these tasks.

Installing Laravel Now add this code block to main.yml to clone a fresh copy of the Laravel repository. We are using Ansible’s built-in Git module.

Next we can use the file module to take ownership of the myapp Laravel folder, and set 755 permission on the Laravel storage folder.

Add this code block to the main.yml file:

What are 755 permissions?

The 755 permissions we set for the storage folder gave all system users permission to read the contents of the folder, but only the owner has permission to write to the folder.

On a Linux file system the we specify three permissions represented by each digit:

  • (1) The file Owner
  • (2) The user Group assigned to the file
  • (3) All other system users

Then we use a binary representation for the permissions we want to assign:

  • 1 = Execute
  • 2 = Write
  • 4 = Read

To work out the permission value, just add up the numbers for the permission you want to grant.

In our case we used these values:

  • Execute, write & read = 1 + 2 + 4 = 7
  • Execute & read = 1 + 4 = 5

Finally we can use the Composer community module to install our Laravel dependencies.

Again add this code block to the end of the main.yml file:

Next we need to make a copy the example .env environment variables file.

The remote_src: yes option tells Ansible to look for the file on your server, not your local machine.

Create a stubs directory containing a myapp.conf file with the following contents:

Now add this code to your main.yml file in order to copy the myapp.conf file to your server.

The second task replaces the $SERVER_NAME placeholder with a special variable called ansible_host.

Next we can execute some shell commands to enable the new host in Apache2.

Add this code to your main.yml file.

We use the pipe operator ‘shell: |’ to be able to run multiple commands.

We just need one more code block to generate our Laravel keys via the shell:

That was a lot of code but we’re done. If you run the playbook one more time your site should be up and running.

ansible-playbook main.yml -i hosts — private-key=my-aws-key.pem

Now visit your EC2 endpoint and you should see the Laravel welcome page!

http://[Your EC2 endpoint]

--

--