Category Archives: Amazon EC2

Installing a LAMP Web Server on Amazon EC2

In this blog post we will understand procedures that will help you install the Apache web server with PHP and MySQL support on your Amazon EC2 Ubuntu instance. We will go through procedure, step by step.

Prerequisites

We assume that you have registered for Amazon AWS account. If you have not registered, first of all register for it through this link .

After you have registered login to Amazon AWS account and go to AWS Management Console. From services, go to Amazon EC2

Launch Amazon EC2 Instance

First of all we have to create and launch Amazon EC2 instance. Click on Launch Instance. It will ask you to select Amazon Machine Image in Step 1

 

 

 

 

From here select Ubuntu Server. In next step it will ask you to select instance type. Depending on your application size and traffic, you can choose instance. For now we will go for default selected micro instance.

Click on Review and Launch. It will take you to final step, to review. From there click on Launch. It will show a pop up to create key pair. This will be used to connect with instance using SSH. Create and download key pair and save it to desktop. Now click on Launch Instance.

It will launch instance, once the instance is launched, click on View Instances. Once you click on running instance, it will show you all the details of instance like Public DNS, public IP etc. IP will use to connect with SSH.

Now, instance is up and running, we will connect with SSH and install necessary components.

Connect to instance with SSH

To connect we will use key pair created while we were creating instance. Go to terminal and go to directory, where you downloaded key and run following command.

chmod 400 YourKey.pem

This will change key permission to make it readable. Now connect to instance using public IP of your instance.

ssh -i “YourKey.pem” ubuntu@YourIP

Install Apache and PHP 7.0

sudo apt-get install apache2 php7.0 libapache2-mod-php7.0

Now verify it.

a2query -m php7.0

Load PHP module.

sudo a2enmod php7.0

Restart apache.

sudo service apache2 restart

New version of ubuntu comes with PHP 7 but to access it you have to install php command lines. Run following command.

sudo apt-get install php7.0-cli

Now test it with

php -v

Create Test PHP

Go to your apache root directory with following command

cd /var/www/html

There will be one index.html file. We have to remove this and create new file for testing.

sudo rm index.html

sudo vim index.php

This will open vim editor, press i and now you can type in file. Let’s create simple php file.

<?php
echo “Hello AWS”;
?>

press esc and : and type wq this will save file.

View test PHP file in Browser

To view file in browser, copy the public DNS and paste in browser. But wait it won’t work because Amazon EC2 instances have inbound rules where you have to configure what types of connections can be made to your instance. By default only SSH is allowed, we have to add HTTP connection.

For that go to security groups and in bound rule.

Now refresh browser and you will see Hello AWS message.

.htaccess Doesn’t Work in Amazon AWS EC2 Instance with Ubuntu

Recently I was configuring Laravel application in Amazon AWS EC2 instance which was having Ubuntu and Apache installed. The problem was htacess file was not working so Laravel routes were not working. Instead of parsing laravel routes it always shows error URL not found.

The problem is by default in apache installed on ubuntu doesn’t have .htaccess enabled so first you have to manually enable it in config file and restart apache.

Here are the steps.

1) Login to your EC2 instance with SSH.

ssh -i “Yourkey.pem” ubuntu@yourpublicip

2) Enable Mode Rewrite, this is just incase if it’s not enabled.

sudo a2enmod rewrite

3) Go to following directory.

cd /etc/apache2/sites-available

If you use LS command here you will see following file.

000-default.conf

That’s the file with default apache configuration which are applied to your sites in /var/www/html folder.

4) Open this file for editing.

sudo nano 000-default.conf

5) Add following lines after DocumentRoot /var/www/html line.

<Directory />
     Options FollowSymLinks
     AllowOverride All
</Directory>
<Directory /var/www/html>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride All
     Order allow,deny
     allow from all
</Directory>

6) Save the file and restart the apache.

sudo service apache2 restart

that’s it and now your .htaccess file will work and your laravel application will start working.

Upload File From Local Machine to Remote Server with SSH

Hello,

Recently while working on SSL on my Amazon EC 2 server I had to upload few certificates files from my local machine to EC 2 server. I had to spent some time to figure out this so thought of publishing blog on it so this may help others

First lets see how to upload file to remote server with permission on directory. We will use scp command for it. Following is the syntax.

$ scp /path/to/local/file sshuser@host:/path/to/remote/server

Once you run command it will prompt you for password. When you enter password it will upload file.

Now lets see what if you don’t have password and have public or private key. Following is the command.

$ scp -i “yourkey” /path/to/local/file sshuser@host:/path/to/remote/server

If you have specified correct key it will upload your file.

Now here comes the real problem with EC 2. I was trying to upload crt file to system directory which was not allowed. So what you have to do is upload that file default ssh directory with following command

$ scp -i “yourkey” /path/to/local/file sshuser@host:filename

And then go to that directory. Usually in amazon EC 2. It’s /home/ubntu directory. Then you can copy files from here using cp command with sudo.

I hope this helps you and dave your time.

Amazon EC2 File Upload Permission

Hello,

Recently in one of my project we hosted our PHP website on Amazon EC 2 which was running Apache on ubuntu. In our project we have file upload functionality which was not working. First I tired to set permission to 777 for the upload directory but still it was not working, so I went into details and found out this was the user permission issue. In this blog I am going to explain how to solve this.

Your website is running in Apache on Ubuntu. So there is a apache user, which is actually handling file operations. By default apache user does not have write permission and it does not own your website folder. So there are three basic steps to resolve this issue.

1) Find out your apache user.

Fist we have to find out name of apache user. Run following command in terminal.

ps -ef | grep apache

Check the left most column of the output. See the image below. That is name of your apache user.

In my case it was www-data user. 
2) Change directory owner to Apache user.
Change owner of your upload directory.
sudo chown www-data /var/www/html/uploads

3) Setup permission on your directory.
sudo chmod 755 /var/www/html/uploads
That’s it. Now you can upload file from your web application. Hope this helps you and save your time.