Ubuntu 8.04 Rails Server Using Passenger - Part 2

Ubuntu 8.04 Rails Server Using Passenger - Part 2
May 13th, 2008
Introduction
Here is the, the long awaited second part of my extensive guide on setting up a rails server running on Ubuntu 8.04. Everything should be going smoothly so far and you should be at the point where we need to setup Apache and link everything together. This guide will be quite verbose and much longer than the first one. This is due mostly to all the configuration that will be required. That being said I will contemplate making a third part of this guide that will cover the version control and capistrano recipes. Your thoughts are greatly apreciated, esecially if you want me to cover any other features or topics after you finish reading this guide.

As pointed out by a reader, some people may not have read part 1 yet. Click here to read Part 1 of this guide.

Enabling GPM
Note: You are going to want to have GPM enabled for this if you are just using Ubuntu Server. GPM will allow you to copy and paste output from the terminal using your mouse.


sudo apt-get install gpm
Mod_Rails
To Start the installation of passenger after it has been installed via the gem run the following command:


sudo passenger-install-apache2-module
This will commence the user-friendly installer created by the Phusion team. My hats off to Phusion for making something so incredible sexy.

Run the following command in a new terminal (Alt-F2):


sudo vim /etc/apache2/apache2.conf
Scroll down the page the bottom of the configuration file and right about the "# Include of directories ignores editors" add the following from the output of the passenger install screen. Copy YOUR output NOT mine.


LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/passenger-2.0.3
PassengerRuby /usr/bin/ruby1.8

NameVirtualHost *:80
Now that you are done with the passenger install we need to take our rails app and create a virtual host for this app. To do that run the following command.


sudo vim /etc/apache2/sites-available/test
Once the file is open for editing add the following but change the parts specific to your application.

Note: The ServerName MUST be resolvable and resolve to the host that the application is running on.



    ServerName test.rails
    DocumentRoot /var/rails/test/public

Now that the virtual host is created and we dont need the default page anymore we can enable the test app vhost and disable the default vhost.


sudo a2dissite default
sudo a2ensite test
Creating Test Application
First things first we want to make the directory that all the rails apps we will be using will be stored.


sudo mkdir -p /var/rails
cd /var/rails
Now we want to create the rails skeleton and hand the permissions over to the apache web server.


sudo rails test
sudo chown -R www-data:www-data test
Once this is complete we are now ready to start the customization of the application and setting up the databases.

Setting Up MySQL
We want to login to the mysql database using the root account and the password we set earlier.


sudo mysql -p
--Enter Password--
There are many parts of MySQL which I do not like, like their user management. It is a poor way to manage access to the database. That being said it still is one of the more popular databases and it has a smaller memory footprint than PostgreSQL.

Creating the Databases

mysql> CREATE DATABASE test_development;
mysql> CREATE DATABASE test_test;
mysql> CREATE DATABASE test;
Creating a User and Setting the Password for the test database
The code listed below will add a user called "testapp" with the password "testpass", I recommend that you make your passwords extremely complex. For example a 16 character password would work wonderfully because it is stored in the database.yml so you dont need to memorize it.


mysql> GRANT all privileges ON test_development.* to testapp@"localhost" IDENTIFIED by 'testpass';
mysql> GRANT all privileges ON test_test.* to testapp@"localhost" IDENTIFIED by 'testpass';
mysql> GRANT all privileges ON test.* to testapp@"localhost" IDENTIFIED by 'testpass';
This command below will flush the privileges so that the access levels we just added will take effect.


mysql> FLUSH PRIVILEGES;
Configuring Rails App To Connect To the Database
Now that the database is configured properly we need to tell the rails app we created about the database. This is all done in the database.yml file.


cd /var/rails/test/config
sudo vim database.yml
Now that we are editing the database.yml file, you want to make you file look as follows...


login: &login
  adapter: mysql
  username: testapp
  password: testpass
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *login
  database: test_development

test:
  <<: *login
  database: test_test

production:
  <<: *login
  database: test
Last Minute Tricks...
Passenger does not like the .htaccess file to be in the public directory of the rails app. To remove this file just run the following command.


sudo rm /var/rails/test/public/.htaccess
Testing the Test Application
Now we have setup passenger to point at our rails app and we have to just restart the webserver.


sudo /etc/init.d/apache2 restart
Edit the /etc/hosts file on your client computer if you do not have DNS setup and add the following line.


SERVER_IPADDRESS test.demo.rails
Now open a web browser and type in the URL specified in the hosts file.

You should see the default "Welcome to Rails" screen

Status Checklist
Should Be Completed
Installed All Packages/Gems
Installed Passenger
Setup Apache 2.2.8 to use Passenger
Created Test Rails App
Created Databases
Configure Test Rails App To Use MySQL Database
Test Rails App is Functioning
Left To Do
SSH Goodies
Reader Suggestions
SSH Goodies
This section pertains to ssh security and best practices. SSH is a very important service that is run on any server and must be configured properly in order to be secure. By the end of this section you should have a "more secure" server that can securely be accessed via your computer using RSA encrypted SSH keys instead of passwords.

Creating Keys on your Client
This is a very simple process, all you need to do is run one command.


ssh-keygen
Now that this is running you will want to accept the default location to save your keys and type a password when prompted. This will password protect your keys for added security. You output should look as follows.


Generating public/private rsa key pair.
Enter file in which to save the key (/home/rvalente/.ssh/id_rsa):
Created directory '/home/rvalente/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/rvalente/.ssh/id_rsa.
Your public key has been saved in /home/rvalente/.ssh/id_rsa.pub.
The key fingerprint is:
60:8e:b1:4c:cd:df:4d:82:b2:c4:05:97:0b:1b:56:fd rvalente@ubuntu
Now all you need to do is copy this key from your client machine to you server as follows. Make sure you use the account on your server instead of the account I list below.


scp .ssh/id_rsa.pub SERVER_IPADDRESS:.ssh/authorized_keys
Once that process completes then turn off password authentication on your ssh server. You do that but opening your /etc/ssh/sshd_config and changing this line.


#PasswordAuthentication yes
PasswordAuthentication no
That is it, now your server will use keys for authentication only.

Last but Not Least
I will be writing a part three of this guide for the capistrano/git version control and automation of a rails server. Since there are not many complete guides and they stop before the actual system administration occurs. Before I do this part three I want reader input for anything that you would like to see on top of the capistrano recipes and version control topics. Any input is greatly appreciated.

Thanks for reading!
上海基方太阳能

-Ron

猜你喜欢

转载自catrose.iteye.com/blog/289313