Install Apache, PHP 7 on Ubuntu 18.04 with 3 Steps only (via Ansible)
Tags: devops, beginners, ansible, tutorial
Published: 2020-02-10 03:15:02

Nowadays, If we want to search "how to install apache with PHP", we will find out, there are many tutorials out there. All the tutorials have a long step-by-step and sometimes it doesn't work in the end. After all, It makes all of us frustrated.

So, I've decided to create a role/script which I can re-use every time I need it without a long step. At this time, I would like to share this role and how to use it. It only needs a three-step to do.

# The First Step: 
We have to prepare the ingredients we need in our workstation.
The Prerequisites:
```
ansible 2.9.4
python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
Python 3.6.9
ansible-playbook 2.9.4
ansible-galaxy 2.9.4
```
you can refer to this [docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html) for more details.
You need to have all the prerequisites above before continuing the next step. Keep in mind, a different version may have different behavior when executing the roles. So, make sure everything already set up before doing the next step.

When creating this tutorial, I use ubuntu derivative, Install all prerequisites in Linux Mint 19.3. If you use another distro or OS, you need to refer to the docs above.
```
sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible
```
After installing all the prerequisites, You need to add some configuration at your hosts to make your process easier. This is what should you do:
```
nano/vim(depends on your favourite text editor)`/etc/ansible/hosts`

```
After that, add this value at hosts file (change server_IP, server_ssh_port, ssh_user accordingly with your server)
```
<Server_IP> ansible_ssh_port=<Server_SSH_port> ansible_ssh_user=<ssh_user> ansible_host_key_checking=false ansible_python_interpreter=/usr/bin/python3
```
# The second step:
After the first step, Right now, we can create a file with our roles inside. I will give a name for this file with `requirements.yml`. Make sure to give this file with extension `yml`.
```
- src: iilness2.ans_common
- src: iilness2.ans_apache
- src: iilness2.ans_php7_2_fpm
- src: iilness2.ans_app
```
All the roles, you can check at my galaxy [here](https://galaxy.ansible.com/iilness2) or see the code at my git [here](https://github.com/iilness2). you can use my git directly also for using my roles like in the example below: 
```
- src: https://github.com/iilness2/ans-common
- src: https://github.com/iilness2/ans-apache
- src: https://github.com/iilness2/ans-php7.2-fpm
- src: https://github.com/iilness2/ans-app
```
After everything settled, you can run this command `ansible-galaxy install -r requirements.yml --force` to start the process. I used `--force` here, so if accidentally, you already use my roles before, the process still can be executed.

# The third step:
In this step, we will create `main.yml` as our main YAML file which we will use to run our ansible. This is inside of our main YAML script we need.
```
---
- name: Install Apache and PHP
  hosts: all
  become: true
  roles:
    - iilness2.ans_common
    - iilness2.ans_apache
    - iilness2.ans_php7_2_fpm
    - iilness2.ans_app
```
After that, you can run your script with this command `ansible-playbook main.yml`. And you can start to see the process of our script installing our roles to our server

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fjnag2boiyg840aochsl.jpg)

After all process finish, We can try to access our server with `<Server_IP>` we input before to see the result. If everything ok, You can see the result like below: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2cz8m9npja3yh6e2km01.jpg)

I use PHP info for this tutorial, to see if all my roles can be installed properly. You can used another php file for playing around or made a suggestion or collaboration to my git, if you have better idea.

I think that's what I want to share right now for this article. Leave a comment below about your thoughts! Thanks.