User Friendly tutorial on setting up FTP, PHP, MySQL and Apache on Amazon’s Elastic Compute Cloud, or EC2 Instance ~ Hybrid Mobile Apps Development, React Native, Flutter, JavaScript, Darts, iOS, Android, NodeJS
Coding Savvy FB Twitter Google
» »

User Friendly tutorial on setting up FTP, PHP, MySQL and Apache on Amazon’s Elastic Compute Cloud, or EC2 Instance

This might not work for you due to Amazon Aws updates, Please visit Amazon aws EC2 'Elastic Cloud' Setup with Ubuntu, PHP, Apache, Node.js, MySQL and PHPMyAdmin Installation for the updated article.
I just set up my own Amazon’s Elastic Compute Cloud, or EC2, After lot of Googleling and reading I finally found a solution to it but it took me time before i got my hands on it, so if you have been in my shoes and you found this site I congratulate you because today is your lucky day. Am writing this tutorial to help people like me who are willing to create their own Amazon’s Elastic Compute Cloud, or EC2 Instance and also we are going to discuss how to link your instance to your Active Domain name. This is a very user friendly tutorial for EC2 Setup, This tutorial will lead you to huw to setup and install PHP, MySQL Apache and FTP on your EC2 instance but you can always use the same procedure to install any other type of server you wist o install too.
This might not work for you due to Amazon Aws updates, Please visit Amazon aws EC2 'Elastic Cloud' Setup with Ubuntu, PHP, Apache, Node.js, MySQL and PHPMyAdmin Installation for the updated article.
I figure Setting up Amazon’s Elastic Compute Cloud, or EC2 is actually difficult for most developer who has never used it before and I also noticed that sometimes when questions concerning the Amazon’s Elastic Compute Cloud, or EC2 is asked on programming forums it very hard to get a straight forward answer.

Required Steps

There are a few steps that need to be completed before you can start installing things and using an EC2 instance to host your site or blog. We need to: Create Amazon EC2 account(https://console.aws.amazon.com) Set up an EC2 instance Create a key pair file for use with SSH Create a security group and open any required ports Set up an Elastic IP address All of these tasks are also relatively straightforward. Amazon provides a lot of options when setting up an instance, but for this guide we’ll mainly be sticking to the default options so we don’t over complicate things.
 

Step 1: Setting up an Amazon EC2 Instance 

This process should not take you time just visit http://aws.amazon.com and log in using your Amazon credentials.You can also sign up from thesame page as a new user if you don't have an amazon account. Amazon will request for your credit card and contact detail's that is if it not in their database already, Don't worry you won't be charged for anything yet until you actually start using their web services. In this tutorial we are going to start with Amazon free usage tier for EC2 but this service is only free for a year after which you will have to pay for more information visit http://aws.amazon.com/free/ and for more on their pricing see http://aws.amazon.com/ec2/pricing/. Amazon EC2 free tier let you run a free Micro instance for one year after you sign up, yu your free to get your hands dirty with the instance without paying a dime.

Setting up an Amazon EC2 Instance

Let start by setting up Amazon EC2 Instance to host a blog site but befor we can get that done we need to Set up an EC2 instance, Create a key pair file for use with SSH, Create a security group and open any required ports ,Set up an Elastic IP address. Login to you Amazon aws account after the initial signing up you still need to sign up for another account, The process is straight forward just click on EC2 tab and complete the sign up process from there.
During the sign up, Your active phone number will be required as a means of verification and also in case Amazon wants to reach you via your phone, To prove this Amazon will place an automated call on your number and you will need to enter the provided code to verify your identity, This service is available to all countries.No let stick our hands in the mud already.

Set up an EC2 instance 

If you are no there already go back to the EC2 tab and click on Launch Instance button on the dashboard you will see a new window popping up in case you were given an option to choose a wizard chose classic wizard, From her you will be shown a list of Amazon Machine Images, or AMIs, which are basically snapshots of virtual servers which have access to repositories of programs to install. The easiest way to start an instance is to select one of the AMIs in the Quick Start tab. The best option if you are just starting out is to use one of the AMIs provided by Amazon or Ubuntu, which guarantees reliability and authenticity. Now,Choose the Amazon Linux AMI,which will most likely be at the top of the list. You can choose 32bit or 64bit instance it doesn't really make any difference right now. On other setting detail you can leave the setting as they are just make sure that the type of instance you select is Micro. On the next step just leave the options at their defaults, then click continue. On the next step you are allowed to define up to 10 tags as key-value that helps you identify the current instance from the rest but since this is your first instance just use the default Name key and give it a value of whatever you want.

Create a key pair file for use with SSH

In this next step you will be ask to create e a Key Pair file. This is a very important step,as this key will serve ass your password to the EC2 instance Via SSH which we will be using to install all the programmes and do all the configurations of your instance. Let name the key as myinstance1 download the file If you’re on a Mac, a good place to store the file is in the ~/.ssh directory, which is a hidden folder you’ll find inside your User folder. If you can’t see your hidden folders, follow the guide at http://www.mactricksandtips.com/2008/04/show-hidden-files.html, which shows you how to view hidden folders on a Mac.
After you have copied your key file to ~/.ssh or whichever directory that you have chosen to use, you will also need to change the permissions for your key file to make it only readable by you. To do this, open up Terminal and switch to the directory you stored your key file in like so.

cd ~/.ssh

Then, enter the following command :

chmod 400 name_of_your_key_file.pem

In case you want to know what this dose Basically, the chmod command is used to permissions for a file. The three digits are permissions for the Owner, Group and World respectively and range in value from 0 to 7, with 0 being no permissions and 7 being full read, write and execute permissions. The command chmod 400 is giving the file’s Owner, which is you, read permissions, and giving everyone else no permissions at all, which is exactly what we want for our key file.  

Windows 

If you are using windows your browser should save it in your Download Directory by default if not you can move it there. you will most likely be using PuTTy for SSH commands. PuTTy uses the extension .ppk for its key files, while Amazon creates a .pem file. You will need to run this .pem file through the PuTTygen tool to use it, which you can download along with PuTTy here.
 http://www.chiark.greenend.org.uk/%7Esgtatham/putty/download.html.
Converting .pem to .ppk

This step is only necessary for window users. Lunch Puttygen -> File -> Load Private Key Under Type of key to generate, select SSH-2 RSA. Click Load. By default, PuTTYgen displays only files with the extension .ppk. To locate your .pem file, select the option to display files of all types. Select your .pem file and click Open. Click OK to dismiss the confirmation dialog box. Click Save private key to save the key in the format that PuTTY can use. PuTTYgen displays a warning about saving the key without a pass phrase. Click Yes.

Create a security group for our instance and open any required ports 

All instance needs to be under a security group, if non is chosen the default security group is automatically selected they are used to tell the instance which ports to open, which will determine the kind of access and command that your instance can accept. Since you have already created an instance we can check for the security group under the description bar. Select your instance and see the description bar for the security group it belongs to. On the left menu bar in your EC2 account click on Security Groups click on the security group of your instance and on the tab below the group list click on Inbound rules add the following rules
  • HTTP port:80 Source:anywhere
  • SFTP port:12000–12100
  • SSH
If you want you can add SMTP, HTTPS. for all leave the source as 0.0.0.0/0 i.e anywhere, which is All Internet. Lastly,review all the setting for your instance and click the Launch button to launch your instance, This process might take few minutes. You can head back to the Instances section of the EC2 dashboard and watch the status of your instance change in real time.  

Setting up an Elastic IP for the Instance. 

The last step in setting up our EC2 Instance is to associate an Elastic IP Address to it, Elastic IP lets you point a DNS to the instance, and makes it easier to connect third party programs,Website or apps to your instance, for example those that handle FTP file transfer. Another benefit is that if you have multiple instances running, and your live instance fails, you can simply reallocate your Elastic IP to a different instance and have your site back up and running right away. Select Click Elastic IP on the left hand side menu. Then click on Allocate New Address and click Yes, Allocate on the pop up that appears. Now for the final step, right click on the IP address you've just created and select Associate, type your instance name, as you Begin typing you instance name should drop down, Select your instance name from the drop down menu, then click Yes, Associate. Congratulations you are done setting up your first EC2 Instance no lets start to install PHP, mySQL, phpMyAdmin and FTP on our instance using SSH.

 Setting up and Installing PhpMyAdmin,PHP, MySQL Database and FTP on our Instance

  Secure Shell (SSH) is a network protocol for secure data communication, remote shell services or command execution and other secure network services.{wikipedia} For windows users 

We will be needing the .ppk file we saved earlier to complete the SSH connection. 1. Start PuTTY (from the Start menu, click All Programs > PuTTY > PuTTY). 2. In the Category pane, select Session and complete the following fields: In the Host Name box, enter user_name@public_dns_name. Be sure to specify the appropriate user name for your AMI. For example: For an Amazon Linux AMI, the user name is ec2-user.
 For a RHEL5 AMI, the user name is either root or ec2-user. For an Ubuntu AMI, the user name is ubuntu. For a Fedora AMI, the user name is either fedora or ec2-user. For SUSE Linux, the user name is root.
Otherwise, if ec2-user and root don't work, check with the AMI provider. Under Connection type, select SSH. Ensure that Port is 22. 3.
In the Category pane, expand Connection, expand SSH, and then select Auth. Complete the following: Click Browse. Select the .ppk file that you generated for your key pair, and then click Open. (Optional) If you plan to start this session again later, you can save the session information for future use. Select Session in the Category tree, enter a name for the session in Saved Sessions, and then click Save.
Click Open to start the PuTTY session. If this is the first time you have connected to this instance, PuTTY displays a security alert dialog box that asks whether you trust the host you are connecting to. (Optional) Verify that the fingerprint in the security alert matches the fingerprint that you obtained in step 1. If these fingerprints don't match, someone might be attempting a "man-in-the-middle" attack. If they match, continue to the next step. Click Yes. A window opens and you are connected to your instance.  

MAC Users 

SSH is run via a command line program such as Terminal for the Mac, and allows you to send commands to your instance over a secure, encrypted connection.  

Connecting to our Instance via SSH 

Go to the Instances section of the EC2 tab, right click on the instance you want to connect to and click connect. You will See a pop up window with two connection options; Connect with a standalone SSH Client and Connect from your browser using the Java SSH Client (Java Required). We are going to connect with a standalone SSH client, Terminal, so choose that option. Because we already set the permissions for our key file using chmod, we can just skip to step 4 of the process. Switch to your .ssh directory in Terminal using the command : First of all in the command terminal switch to your SSH directory using this command

cd ~/.ssh

MAC only 

And then enter the command that Amazon gives you to connect to your instance, which will be something like :

ssh -i myinstance1.pem ec2-user@23.45.679.90

All Users 

First let apply update.

sudo yum update 
This command will update your instance but let try and get to know some SSH commands
 sudo – Adding sudo before any commands allows you to run the command as the Unix root or superuser instead of the ec2 user. We will be using this in front of most commands as the ec2 user doesn’t have many install or other security privileges. yum – Yum is a software installation tool for Red Hat/Fedora Linux, which is what Amazon’s Linux AMI uses. We will be using yum to install any packages, such as PHP and mySQL, on our server. vi – Vi is the visual editor that comes with Unix systems, and it allows you to edit files on the instance using the Terminal window. Vi operates in both a command mode and an insert/edit mode, and I will go over a few of the commands later when we start editing files with it. Let install Apache first.

sudo yum install httpd

After Apache installation we need to edit Apache configuration file httpd.conf uding VI Editor. Let see some VI Editor commands i – Enter insert mode. This will allow you to edit the contents of the file. Esc – Enter command mode. This will allow you to run commands on the vi editor, such as saving and quitting. While in command mode :q! – Quit the editor without saving changes. :wq – Save changes and quit the editor. G – Hold down shift and press g to move the cursor to the last line of the open file. see more command here http://www.cs.colostate.edu/helpdocs/vi.html Open the httpd.config file in the editor by entering the following command:

sudo vi /etc/httpd/conf/httpd.conf

We are here to set up a virtusl host on port 80 on the server.Virtual Hosts allow you to run multiple websites on the same server, which can be IP address based or name based. After the last command your terminal should show you the virtual editor diplaying your httpd.conf file, Now scroll to the buttom of the file you can do this by pressing G on MAC and if you are using windows you can press the insert button to switch to edit mode. towards the buttom of the file you will see a section like this :
# # ServerAdmin timotewpeters@gmail.com
 # DocumentRoot /var/www/html/ 
# ServerName ec2-52-24-252-24.us-west-2.compute.amazonaws.com
 # ErrorLog logs/dummy-host.example.com-error_log
 # CustomLog logs/dummy-host.example.com-access_log common
 #
 
Make the section look like this Replace the Server name with your own ec2 public DNS: you can get this from your instance detail tab which will be similar to the one below
Replace the server admin with your own Email
leave ErrorLog and CustomLog comment out
Change the DocumentRoot to : /var/www/html/

ServerAdmin timotewpeters@gmail.com 
 DocumentRoot /var/www/html/ 
ServerName ec2-52-24-252-24.us-west-2.compute.amazonaws.com 
# ErrorLog logs/dummy-host.example.com-error_log 
# CustomLog logs/dummy-host.example.com-access_log common

Now scroll toward the middle of the file you will see  
DirectoryIndex
edit the section to look like this:

DirectoryIndex index.html index.php index.sh default.html default.jsp

This setting allow PHP file to be the Directory Index file. The file name does not need to be index.*, this is usually just the default file name that most websites use. After you have changed everything, press Esc to enter command mode and type :wq to save and exit. Finally, start the Apache server using the command:
sudo service httpd start

Your site will now be visible to the public. you should now see your site using the public dns of your website.

 Installing PHP, mySQL and phpMyAdmin

  To ensure that all software packages are up to date, we need to run the following command on the instance to perform a quick software update.
sudo yum update -y

To install everything you need for PHP, just run this command:
sudo yum install php-mysql php php-xml php-mcrypt php-gd php-mbstring php-cli mysql

The command above will install PHP and the command required by PHP. Next, we will install mySQL. Install and run the mySQL server with these commands, and say yes to all of the prompts:
sudo yum install mysql-server
sudo /etc/init.d/mysqld start

let set the password for the recently installed MySQL.
Type your password without the quotes
mysqladmin -u root password 'MyPassword'

Installing phpMyAdmin

Apache,MySQL has been installed successfully and running,Now to make the MySQL interaction more user friendly we need to install phpMyAdmin. phpMyAdmin is a web based interface for administering mySQL, including managing users and setting permissions, creating databases and queries, and handling other similar database administration tasks. The process of installing phpMyAdmin takes the most steps in this guide.
First, change directory to the Apache root folder:
cd /var/www/html

Let download phpMyAdmin into the html folder:
sudo wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/3.3.9.1/phpMyAdmin-3.3.9.1-all-languages.tar.gz

Next, extract the files to the root of the folder:
sudo tar -xzvf phpMyAdmin-3.3.9.1-all-languages.tar.gz -C /var/www/html

Rename the folder to phpmyadmin and remove the zip file:
sudo mv phpMyAdmin-3.3.9.1-all-languages phpmyadmin
sudo rm -rf phpMyAdmin-3.3.9.1-all-languages.tar.gz

Create a Unix user for phpmyadmin and give it permission/ownership over the phpmyadmin folder.
sudo adduser phpmyadmin
sudo passwd phpmyadmin (After this you will be prompted to enter a password:enter the password for your mysql)

Give the user phpmyadmin permission/ownership over the phpmyadmin folder, first by finding which user Apache uses by running:

egrep 'User|Group' /etc/httpd/conf/httpd.conf

And then, after finding this user, by changing to the root Apache directory and running the chown command on the phpmyadmin folder.
cd /var/www/html
sudo chown phpmyadmin.apache phpmyadmin/

Now, run this set of commands one after the other line by line:

cd /var/www/html/phpmyadmin/
sudo mkdir config
sudo chmod o+rw config
sudo cp config.sample.inc.php config/config.inc.php
sudo chmod o+w config/config.inc.php
sudo service httpd restart

This changes your directory to the phpmyadmin folder, makes a config directory and sets permissions for it, copies a sample config file for phpmyadmin to the config directory and renames it then adds permissions to the config.inc.php file. Finally, the Apache server is restarted.
We need to configure the recently installed phpMyAdmin
Navigate to http://[your_instance_IP or public DNS]/phpmyadmin/setup/index.php and click New Server.
The only thing that need to be changed on the next screen is the PHP extension from mysql to use to mysqli if it is not already selected, and then enter the config auth user, which will be root, and config auth password, which is the password we entered earlier for the root mySQL user.
Click save and you will be done. Don’t worry about the errors that appear after the setup, we will be fixing them up next.
You can now navigate to http://[your_instance_IP]/phpmyadmin/index.php and log in using the root user credentials.
We will now handle the errors shown. One should be something along the lines of The configuration file now needs a secret passphrase (blowfish_secret)., and the other should be telling you to delete the config folder in the phpmyadmin directory. There will also be another error saying something along the lines of The additional features for working with linked tables have been deactivated. 
To find out why click here., but don’t worry about that for now. We’ll fix the first error, and set up some other things in the config file, before deleting the config folder.
Open the phpMyAdmin config file, config.inc.php, by running this command:

sudo vi /var/www/html/phpmyadmin/config/config.inc.php

Near the top of the file you will see this line:

$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

All you need to do here is enter a random string of characters between the quotation marks. Scroll down to the /* Server Parameters */ section, and change

$cfg['Servers'][$i]['extension'] = 'mysql';

to
$cfg['Servers'][$i]['extension'] = 'mysqli';

When you scroll down a bit further and you will find the section /* User for advanced features */.
Leave the control user and the control password commented out, as we will not need them, and uncomment all of the other lines starting with $cfg in this section and the two below it. Save your changes by pressing Esc and entering :wq. Change directory back to the phpmyadmin config folder.
cd /var/www/html/phpmyadmin/config

The setting up of phpMyAdmin is almost complete, We just need to complete a couple more steps to ensure the installation is successful and all of the components are correctly configured, and to get rid of the final setup error.

First, log into phpMyAdmin and go into the Privileges tab, and select Add a new user. Enter phpmyadmin as the user name, change the Host to Local, then enter a password for the user. Select Create database with same name and grant all privileges, then click Check All in the Global Privileges section, then click Go and the database and user will be created.

Setting up and Installing SFTP

We will be installing vsftpd SFTP server on our instance. using the following command:
sudo yum install vsftpd

Next we need to edit the configuration file of vsftpd using this comand:
sudo vi /etc/vsftpd/vsftpd.conf

In here we will be changing a few properties to make the SFTP server more secure. Change the properties listed to the values shown.
anonymous_login=NO
local_enable=YES
write_enable=YES
connect_from_port_20=NO
chroot_local_users=YES (you may need to uncomment this)
local_umask=022
Avoid leaving excess space during editing of files i.e it should be /local_enable=YES/ not /local_enable=YES / try to avoid unwanted spaces
There is also some lines that we will need to add to this configuration file to allow passive connections to the FTP server using the ports that we defined earlier in the security group for the instance (12000–12100). Add these lines to the bottom of your config file, then save and exit using: ESC and typing :wq.
pasv_enable=YES
pasv_address=your_instance_IP
pasv_min_port=12000
pasv_max_port=12100
port_enable=YES

Now we will be securing the FTP upload directory to the ec2-user, so only they can read/write to the directory. First we will set the ec2-user to the owner of that directory and set the correct read/write permissions for that directory:

sudo chown -R ec2-user /var/www/html
sudo chmod 775 /var/www/html

After that, We will need to create a .userlist file and add the ec2-user to it, which will be used in vsftpd.conf as a list of users to give access to the FTP directory:
sudo vi /etc/vsftpd/vsftpd.userlist

Once in edit mode for the file, add the ec2-user user to it, save and quit. Now,Open the vsftpd.conf file again using:
sudo vi /etc/vsftpd/vsftpd.conf

and add the following lines:
userlist_file=/etc/vsftpd.userlist
userlist_enable=YES
userlist_deny=NO

Finally for this section we need to add nologin to the shell set to connect.Open
sudo vi /etc/shells

which should look something like:
/bin/ksh
/usr/bin/rc
...
Add the line
 /usr/sbin/nologin
to the end of the file.
To finish off, create a usergroup and add the ec2-user to it, then start up the vsftpd service:
sudo groupadd ftpusers
sudo usermod -Gftpusers ec2-user
sudo service vsftpd start

You will now be able to connect to your instance and upload/download files using SFTP in your preferred file transfer application.

Connecting to the Instance via FileZilla

You would not want to be uploading file to your instance using the SHH terminal all the time, would you?
Download FileZilla to your pc after the installation open fileZilla
Edit (Preferences) > Settings > Connection > SFTP, Click Add key file
Browse to the location of your .pem file and select it.
A message box will appear asking your permission to convert the file into ppk format. Click Yes, then give the file a name and store it somewhere.
If the new file is shown in the list of Keyfiles, then continue to the next step. If not, then click "Add keyfile..." and select the converted file.
Goto Files->Site Manager-> New Site.
in the host field enter ec2-your_instance_pulic_dns something like this ec2-52-24-252-24.us-west-2.compute.amazonaws.com
in the Logon type set it to Normal on the protocol select SFTP - SSH File Transfer Protocol
set the username as ec2-user and leave the password and port field blank, Tmezone offsets to 0.

Press Connect Button - If saving of passwords has been disabled, you will be prompted that the logon type will be changed to Ask for password. Say OK and when connecting, at the password prompt push OK without entering a password to proceed past the dialog.
Now Your should have access to your instance files, That's all.

Sources :

  • http://www.martin-brennan.com/php-mysql-and-ftp-on-an-amazon-ec2-instance/
  • http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html
  • http://stackoverflow.com/questions/16744863/connect-to-amazon-ec2-file-directory-using-filezilla-and-sftp
  • http://docs.aws.amazon.com/gettingstarted/latest/wah-linux/getting-started-deploy-app.html
Was this article helpful?
Thanks! Your feedback helps us improve tutorials.

You May Also Like...

No comments:

Post a Comment