Codeigniter 4 with Docker on AWS Cloud9
Tags: aws, cloud9, codeigniter, docker
Published: 2020-04-26 11:57:30

---
title: Codeigniter 4 with Docker on AWS Cloud9
published: true
cover_image : https://dev-to-uploads.s3.amazonaws.com/i/kp8epvhtlopny12mkhw7.jpg
tags: aws, cloud9, codeigniter, docker
---

Hey devs :computer:, I've been spending my days during lockdown trying get familiar with Docker & Amazon Web Services (AWS) Cloud9. 

As a PHP developer, I once was using Codeigniter 3 for developing applications. Then it came to my attention that Codeigniter 4 (CI4) was released. 

So I decided to combine these 3 and by using **AWS - Ubuntu Server 18.04 LTS** Platform, I will use **Docker** and have **CI4** working on it.

My AWS environment settings are as follows :

- Environment type : **EC2**
- Platform : **Ubuntu Server 18.04 LTS**


## Let the trip begin :sunglasses:

In your bash terminal on AWS, just to check the php version, we won't be using this one though.
```console
ubuntu:~/environment $ php -v
```

Normally you should see *php version 7.2*,
![PHP version](https://dev-to-uploads.s3.amazonaws.com/i/n560rn12whom8xl4c3lz.png)<figcaption>PHP version</figcaption>

I actually spent couple of days trying to built a nice Docker Environment comprising of

- nginx
- php-pfm
- database

It is available on Github

{% github fadilxcoder/docker-env %}

Clone the repository and move the files to the root of your project.

```console
ubuntu:~/environment $ git clone https://github.com/fadilxcoder/docker-env.git
ubuntu:~/environment $ mv docker-env/{*,.*} ./
ubuntu:~/environment $ rmdir docker-env/
```

![AWS](https://dev-to-uploads.s3.amazonaws.com/i/lsvqegda9fthsxnop3zt.png)<figcaption>AWS</figcaption>


Okay, from here i'll explain to you the file structure.

- In `src` folder, we will add only the application codes. It can be html, js, css, php files or assets (images / favicon). Overall , our codes goes in this folder.

- In `docker` folder, it consist of a docker-compose file where we have the configuration parameters for docker and other Dockerfile each for an image. Depending on your application, you might need to updated these files before building it.

Now we will get CI4 working!

## Let the trip continue :alien:

You will have to edit the Dockerfile in php-fpm as CI4 uses the intl php extension.

```console
RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
    install-php-extensions gd xdebug mysqli intl
```

Ans also in docker-compose.yml, change port as AWS by default uses port `8080`.

```console
    ports:
      - "8080:80"
```

AWS Environment does not have `docker-compose` command by default, you will need to install it by running `sudo apt install docker-compose`.

Once installed, go to docker repository and run : 

```console
ubuntu:~/environment/docker $ docker-compose up --build
```

It might take some time as it will have to download the image if it is not present.

Open up another terminal in your AWS and check the container status whether it is up and running,

```console
ubuntu:~/environment/docker $ docker ps
```

![AWS Docker Container](https://dev-to-uploads.s3.amazonaws.com/i/m6fk6bt8dysxbwjruwrz.png)<figcaption>AWS Docker Container</figcaption>

Now you will need to connect to your container terminal, 

```console
ubuntu:~/environment $ docker exec -it fx_php_fpm ash
/var/www/html # composer create-project codeigniter4/appstarter ci4
```

The installation will start...

![CI4 in Docker Container](https://dev-to-uploads.s3.amazonaws.com/i/91743ifdal6lp3ly565t.png)<figcaption>CI4 in Docker Container</figcaption>


## 95% complete :grin:

In the `src` folder, uncomment the `phpinfo();` save the file and the preview your application.

![php version 7.4.5](https://dev-to-uploads.s3.amazonaws.com/i/keujizga4orq4tn3xwp1.png)<figcaption>php version 7.4.5</figcaption>

To preview your CI4 application,

URL : `http://xxxxxxxxxxxxxxxxxxxxxxxxx.vfs.cloud9.us-east-2.amazonaws.com/ci4/public/`

![Codeigniter 4](https://dev-to-uploads.s3.amazonaws.com/i/kny0p350tp222kevusdr.png)<figcaption>Codeigniter 4</figcaption>

One problem that you might come across is that you may not be able to edit the code of the codeigniter app. 

But no worry :smiley:

Use the 2 commands below to change files ownership & group.

```console
ubuntu:~/environment/src $ sudo chown -R ubuntu ci4/
ubuntu:~/environment/src $ sudo chgrp -R ubuntu ci4/
```
![File Permission](https://dev-to-uploads.s3.amazonaws.com/i/osvo5zbqycsu9qpykku1.png)<figcaption>File permission</figcaption>

## Trip's over :sparkles::sparkles::sparkles: :punch: :heart: :v: :sparkles::sparkles::sparkles:

Now we have a fully functional docker container with Codeigniter on AWS C9 and you can code on it anytime/ anywhere...

![We did it folks]
(https://dev-to-uploads.s3.amazonaws.com/i/thr6r8m9m496jnpax8re.gif)