I like to provide hosting to friends and such, so having a control panel for them is very important. However, at the same time, I’m too cheap to buy a DirectAdmin or cPanel license. After a lot of researching and trying different panels, it’s my personal opinion that Virtualmin is the best free hosting control panel out there at the moment. Although it isn’t as flashy and aesthetically pleasing as commercial control panels, it provides many of the same features (except really good script installers) and I feel that it even gives more control to both the admin and the users. Plus, it doesn’t dictate the versions or setups of the software, so you can basically roll with anything you want.
I chose to use Debian over CentOS since Debian has more up to date packages and I’m more experienced with Debian. CentOS is really good for stability, but I’m going to be hosting my own content and friend’s content so it won’t be anything that’s really mission critical.
In this tutorial, I’ll be teaching you how to install a VPS or dedicated server with Virtualmin running Apache2 with MPM worker, PHP5 on FCGI, MySQL5, RoR with RVM, and Varnish. I believe this setup to be very good on performance and resources, but you’ll need a VPS with at least 1GB RAM. 512MB is really pushing it and you probably wouldn’t be able to run Varnish at the start, and a very limited number of websites.
It’s highly recommended that you start this guide with a fresh install of Debian Squeeze. Installing Virtualmin on an existing system with a web stack will probably cause a lot of problems.
Once you’ve installed, update the system:
apt-get update
I like to aptitude instead of apt-get, so this command is optional:
apt-get install aptitude
From now on I’ll be using aptitude, but you can substitute it for apt-get if you want. Next, install Virtualmin by downloading and running the script:
wget http://software.virtualmin.com/gpl/scripts/install.sh
sh install.sh
It’ll prompt you about installing Virtualmin and just press ‘y’ and enter, then it’ll install. The install itself will take a while so go get some coffee or watch a show while it installs. After it’s done, you can login to your site from the web panel located at https://your-hostname:10000, or put your IP if you didn’t configure your hostname yet.
You won’t need to login yet since we’re going to be installing and configuring some more stuff server-side, but go ahead and login if you want. After Virtualmin is nice and cozy on your server, go ahead and install some important tools that you’ll need not only for this tutorial, but later on for anything else you’ll want to build or compile:
aptitude install build-essential autoconf automake
Apache2 will come with prefork as the default, so we’ll be uninstalling the prefork module and installing the mpm module. This can easily be done with the following command:
aptitude install apache2-mpm-worker
This should prompt you about uninstalling prefork, and just agree to the solution. Since mod_php is only for prefork, you’ll need to use fcgi now to serve PHP. PHP CGI should already be installed but in case it’s not, one of these commands should do the trick:
aptitude install php5-cgi
or
aptitude install php-cgi
You need the actions module enabled in Apache and it should be enabled by default, but in case it’s not, here’s the command:
a2enmod actions
After that’s done, go into /etc/apache2/conf.d/ and add a new file called php-cgi.conf or whatever you want, then add the following into this new file:
ScriptAlias /local-bin /usr/bin
AddHandler application/x-httpd-php5 php
Action application/x-httpd-php5 /local-bin/php-cgi
Save and exit, then restart apache with
service apache2 restart
Next, you’ll install RVM system wide install by running this command as root, or using sudo. IT’S VERY IMPORTANT THAT YOU RUN THIS AS ROOT OR SUDO! If not, it will install the user version of RVM instead of the system wide, but if that’s what you want, then go ahead:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Now go into /etc/profile (file) and add the following:
[[ -s "/usr/local/bin/rvm" ]] && . "/usr/local/bin/rvm" # Load RVM into a shell session *as a function*
NOTE!: You might need to add the line above to each individual user’s .bash_profile or whatever equivalent your shell has.
RVM should be installed now. Install Ruby Enterprise Edition with RVM by running these commands:
aptitude install
rvm install ree
We want REE to be the default interpretor, so the following command will do that:
rvm –default use ree
Almost done! All we have to do is install varnish. Run these commands to install Varnish:
sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
sudo echo “deb http://repo.varnish-cache.org/debian/ $(lsb_release -s -c) varnish-2.1″ >> /etc/apt/sources.list
aptitude update
aptitude install varnish
Now open the /etc/default/varnish file and find the uncommented DAEMON_OPS line. You’ll see 6081; change it to 80. A few lines below, look for “default.vcl”. Change it to something else, I used virt.vcl. Save and exit. Now create a new file in /etc/varnish/ named after what you changed default.vcl to, so for me I made a file /etc/varnish/virt.vcl. Paste the following contents:
## Redirect requests to Apache, running on port 8000 on localhost
backend apache {
.host = "127.0.0.1";
.port = "8000";
}
## Fetch
sub vcl_fetch {
## Remove the X-Forwarded-For header if it exists.
remove req.http.X-Forwarded-For;
## insert the client IP address as X-Forwarded-For. This is the normal IP address of the user.
set req.http.X-Forwarded-For = req.http.rlnclientipaddr;
## Added security, the "w00tw00t" attacks are pretty annoying so lets block it before it reaches our webserver
if (req.url ~ "^/w00tw00t") {
error 403 "Not permitted";
}
## Deliver the content
return(deliver);
}
## Deliver
sub vcl_deliver {
## We'll be hiding some headers added by Varnish. We want to make sure people are not seeing we're using Varnish.
## Since we're not caching (yet), why bother telling people we use it?
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
## We'd like to hide the X-Powered-By headers. Nobody has to know we can run PHP and have version xyz of it.
remove resp.http.X-Powered-By;
}
You can change the port to whatever you want, I used 8080. However, you must remember this port since we’re going to have to change some Apache configs to listen on that new port since Varnish will be listening on port 80. Save and exit. Open up /etc/apache2/ports.conf and find where 80 is, and change it to the port in the previous file. For me it was 8080, so I changed all 80 to 8080. Save and exit. Next, run this command:
aptitude install libapache2-mod-rpaf
Then restart Apache with
service apache2 restart
Now that everything is installed, we still need configure a few things, such as RVM. The system wide install of RVM requires users to be in a group called “rvm” in order to use it, so go ahead and log into the Virtualmin control panel which is usually located at https://hostname:10000, where hostname is your hostname. After you login, look at the side menu and find “Server Settings”, and click on it. This will open a new menu, and find the second item called “Server Templates” and click on it. The server templates are very useful for customizing your Virtualmin experience, so remember it! Now, you should see 2 templates already made for you: “Default Settings”, and “Settings For Sub-Servers”. Click on “Default Settings”. You should now see a page with a drop down menu near the top. Click the drop down menu and find the second item called “Administration User”. This should take to you a new page. If it doesn’t, click the “Change” button with “Administration User” selected in the drop down menu. Now, look for the item “Add domain owners to secondary group” and select “Selected group”. A new window should open up displaying all the groups. Find “rvm” and select it, then click the “Save” button.
Now you’re done! Everything should be working correctly assuming you didn’t mess up somewhere. Enjoy your new server! If I missed anything anywhere, please don’t hesitate to comment!
References:
http://www.howtoforge.com/putting-varnish-in-front-of-apache-on-ubuntu-debian
http://library.linode.com/web-servers/apache/php-cgi/debian-5-lenny