Amazon AWS: Setting up a Simple Web Server on Amazon Linux

Amazon Linux

Have you ever wanted to set up your own web server? I recently wanted to see the difference between setting up my own server and using a shared hosting solution. The problem was that I had no previous experience with Linux and had to learn everything from scratch. But I soon learned that Linux is not that complicated and installing web services is even more easy on Linux than on Windows Server.

Setting up your EC2 Instance on AWS

Start off by registering on Amazon AWS. For new users, you will get a free tier available for 12 months which means that you can use a Virtual Machine free for 12 months!

Now we want to create an EC2 Instance, so search for EC2 on the AWS services and open it up. Click Launch Instance and choose Amazon Linux AMI. Now you can select the desired hardware for your virtual machine. If you choose t2.micro, you will get it free for the first 12 months.
Choose Configure Instance Details, and on the next page, you can leave everything as default except for termination protection. This option is recommended to be turned on if you don’t want to accidentally delete your whole server. Click Next to add storage. Here, you can leave everything as default, the standard is 8GB General Purpose SSD. Extra storage can easily be added later. Skip tags and go directly to Configure Security Group.

On this page, you are basically choosing which ports you want to open. You will have to open these ports: HTTP port 80, HTTPS port 443 (if you want to use SSL), SSH port 22 and FTP port 20-21. HTTP and HTTPS must be opened to everyone, but you can set SSH and FTP to only be available to your own public IP; this is recommended to avoid brute force attacks. Now click Review and Launch, then click Launch again.

You are now prompted to create a new key pair and download it to a secure location on your own computer. This key pair is the only way to access your server, and if lost you will have a big problem, so I suggest you create at least one backup.

Your EC2 Instance should now be starting up for the first time which will take about 2-5 minutes. Go to Instances and you will see Instance State: Running when it is ready to use. If you want to have a static IP address, you will have to go to Elastic IP’s and Allocate a new address, then you have to associate the address with your new instance. If you skip this step, your virtual machine will get a new public IP on every reboot.

Now it’s time to connect to your server with a SSH client. In this guide, I will be using Putty on Windows 10.
Go here to download Putty and Puttygen. We will start off by using Puttygen to export our private key which is in .pem format to the compatible Putty format .ppk. Open up Puttygen and click Load and choose your private key, click Save private key and choose a name.
We are now ready to open Putty and add your EC2 server info. When Putty is open, you can start off by adding your IP address (find your IP under instances in the AWS console) and port (22), then go to Data and write ec2-user on Auto-login username, this is the standard username on Amazon Linux. Now you have to add your private key, open up SSH and select Auth, under Private key file just browse and choose your .ppk private key file. Everything should now be ready to connect. Just go to session and save your settings before you click Open. When you connect for the first time, you will get a security alert, just click Yes and you will get connected.

Installing Apache, MySQL and PHP

When you connect, you will see something like this:

Using username "ec2-user".
Authenticating with public key "imported-openssh-key"

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/
5 package(s) needed for security, out of 9 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-26-36 ~]$

First, I will explain some basic Linux commands so you can have some idea of what we are doing.
Remember, you can always use –help behind the command to get some more info.

sudo – Allow current user to execute a command as superuser. Use “sudo -i” to log in as superuser.
cd – Open folder FOR example cd /var/www/ you can ALSO use “cd ..” to go back.
ls – List files and folders.
mv – Move files or folders. Example “mv /var/www/index.php /etc/” will move the file to the folder “etc”.
cp – Copy files or folders. Example “cp /var/www/index.php /etc/” this will copy the file to the folder “etc”.
rm – Remove files or folders. Example “rm /var/www/index.php” this will remove index.php or if you are in the folder you can use “rm index.php”.
Use “rm -r” to remove folders.
mkdir – Create a directory. Example “mkdir /var/www/vhost”.
nano – Text editor. Example “nano /var/www/index.php” this will open index.php in nano text editor.

We can start by typing “sudo yum update”. This will update some packages mostly for security reasons. Use “y” to answer yes when prompted.
We will now install Apache, Mysql and PHP packages.

sudo yum install -y httpd24 php56 mysql55-server php56-mysqlnd

Start the Apache web server.

sudo service httpd start

Make sure Apache starts automatically at system boot.

sudo chkconfig httpd on

Now you can test if everything is working by typing your IP in a web browser.

 

Amazon Linux Test Page

The root that you now see is placed at this directory /var/www/html and this test page only appears when there is no content, so let’s create an index file to check if it works.

sudo nano /var/www/html/index.php

This will create an index.php file in your root folder for the Apache server. Copy and paste this content in the nano text editor. To paste, just press the right mouse button.

<html>
 <head>
  <title>Check out this amazing test page</title>
 </head>
 <body>
 <?php echo '<p>Hello World, check out this super cool site.</p>'; ?> 
 </body>
</html>

Press Ctrl+X then press y and enter to save the file.

Reload the page and you should see a white page with the text “Hello World. Check out this super cool site.”

Ownership and Permissions

If you want the ec2-user to be able to edit files in the www directory, you will have to do some configuring.
In this example, we want to create a www group that will have full permission on the www directory. Start by creating the www group.

sudo groupadd www

Now we will add the ec2-user to the www group, -g means that you add the user to the specified group.

sudo usermod -G www ec2-user

Now, exit and log back in again to verify your membership of the group. Just write “groups” and you should see something like this.

[ec2-user]$ groups
ec2-user www

Change the ownership of the www directory to the group www.

sudo chown -R root:www /var/www

Check the ownership of the www directory and you should see something like this.

[ec2-user]$ ls -l /var/www
total 20
drwxr-xr-x 2 root www 4096 Jul 29 21:42 cgi-bin
drwxr-xr-x 3 root www 4096 Jan 18 12:51 error
drwxr-xr-x 2 root www 4096 Jan 18 13:07 html
drwxr-xr-x 3 root www 4096 Jan 18 12:52 icons
drwxr-xr-x 2 root www 4096 Jan 18 12:51 noindex

Now you’ll need to set some permissions if you want the www to have write permissions on the www folder type this.

sudo chmod 2775 /var/www


Secure the MySQL server

We want to secure the MySQL server as it’s only set to a default development configuration. First, start the MySQL server and make sure it starts on boot.

sudo service mysqld start
sudo chkconfig mysqld on

Then start the mysql secure installation.

sudo mysql_secure_installation

Just press Enter on password and press y to set a root password, press y to remove anonymous users, y to disallow root login remotely, y to remove test database, and finally, y to reload privilege tables. Your mysql server should now be ready to use.

Conclusion

You have now configured a fully functional apache web server with PHP and MySQL. The difference between running your website on your own virtual machine or using a shared hosting solution is big. There is no limit to what you can do, and you will most likely experience better performance. I will also go into possibilities to boost your website performance in future posts because there’s really a lot of possibilities out there. On the next post, I will explain how you can setup multiple websites on one server. I will also show you how to configure a SSL certificates for each domain.

If you have any questions, don’t hesitate to ask in a comment or create a new topic in the Forum, see you!

 

 

Leave a Reply