Installing Nginx, Mongrel Clusters, MySQL, Rails, Ruby on Ubuntu 6.06
September 23rd, 2007
This is my experience setting up a slice from Slicehost to run Unbuntu 6.06, Nginx, Mongrel Clusters, Rails, and MySQL. This was my very first time setting up a server with this stack; no guarantee this is the absolute 'correct' way, but it's the way I got it working.
Lock down the server a bit.
Root and Sudo Access
Okay, first thing's first! SSH into your new fresh Unbuntu 6.06 install with root and your root password using your IP.
First thing we're going to do is a bit of cleanup on the server. Once logged in as root, change the password from the default that was given.
passwd
Set up a new user with sudo access and then use that instead of root. Replace [new user] with your new user name.
adduser [newuser]
Open the /etc/sudoers file with the following command:
visudo
Add the new user to the sudoer access by adding the following under 'user privilege specification':
[newuser] ALL=(ALL) ALL
SSH Access
We want to lock down SSH a bit before we continue and remove root access to SSH as well as change the port you are SSHing too. The default port is 22, but you can pick whatever floats your boat.
nano /etc/ssh/sshd_config
This brings up the SSH configuration. Change 'PermitRootLogin' to 'no' and change 'Port 22' to 'Port 30000' or whatever port of your choosing. After this is done, save, reload SSH, and switch users to the new user you created.
/etc/init.d/ssh reload su [newuser]
If this does not work, you can always restart the system and reSSH back in. To do that use the following:
shutdown -r now
More information about locking down your slice can be found at http://articles.slicehost.com/2007/9/4/ubuntu-lts-setup-page-1
Installing ruby, mysql, rails, etc.
Apt-Get and Your Sources.list
Uncomment your sources.list so you can grab the appropriate packages needed.
sudo nano /etc/apt/sources.list
Uncomment the following and save:
deb http://us.archive.ubuntu.com/ubuntu dapper universe deb-src http://us.archive.ubuntu.com/ubuntu dapper universe
Update your sources, etc:
sudo apt-get update sudo apt-get dist-upgrade
Start Installing!
Install the build essentials needed to run many of the programs you are about to install:
sudo apt-get install build-essential
Grab mysql, ruby, and many of the helper programs that make it all work.
sudo apt-get install ruby ri rdoc mysql-server ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 mysql-client-5.0 mysql-common mysql-server-5.0 rdoc1.8 ri1.8 ruby1.8 irb ri rdoc libpcre3-dev libopenssl-ruby openssl zlib1g zlib1g-dev libssl-dev
If any of those give you flack, remove it from the install and try again. Once installed, add some linking to the appropriate directories so everything knows where ruby, and others are located.
sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb
Give MySQL a root password, since the default is blank.
sudo mysqladmin -u root password [yourrootsqlpassword]
performance enhancements for mysql
You can add a few enhancements to your MySQL to make it use less memory. I grabbed these from the slicehost help tutorial. I'm not going to go into details about what they do, but if you want to learn more you can read their tutorial.
Open the mysql config file.
sudo nano /etc/mysql/my.cnf
Add the following:
language = /usr/share/mysql/english skip-external-locking skip-locking
Also add and make these changes under 'Fine Tuning'.
# * Fine Tuning # key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K thread_cache_size = 4 sort_buffer=64K net_buffer_length=2K
Save and restart mysql.
sudo /etc/init.d/mysql restart
Installing Ruby Gems
You will need to grab the latest version of ruby gems from their website. At the time of this writing it is 0.9.4 so change that accordingly. Start by creating a folder to extra the ruby gems program to:
cd ~ mkdir sources
Grab ruby gems files from their website and decompress them.
wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz tar xvzf rubygems-0.9.4.tgz
Install ruby gems.
cd rubygems-0.9.4 sudo ruby setup.rb
Installing Rails & Mongrel Cluster
Once you have ruby gems installed, you can now use 'gem install [program]', so we'll use this to grab rails, mongrel, and others.
sudo gem install rails --include-dependencies
Grab mongrel and mongrel cluster using ruby gems. Choose yes for all dependencies.
sudo gem install mongrel mongrel_cluster
Now we need to make sure if the server dies and restarts, that the mongrel clusters will restart automatically. Copy the init file over to /etc/init.d/ by typing this all on one line:
sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/mongrel_cluster
Next, add a path statement to mongrel_cluster file just above the CONF_DIR variable:
nano /etc/init.d/mongrel_cluster
Add:
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
Change the USER from:
USER=mongrel
to
USER=www-data
Ubuntu has a built in user called www-data that we will use. Finally, let's modify permissions and make sure we boot mongrel on startup:
sudo chmod +x /etc/init.d/mongrel_cluster sudo update-rc.d mongrel_cluster defaults
Installing Nginx
We will need to install nginx from source if you are on ubuntu 6.06 and even if you are running 7.x you should do the same to make sure you are running the latest version.
cd ~ cd sources
Grab the latest nginx tar; currently 0.5.31 but check their website for the latest.
wget http://sysoev.ru/nginx/nginx-0.5.31.tar.gz
Decompress it.
tar -zxvf nginx-0.5.31.tar.gz cd nginx-0.5.31
Build it. Make it. Install it.
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module make sudo make install
Now you’ve got it built and sitting in /usr/local/sbin/ and ready to go. Next we need to set it up to run upon boot up like our mongrel clusters in case of an unfortunate reboot. Fortunately notrocketsurgery.com has built a init.d file for us to use. Run each of the following lines:
cd ~ cd sources sudo wget http://notrocketsurgery.com/files/nginx -O /etc/init.d/nginx sudo chmod 755 /etc/init.d/nginx sudo update-rc.d nginx defaults
Installing Subversion(SVN)
Now we are ready to pull in a project we've been developing on. The easiest way is to just check out the project from SVN and keep it updated that way. Grab subversion; yes to all dependencies.
sudo apt-get install subversion
Create a location to put your projects. For this example we'll store them in /var/www. You may need to create it.
cd var sudo mkdir www cd www
Check out your project from SVN.
sudo svn co [path to svn repo] [project-name]
Assign the folder and files the appropriate owner to be shown on the web. From inside the /var/www folder type:
sudo chown -R www-data:www-data [your checked out rails app]
Configure your mongrel clusters
We now need to set up the mongrel cluster configuration. This will produce a file called mongrel_cluster.yml. Each site you have NEEDS to have a file called this located in its /config folder. For the port you can use whatever you want, and it will increment the port by 1 for each mongrel you set up. In this example, we are setting up 3 clusters, by specifying 3 on the -N option. This will use ports 8000,8001,8002. Feel free to use more if you like.
cd /var/www/[your rails app] sudo mongrel_rails cluster::configure -e production \ -p 8000 -N 3 -c /var/www/[yourrailsapp] -a 127.0.0.1 \ --user www-data --group www-data
Now let's create a symlink to that file from within /etc where all our configs live. When our system starts up it looks for mongrel cluster configs in the /etc/mongrel_cluster folder. We need to put all our mongrel_cluster.yml's into this folder by creating symlinks. For more then 1 site, make sure you call your file mongrel_cluster.yml in the site's config folder, but when you symlink it you can name it something else.
sudo mkdir /etc/mongrel_cluster cd /etc/mongrel_cluster/ sudo ln -s /var/www/[your rails app]/config/mongrel_cluster.yml
Add user to mongrel_cluster.yml
nano /etc/mongrel_cluster/mongrel_cluster.yml
Right next to group add 'user: www-data
Configure your Nginx
We need to make some changes to our nginx.conf to make sure all is set up correctly.
sudo nano /usr/local/nginx/conf/nginx.conf
A demo file can be found here: http://webchicanery.com/code/nginx.conf.txt thanks to WebChicanery. They have a great write up about installing and configuring Nginx that help me greatly. You can find it here. http://webchicanery.com/2007/08/07/slicehost-nginx-mongrel-part-2/
In the nginx.conf make sure all your log paths, processes, ports are the same as what is in your mongrel_cluster.yml.
Set up your rails app
Create your db in mysql.
mysqladmin -u [username] -p create [database_name]
Migrate your application using the production environment. Navigate to your application folder.
sudo rake db:migrate RAILS_ENV=production
Stopping/Starting Nginx and Mongrel Clusters
After making changes to the nginx.conf you will need to stop and restart it. Apparently 'restart' does not work, so you will need to kill the process.
sudo kill [nginx pid]
Then start ngnix again.
sudo /etc/init.d/nginx start
To restart mongrel_clusters run the following:
sudo /etc/init.d/mongrel_cluster restart
Mongrel cluster can also take stop|start instead of restart. One last way to restart this is to restart your entire system. If everything is set up correctly, both nginx and mongrel clusters should automatically start when the system starts since we added them to the /etc/init.d folder.
Closing
I hope this helps! If you see anything that is completely incorrect let me know and I'll update the post. Like I said, this is based on my initial install.
Here are some of the websites that helped me not only during the install but also in writing this post:
- Slicehost Articles and tutorials - http://articles.slicehost.com/
- WebChicanery - Part 1- http://webchicanery.com/2007/08/06/slicehost-nginx-mongrel-part-1/
- WebChinanery - Part 2 - http://webchicanery.com/2007/08/07/slicehost-nginx-mongrel-part-2/
- http://brainspl.at/rails_stack.html
- http://symbiotix.net/blog/entry/15
- http://www.urbanpuddle.com/articles/2007/05/09/install-ruby-on-rails-on-ubuntu-feisty-fawn
- http://www.urbanpuddle.com/articles/2006/06/10/install-ruby-rails-on-ubuntu-dapper-drake
Sorry, comments are closed for this article.