Azarbod Blog

Here discuss anything related to IT, Software Systems for Manufacturing and Distribution or the latest Azarbod Events.

How to Setup LAMP Stack on Amazon EC2 Machine

Mon, Feb 01- 2016


I recently moved one my of our systems to Amazon Cloud. It was previously hosted on a shared hosting, but we needed root access to be able to use PHP Composer Dependency Manager. That is why I had to set it up and move it to an Ubuntu EC2 machine on Amazon.

It took me about 3 days to figure it out, (maybe because I am not good at setting up servers) and that is why I decided to share the experience here. It might help some other people in the same situation.

Please Note: The instructions here are geared only towards my own needs for a PHP LAMP stack under Ubuntu Server and not anything else. If you are looking for any other variation please consult Amazon guides or webinars.


Step 1: Setup Ubuntu under EC2

  • Go to http://aws.amazon.com and login to your amazon account
  • Then on the top left corner, click on EC2 to go to your EC2 dashboard
  • On the dashboard, if you do not have an already running instance, click on Launch Instance
  • On the first step of the wizard, pick Ubuntu and on the next one pick a server that fits your computing requirements. I picked General Purpose (t2.micro)

Security on EC2

Generally when you connect to a linux machine via SSH, you will provide liux user name and password after connecting to the actual box. On Amazon EC2, it is different. When setting up the EC2, you should create and encryption key. This key is used by say Putty SSH to connect to the instance. If you do not create the key or loose it, then your machine is gone. Please follow the below instructions on how to create the key.

  • On the next screen click launch and then it shows a pop-up for key pair. This key is required to connect to the instance via SSH (say Putty.) in place of user name/password.
  • If you already have created a key (say for your other instances,) here you pick an existing pair and check the check box. Otherwise you have to create a new key pair and download it to a secure place to be used as later explained.
  • If just created, it will create a key with .pem extension. Save it somewhere safe for later use.
  • Then on security page make sure to add public to all protocols or the instance will not be accessible
  • Now in order to use Putty, you have to convert the .pem file to .ppk. In order to do so you should use puttygen.exe that comes as part of the Putty download package.
  • After you run puttygen.exe, click load and then select All Files (*.*) and browse to the saved .pem file. Once loaded, click Generate and do not put any passphrase when warned. Passphrase adds another annoying layer of security. At the end you save it as a .ppk file somewhere safe.
  • Now run Putty.exe. In the host name put ubuntu@IP Address of the instance. It will be like: ubuntu@172.198.12.33 There is no password and leave the port to default 22. Then click Connection -> SSH -> Auth and the browse to the generated .ppk file.
  • Now you are connected to the newly setup EC2 box via SHH and can start the rest of the LAMP Stack.

Step 2: Install Apache under the EC2

Note: Do not use yum that is in the Amazon docs. Seems the docs are not for Ubuntu.

  • Get the latest updates
  • $ sudo apt-get update
  • Install Apache
  • $ sudo apt-get install apache2

Step 3: Install PHP under the EC2

Now install PHP. The second step is probably not needed.

  • $ sudo apt install php libapache2-mod-php
  • $ sudo apt install php-cli
  • $ ssudo apt install php-cgi
  • $ ssudo apt install php-mysql
  • $ sudo /etc/init.d/apache2 restart
  • or
  • $ sudo service apache2 restart

Step 3: Install FTP Server under the EC2

  • $ sudo apt-get install vsftpd
  • Add a user for FTP
  • $ sudo adduser *ftp-user* [replace *ftp-user* with your choice of name]
  • It will ask for password and retype of the password
  • The folder /var/www/html is the root folder for Apache. We have to change the owner of this folder to the new *ftp-user* or we can not write here:
  • $ sudo chown *ftp-user*:*ftp-user* /var/www/html
  • Edit file /etc/vsftpd.conf
  • $ sudo nano /etc/vsftpd.conf
  • then add this line to end of this file
  • allow_writeable_chroot=YES
  • Also uncomment the line
  • write_enable=YES
  • Make sure to disable anonymous connection
  • anonymous_enable=NO
  • save the file and exit.
  • Restart FTP service
  • $ sudo service vsftpd restart

Step 4: MySQL

  • $ sudo apt-get install mysql-server
  • $ sudo service mysql start
  • Restart Apache
  • $ sudo service apache2 restart

Step 5: Install PHPMyAdmin

  • $ sudo apt-get install phpmyadmin
  • Use apache2 for web server. Update apache cofig file
  • $ sudo nano /etc/apache2/apache2.conf
  • and then add this line to the end of it:
  • Include /etc/phpmyadmin/apache.conf
  • Increase Session Timeout
  • Go to folder /etc/phpmyadmin
  • edit config.inc.php
  • $sudo nano config.inc.php
  • and add this line to the bottom of the file
  • $cfg['LoginCookieValidity'] = 36000 ; // Or any other number
  • Then restart apache or maybe not required
  • $ sudo service apache2 restart
  • Note: Somewhere was saying to change host, I did but seems it was not needed
  • Restart apache
  • $ sudo service apache2 restart
  • Then use http:// EC2 public DNS/phpmyadmin/ to connect to phpmyadmin
  • Set PHP to dispaly error
  • $ sudo nano /etc/php5/apache2/php.ini
  • Then search for display_errors and display_startup_errors and change them to:
  • display_errors=On
  • display_startup_errors=On
  • Restart apache
  • $ sudo service apache2 restart

Step 6: Install Composer

  • create composer.json
  • Add something like
    {
    "require": {
    "phpoffice/phpword": "v0.12.1"}
    }
  • v0.12.1 should come from packagelist.org. Click on package name and use that version
  • Then in that folder run this command:
  • $ sudo composer install

Reza Zare
January 2016