Before we beign
I believe this article is of high value to any web developer seeking a better hosting solution, and in particular, cloud hosting. Though somewhat technical, the instructions given here are easy to follow, and you don't need a background in server management or server-side development to follow the tutorial. I'm a designer, and the first time I ever opened terminal was when I was trying to connect to my amazon linux server, so if you don't feel like you're up for setting up your own linux server, then trust me, it's easier than it sounds. Because of it's length and technical nature, I've also made the article available for download in MS Word format.
Amazon EC2 Cloud Hosting
Amazon provides an incredible, ever-expanding service for cloud hosting. Their EC2 service is bundled with a fantastic CDN (S3), Site Monitoring (Cloud Watch), and a myriad of complementary tools and services that make Amazon a great option for moving your site into the cloud. This is a guide to help you take advantage of those services.
Linux vs. Windows
The reason for choosing Linux vs. Windows is simple: cost. It’s much, much cheaper in the long run, well, in any run, to host an EC2 instance with Linux over Windows. Considering the relative operating costs, the choice is easy.
Don’t worry, Be Happy
If you’re like me, you’re mildly daunted at the idea of getting your hands dirty with Linux and Terminal. Don’t worry; it’s easier than it looks. I’m a designer, and until recently knew almost nothing about Linux or Terminal. I found that after you get the basics down, it’s surprisingly easy to get moving with the rest.
The first thing you’ll need on this journey is an account with Amazon Web Services (AWS). Simply head to http://aws.amazon.com, and click the Create an AWS Account link at the top. They’ll have you verify your identity by giving you a PIN over the phone to complete your registration.
Setting up your Instance
After you’ve signed in with your new AWS account, you’ll want to click into the Amazon EC2 tab, and then click the Launch Instance button.
Amazon EC2 offers a multitude of Amazon Machine Image’s (AMI’s) to choose from, including both Linux and Microsoft options, but as stated before, we’ll be working with Linux. Yay!
Select the Basic 32-bit Amazon Linux AMI 2010.11.1 Beta (AMI Id: ami-76f0061f).
Next, on the Instance details section, make sure to select Launch Instances, and then select your instance type. I chose micro, because it not only defines the amount of traffic my site gets, but it’s free for a year.
From here you can also define the number of instances you’d like, and the availability zone, I kept the defaults. Hit continue.
In this section, you can configure Advanced Instance Options, including enabling CloudWatch (very handy monitoring tool), but again I left everything on their defaults.
Now you can add some tags; you can read about using tags here: Using Tags. After you’ve selected your tags, hit continue.
Now we’ll create the Key Pair for this server. Select the name, and hit the Create & Download your Key Pair. Do not loose this file, it’s important, and you can’t download it again. Keep it secret, keep it safe. After you’ve downloaded your key pair, you’ll be brought to the next step.
On the next step, select Create a new Security Group, and then give it a name and description. We’ll want to add at least two rules here, SSH and HTTP, you may want more rules depending on how you’ll be using this server. Make sure you select SSH and HTTP.
That’s it! Just hit the Launch button (makes you feel like your head of mission control for NASA), and wait for your instance to launch. You can just hit close on the next dialog box.
After your server has moved from pending status to running status, move to the left hand menu, and select Elastic IPs, where obviously we’ll be giving our server an IP address.
Click the Allocate New Address button, select the new address, and hit the associate button to assign this IP address to the Instance you created.
Now that we have an instance up and running, we can connect through Secure Shell (SSH). This article was written for Mac users, but Windows users can accomplish the same thing using a program like PuTTY. So now we’ll need to grab that .PEM file you saved in a secure spot when you downloaded your Key Pair. Again, don’t loose this file. First we need to copy that .PEM file (mines called calebogden.pem) into the .SSH folder of your mac. Open up terminal and input this command to open that hidden folder.
Drag your .PEM file into the SSH folder.
Now we can test our ssh connection through terminal. Launch terminal, and input this command:
ssh -i ~/.ssh/YOUR_KEYPAIR_FILE.pem ec2-user@YOUR_IP_ADDRESS
When asked about the authenticity of the host, type yes and hit enter. You’ll likely also see a warning that says WARNING: UNPROTECTED PRIVATE KEY FILE!, in which case run this command to give your .PEM file the proper permissions.
sudo chmod 600 ~/.ssh/YOUR_KEYPAIR_FILE.pem
If you had to change the file permissions, then run your connection again. You should see this beautiful welcome text.
Connecting as the Root User
Now that we’re in, we want to work as the root user, but we’ll have to jump through some hoops to be able to connect as the root every time. If you try to connect with root at first, instead of ec2-user, you’ll hit a roadblock. To fix this, become a superuser (I know how awesome that sounds) by running this command.
Now we’ll open the SSH config and change some settings. As with all things in Linux, you want to be very careful to only change / edit what’s listed here. To open the config we’ll be using the Linux Vim editor by running this command.
Find the line that has the setting PermitRootLogin forced-commands-only, comment that out by putting a # sign before it, and then add PermitRootLogin without-password beneath it. This will allow us to connect as a root user.
Note: To start editing, just hit the ‘I’ key while in VIM, to exit the insert mode, it the ESC key.
After you’ve fixed the config file, hit ESC to exit insert mode, and then run the command :w to write, and the :q to quit, alternatively :wq.
Now we have to change the authorized_keys SSH file to not run commands in the first line. Open that config file with the command:
Once its open, delete the command command="echo 'Please login as the ec2-user user rather than root user.';echo;sleep 10, all the way until you reach, ssh-rsa. Now it ESC and :wq to leave vim.
We now need to restart the SSH service, this is done by using the command:
service sshd restart
Now open a new terminal window without closing the old one, and test the connection.
ssh -i ~/.ssh/YOUR_KEYPAIR_FILE.pem root@YOUR_IP_ADDRESS
You should see the beautiful welcome message again; you’ll now be connected as a root user. Awesome.
Exhausted yet? Well we’re just getting started. Now, head back to the hidden .SSH folder on your computer, create a new file called config (no extension), you’ll need to use a text editor when making change to this file. We’ll just create a shortcut that will allow us to connect easily with programs like Coda and Transmit, and an easy entrance with SSH through terminal.
In this config file, we’ll define a Host, User, IdentityFile, and HostName
Host HOST_NICKNAME_HERE User root IdentityFile "~/.ssh/YOUR_KEYPAIR_FILE.pem" HostName YOUR_IP_ADDRESS
Now head back to terminal, open a new window, and test the connection with this line:
You’ll see the familiar welcome text, and now we’re now ready to start pumping in software!
Installing Server Software
Programs can be easily installed with the yum command. So lets start by installing Apache with the command line:
yum install httpd
And then start apache with command:
service httpd start
Now we’ll install PHP and some needed libraries:
yum install php libmcrypt libmcrypt-devel php-mcrypt php-mbstring
After which we’ll want to restart Apache:
service httpd restart
yum install mysql
yum install –y mysql-server
Start MySQL Server
service mysqld start
Now we can assign a MySQL Server password
/usr/bin/mysqladmin -u root password ‘SUPERSECUREPASSWORD’
And that’s it! You have your basic webserver, php, and mysql installed. At this point you can see things coming together by browsing to: http://YOUR_IP_ADDRESS. You should see the Amazon Linux AMI Test Page.
Now since phpMyAdmin isn’t in Amazon’s yum, and since some older copies are buggy, we’ll install the latest version by hand.
First lets go to the Document Root (where apaches loads files for the web from), which should be /var/www/html, but you can verify by running the command grep DocumentRoot /etc/httpd/conf/httpd.conf.
Go to Document Root
Download phpMyAdmin 22.214.171.124 (or latest version, if you update a later version, remember to change your folder/file paths to the correct version)
Extract to Document Root
tar -xzvf phpMyAdmin-126.96.36.199-all-languages.tar.gz -C /var/www/html
Rename folder to phpmyadmin, and delete downloaded file.
mv phpMyAdmin-188.8.131.52-all-languages phpmyadmin
rm -rf phpMyAdmin-184.108.40.206-all-languages.tar.gz
We now need to add a user and give that user permission for this folder:
After you set a password, find the user group (should be apache) with this command:
egrep 'User|Group' /etc/httpd/conf/httpd.conf
Which will return:
And last, run this command to associate that user with the phpmyadmin folder:
chown -R phpmyadmin.apache phpmyadmin/
Now we need to go the that folder, and set some permissions:
chmod o+rw config
cp config.sample.inc.php config/config.inc.php
chmod o+w config/config.inc.php
And now we should restart Apache…
service httpd restart
Finally! You can head over and configure your phpMyAdmin!
Browse to: http://YOUR_SERVER_IP/phpmyadmin/setup/index.php, and hit the new server button.
The only values you need to input are the Verbose name of the server, and Password for config auth.
Now you can login by browsing to: http://YOUR_SERVER_IP/phpmyadmin/index.php. Username will be root and password is whatever you set as your super secure password for MySQL above.
NOTE: If you see an error that says “Cannot load mysql extension. Please check your PHP configuration”, then run the commands below, and then reload the page. You may have to wait a few seconds after you restart it. If the message still displays, try restarting a final time after waiting a few minutes.
yum -y install php-mysql
service httpd restart
After you log in, as the warning near the bottom of the page says, the last step is to remove the config folder we created above, and adjust some other options.
rm -rf config
Now use VIM to rename, and edit the main config file:
mv config.sample.inc.php config.inc.php
Add a BlowFish Secret as shown below:
$cfg['blowfish_secret'] = 'SuckBird'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
Hit ESC, and then :wq to save and quit.
Now, we head back into the world of GUI, and setup our Wordpress Database. Getting close!
Browse to: http://YOUR_IP_ADDRESS/phpmyadmin/index.php, after you log into phpMyAdmin again, create a new table called wordpress.
After this table is created, head to the privileges tab, and add a new user. Fill in the User name, and password / confirm. For the Database for user section, select the option Grant all privileges on database "wordpress”.
Whew! That was intense. Okay, just a few more minutes and you’ll have your Wordpress Platform running on Linux.
Now the easy (and fun) part of the process, getting wordpress installed. Head back to the root:
Execute these commands to download wordpress, extract it to your Document Root, and install wordpress in the main root of your site.
tar -xzvf latest.tar.gz -C /var/www/html
cp -avr /var/www/html/wordpress/* /var/www/html
rm -rf /var/www/html/wordpress
Lastly, we need to give Apache some permissions for the Document Root folder.
chown -R apache /var/www/html
chmod -R 755 /var/www/html
Now lets do a restart on apache, and start setting up Wordpress!
service httpd restart
Browse to your wordpress site: http://YOUR_IP_ADDRESS. You should see a Create
Configuration File button, click that, and then click the Lets Go button.
Input your credentials
Input your Wordpress Site information…
From this point, you can connect easily with programs like Coda and Transmit to start working on your code!
Amazon Cloud is, by far, the best option I’ve found for Cloud Hosting, and this article barley scratches the surface of the power found in the variety of AWS products.
As for hosting on a Linux server, there is a reason Microsoft is so dang expensive to operate; it’s a lot easier to get things started, however, the cost savings with operating on Linux vastly outweighs the rigors of using it.
Once you’ve gone through this process a single time, you can launch additional images based on the AMI you created, which in turn means you can get your clients hosted in the cloud on their own server, free for a year, with a few seconds of work. Now that’s incentive.
Additional Help and Resources Used
This article could not have been possible without the stellar work of the people who own the resources below. In addition I've turned on comments for this article to answer any questions.
The Geek Stuff http://www.thegeekstuff.com/2010/09/install-phpmyadmin/
Linux Blog Magazine http://www.jonboy60.com/2010/08/23/howto-install-mcrypt-and-recompile-php/