Do me a favor, take a few seconds to have a look at my last project.

Thank you, Lulu

# How to configure and run Apache server on AWS EC2 Cloud9?

## Introduction

Migration from Cloud9 to AWS Cloud9 was not easy. On cloud9, Apache was installed and configured by default. On AWS, Apache is not properly configured, you have to manually configure the server. This page presents a step by step guide.

The following has been tested on a freshly AWS EC2 Cloud9 environement with the following versions:

## Install and run Apache

AWS Cloud9 environements come with Apache already installed. Launch a terminal (Alt-T), and run the following command:

sudo service apache2 status

The Apache server is active and running:

Unfortunatly, it is not configured to work with the Cloud9 development environment. If you click on Preview Running Application, the IDE browser displays something like:

## Configure Apache on port 8080

To run with the Cloud9 preview system, Apache server needs to run on ports 8080, 8081 or 8082.

First, edit the file /etc/apache2/ports.conf by running one of the following commands:

sudo vim /etc/apache2/ports.conf
sudo nano /etc/apache2/ports.conf 

Replace the first line:

Listen 80

by

Listen 8080

Edit the file /etc/apache2/sites-enabled/000-default.conf with one of the following commands:

sudo vim /etc/apache2/sites-enabled/000-default.conf
sudo nano /etc/apache2/sites-enabled/000-default.conf 

Replace the line:

<VirtualHost *:80>

by

<VirtualHost *:8080>

Restart Apache with the following command:

sudo service apache2 restart

The preview should now be displayed when clicking on Preview Running Application:

## Set Apache root directory

Apache is configured with the root directoy located at /var/www/. Of course, as we want to use the Cloud9 IDE, we need to change Apache root directory for /home/ubuntu/environement/.

Edit the file /etc/apache2/apache2.conf with one of the following commands:

sudo nano /etc/apache2/apache2.conf
sudo vim /etc/apache2/apache2.conf

Find and replace the following line:

<Directory /var/www/>

by

<Directory /home/ubuntu/environment/>

Edit the file /etc/apache2/sites-enabled/000-default.conf with one of the following commands:

sudo nano /etc/apache2/sites-enabled/000-default.conf
sudo vim /etc/apache2/sites-enabled/000-default.conf

Find and replace the following line:

DocumentRoot /var/www/html

by:

DocumentRoot /home/ubuntu/environment/

Restart Apache with the following command:

sudo service apache2 restart

Create a file index.php at the root of your Cloud9 environement (/home/ubuntu/environment/):

<?php
echo '<h1>Apache root directory changed !</H1>';

Refresh the IDE browser, your PHP script should be running:

Important: I noticed that the internal Cloud9 browser do not always refresh the page properly. I don't know how the cache is working, but I noticed strange behaviors. I suggest to test the server on a new window and avoid using the internal Cloud9 browser.

## Configure .htaccess

The basic configuration of Apache server does not allow .htaccess file to run properly. Let's check it by creating a new .htaccess file with the following content (an environment variable accessible from the PHP scripts):

SetEnv ENVAR test

Update the file index.php created previously:

<?php
echo '<h1>Apache root directory changed !</H1>';

var_dump(\$_SERVER['ENVAR']);

Refresh the preview in the browser:

.htaccess is not enable, let's enable it. Edit the file /etc/apache2/apache2.conf with one of the following commands:

sudo nano /etc/apache2/apache2.conf
sudo vim /etc/apache2/apache2.conf

Find and replace the following lines:

<Directory /home/ubuntu/environment/>
AllowOverride None
Require all granted
</Directory>

by:

<Directory /home/ubuntu/environment/>
AllowOverride All
Require all granted
</Directory>

Enable mod_rewrite with the following command:

sudo a2enmod rewrite

Restart Apache in order to apply changes:

sudo service apache2 restart

Et voilà !