Apache, PHP, MySQL and FTP Server on EC2(Linux) : Understanding, Installing and Configuring

So you want to use cloud instead of traditional web hosting, Nice Choice. But you need to know few things to quickly get an overview of what is the difference and why cloud is better than normal web hosting.
No! I am not going to give you a Lecture with a bunch of definitions and all. You can find them elsewhere!
Apache, PHP, MySQL and FTP Server on EC2(Linux) : Understanding, Installing and Configuring software guides


Normal Web hosting :

  • You get access to some storage on someone else’s hard disk.
  • Everything will be already there, you just get a user account from the existing ones for you to use.
  • You get something for a fixed cost , use it or not just pay because you bought it !!

Cloud Service :

  • You get an entire computer just not laying on your own desk.
  • You need to install and configure what all software you want like apache for Web Server, MySQL for database ,etc.
  • You will be charged only for the things you use, along with scalability option anytime or even automatic scaling!.
  • Enough. This much is more than enough for now!! Let’s get into the core thing of setting up a Linux server and launching it.
How to create an account and all, are not included , that’s easy. You can refer to Amazon Documentation on EC2 getting started up-to “Step 3: Connect to Your Instance”(including step3). I am not explaining the above because you can find it in the best place and more over its already there, so it’s not needed for me to replicate it again over here. Get back to this page when you are done.
Now that you can connect to your Linux Box , we need :
  • To make that system a Web Server so that it can handle requests from clients and serve your need. We will install the most popular server “Apache” for this.
  • To make it run PHP and alongside a MySQL Database. We will install the respected ones separately for this.
  • To make it a FTP Server for easy further access. We install VSFTP for this.
When EC2 console is still open , go to the “Network and Security” section of the Amazon EC2 Console and under “Security Groups” select the group you used for your instance and added port 22 for SSH if you remember(mostly ‘quicklaunch-1’) and add the following ports by entering them in the ‘port range’ field and click on ‘Add Rule’ Button.
  • 80 (for HTTP main thing!)
  • 443 (for HTTPS)
  • 20-21 (for FTP in and out)
  • 1400-1450(for FTP Passive Mode)
  • 3306(for MySQL remote access)
Don’t move on without clicking ‘Apply Changes’ .
Connect to the system using any SSH client as you may have read on the Amazon’s getting started page above and follow up by typing the things said below  in the Linux shell.

Login as root:

# sudo su
Enter the root password if prompted. Follow this(coming soon) tutorial to get the root access if you don’t have it already.

Installing Apache :

Type in:
# yum install httpd
This will quickly print some lines to the console stopping at [y/n] after showing you the download size .Just press ‘y’ and  ‘enter’ to continue installation.Do the same in further installations as well.
Make it start on system start-up automatically by the following command:
# chkconfig httpd on
Then start the server by:
# service httpd start
Apache’s access , error logs are found in /var/log/httpd by default.
Ok now, we ‘ill quickly configure some basic things to make a classic server.
Do the following to apache’s .conf file usually   /etc/httpd/conf/httpd.conf
Open the file by typing :
# vi /etc/httpd/conf/httpd.conf
<press i to enter insert mode for vi editor>
To disable directory browsing ,
Find the line :

Options Includes Indexes FollowSymLinks MultiViews

And then remove the word Indexes from that line which then becomes :

Options Includes FollowSymLinks MultiViews

For basic root directory security ,
Find the <directory/> section and make the changes needed for the options to look like this:
<Directory />
	AllowOverride None
	Order Deny,Allow
	Deny from all
Save Changes to the file by :
<press ESC to get out of insert mode and then type :wq to Write(save changes)
and Quit the editor>
Restart the server for changes to take effect by :
# service httpd restart
Apache’s Document Root i.e., http server’s root directory is /var/www/html by default.

Installing PHP :

Type in :
# yum install php-mysql
This will do it all.
php.ini path : /etc/php.ini
Errors will be logged into the apache’s error log file found at /var/log/httpd by default.
Restart apache to tell it that you have installed PHP.

Installing MySQL :

Type in :
# yum install mysql-server
Make it start on system start-up automatically by the following command:
# chkconfig mysqld on
Then start the service by:
# service mysqld start
Now let us configure it . This is important, so go on.
Setting the root password :
# mysqladmin -u root password '<*enter new password here>'
*Enter the password in between the quotes.
Login as root and remove anonymous user account :
# service mysqld restart
# mysql –u root –p
<enter root password you set above>
# mysql> DELETE FROM mysql.user WHERE user = ''; 
Done with MySQL and Done with all. Finally restart httpd and mysqld.

Finally Installing VSFTP :

Type in :
# yum install vsftpd
This will install it. But now we need to add passive mode for remote access.
The vsftp’s configuration file path is /etc/vsftp/vsftpd.conf
Open the file in the same way as we did for httpd.conf
Add the following lines to the .conf file :
pasv_address=<enter elastic IP here>
Max and Min ports are the ones’ we added to the security group in the starting of this whole thing.
Optionally instead of using the elastic IP you can use your domain name by replacing the last line with :
pasv_address=<enter your domain here>
Save the file and restart vsftpd service for the changes to take effect in the same way as we did for httpd. Also make it automatically start on system start-up using chkconfig command as done above.
By default you can access the ftp server by logging in using the same system username and password but the root user is blocked for security reasons (you can change it!).
Note: To set a password for system user accounts use passwd command.
Now you can connect it using FileZilla or any other FTP client via FTP instead if SSH.
All Done! Now you have a remote system on EC2 running Amazon Linux OS, Apache HTTP Server with PHP ,MySQL Server ,FTP Server.
Share, Comment and Subscribe if you think this blog post deserves it. Cheers Apache, PHP, MySQL and FTP Server on EC2(Linux) : Understanding, Installing and Configuring software guides and All the Best (y).

Leave a Reply