Archive | May, 2011

Rackspace Cloud Servers

3 May

I have been playing with Rackspace “cloud” servers for a few weeks now. They’re easy to fire up and shut down, and you only pay for the time the servers are online, so they are great for spinning up a development server for a few hours.

I’ve put together the following as my own selfish reference to setting up a working CentOS LAMP server.

This post assumes you have knowledge of using SSH and have some idea of what you are trying to achieve which in this case is a working LAMP web server.

But it also involves managing your own stuff. Updates, checking logs, making sure your websites are up and running, fixing issues etc. will now be your responsibility.

If the sound of all that makes you dizzy, then you might want to consider a fully managed web hosting service from a reputable hosting company instead who will do all that boring stuff for you.

Still with me?

Good. Lets fire up a server. Remember to give your server a cool name, this will help gain you the respect of more seasoned server admins. If you own a retro Transformers T-shirt, put it on now.

Create a server

Create a server

When you create a server on rackspace, you will be issued with the important basics. The unique IP address, and your root username and password. So fire up a terminal and get your eyes dirty. For the purpose of this post, I will be using the server “clapton” (cool, huh?) and the IP address is 46.38.165.175, I’m logged in with a clean install of CentOS  5.5.

[root@clapton ~]#

Yum is your friend

You know that cool retro Tansformers T-shirt you’re wearing? Well Yum is even cooler.

First up, lets update any existing packages that have been included in your CentOS install by using the “update” option

yum update

The installed packages are now up to date, now lets move on to the stuff we want. I want to install the following;

  • Apache
  • MySQL
  • PHP
  • Open SSL
  • Sendmail
  • Git
  • VIM

I’m not going to bother with FTP (real men use Git) and I want to keep things lightweight and locked down as much as possible. Having FTP running just means faffing with IP lists in an attempt to stop 7 million Chinese teenagers pwning your FTP login.

Open SSL comes included with CentOS, we just need the apache mod_ssl. I will write another post regarding setting up SSL certs, as this was my one stumbling block when I first started. I’d previously been spoiled by admin panels like Plesk when it came to things like that.

Core settings

There isn’t really much house work to take care of, other than checking the servers HOSTS file, located at /etc/hosts;

127.0.0.1    localhost    localhost.localdomain
46.38.165.175    clapton    46.38.165.175.static.cloud-ips.co.uk

In the Rackspace control panel, if you view the DNS for the server you will see a xxxx.cloud.co.uk record, add this (along with the server name if not already there) to the IP in your HOSTS file.

I also add a localhost.localdomain entry, this will make life easy for things like sendmail.

VIM

The rest of the setup may require editing files, be nice to your eyes and use VIM.

yum install vim-enhanced

Apache

Lets get Apache up and running. We want SSL support too so we’ll also install the apache mod now (OpenSSL should be installed by default as mentioned above, we just need the mod itself)

yum install httpd mod_ssl

Fire up VIM and edit the apache conf file, we need to tell apache to “listen” on port 80, its around line 135 of httpd.conf;

vim /etc/httpd/conf/httpd.conf

httpd conf listen port 80

httpd conf listen port 80

Now Apache is listening to port 80, we now need to open this port as CentOS by default will have every port locked down for obvious reasons.

To open port 80 (http) and 443 (https) we add a rule for each to the iptables config;

iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT

and then commit these new rules;

/etc/init.d/iptables save

That’s it, fire up apache;

service httpd start

and go to your IP address in a browser;

apache default page

apache default page

 

 

PHP

Use Yum to install PHP, the MySQL extension, and MySQL itself in one fell swoop;

yum install php php-mysql mysql-server

As of writing this, the latest PHP version that Yum will use is 5.1.x, which lets be fair, is pretty outdated.

5.2.3+ is fine for me, so I’ll go with that. If you want to go big and have 5.3+ (but have never installed manually before) then I would suggest get going with 5.2.x to start with, then upgrade to 5.3 later on once you have a basic web server up and running.

To update to 5.2+ we need to add a .repo file which instructs Yum to do some kung foo for us when finding which packages we want.

We then just update PHP using Yum.

So first, add the .repo file as follows;

touch /etc/yum.repos.d/CentOS-Testing.repo

Then open the empty file;

vim /etc/yum.repos.d/CentOS-Testing.repo

Then add the following config to the repo file you just created;

# CentOS-Testing:
# !!!! CAUTION !!!!
# This repository is a proving grounds for packages on their way to CentOSPlus and CentOS Extras.
# They may or may not replace core CentOS packages, and are not guaranteed to function properly.
# These packages build and install, but are waiting for feedback from testers as to
# functionality and stability. Packages in this repository will come and go during the
# development period, so it should not be left enabled or used on production systems without due
# consideration.
[c5-testing]
name=CentOS-5 Testing
baseurl=http://dev.centos.org/centos/$releasever/testing/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing
includepkgs=php*

Now update PHP as normal with Yum;

yum update php

Restart apache

service httpd restart

MySQL users !IMPORTANT

MySQL is now installed, but comes with a default user….with NO PASSWORD. So we need to start MySQL, drop the users with no passwords, and create a user with a password;

service mysqld start

Log in as the default password-less root user;

mysql -u root

Set a password for your root user;

SET PASSWORD FOR root@localhost = PASSWORD('newpassword');

and

SET PASSWORD FOR root@127.0.0.1 = PASSWORD('newpassword');

Drop the ‘any’ user too

DROP USER ''@localhost;

All done. Exit MySQL;

exit;

Email (Sendmail)

yum install sendmail sendmail-cf
service sendmail start

Done :)

Services

At this stage we have 3 main services installed; Apache, MySQL and Sendmail.

But these are currently manually started, which means if you had to reboot your server, you would also have to also manually start each service again, which is pants.

To set all 3 to start by default use the chkconfig command;

chkconfig httpd on
chkconfig mysqld on
chkconfig sendmail on

You can also use the –list parameter to see the changes and view any other default services and their run levels.

chkconfig --list

chkconfig --list

Git

Git is cooler than both Yum and your Transformers T-shirt combined.

To install Git we can employ the EPEL (Extra Packages for Linux) RPM;

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/
epel-release-5-4.noarch.rpm

We can now Yum install;

yum install git
git --version

Should display;

git version 1.5.5.x

Great Success

That’s it. You now have a LAMP stack on Rackspace. A great tool in the Rackspace Panel is “My Server Images”. Now would be a good time to create an image of what you have done so far, then you can roll this out on other servers really quickly in future.

But don’t be a dingbat and loose all your shit, image or no image. Remote file storage is seriously cheap, see this post on Remote Backups to Amazon S3 and save yourself a huge headache one day ;)