Closed sionc closed 12 years ago
Ubuntu + Apache + MySQL + RVM + Ruby + Rails + Passenger = Happy Rails Production Server
Blog about setting up a ruby on rails production server using mysql http://techinspiration.wordpress.com/2011/11/26/how-to-set-up-ruby-on-rails-server-with-mysql-and-passanger-on-a-linux/
Railscast about Passenger http://railscasts.com/episodes/122-passenger-in-development
Website for Phusion Passenger http://www.modrails.com/
Post about Apache on Linux http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch20_:_The_Apache_Web_Server
Rails with MySQL http://stackoverflow.com/questions/5366934/help-setting-up-ruby-on-rails-and-mysql-reward-offered
Problems installing MySQL gem http://www.petermac.com/installing-mysql-gem-on-osx-10-6/
Blog about installing mysql: http://www.frederico-araujo.com/2011/07/30/installing-rails-on-os-x-lion-with-homebrew-rvm-and-mysql/
Apache with Passenger tutorial http://www.modrails.com/documentation/Users%20guide%20Apache.html#_tutorial_example_writing_and_deploying_a_hello_world_rack_application
Allowing remote connections http://www.sitepoint.com/ubuntu-12-04-lts-precise-pangolin-introducing-mysql-server/
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /var/lib/gems/1.9.1/gems/passenger-3.0.17/ext/apache2/mod_passenger.so PassengerRoot /var/lib/gems/1.9.1/gems/passenger-3.0.17 PassengerRuby /usr/bin/ruby1.9.1
After you restart Apache, you are ready to deploy any number of Ruby on Rails applications on Apache, without any further Ruby on Rails-specific configuration!
Press ENTER to continue.
Deploying a Ruby on Rails application: an example
Suppose you have a Rails application in /somewhere. Add a virtual host to your Apache configuration file and set its DocumentRoot to /somewhere/public:
<VirtualHost *:80> ServerName www.yourhost.com
DocumentRoot /somewhere/public
<Directory /somewhere/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
And that's it! You may also want to check the Users Guide for security and optimization tips, troubleshooting and other useful information:
/var/lib/gems/1.9.1/gems/passenger-3.0.17/doc/Users guide Apache.html
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) https://www.phusionpassenger.com
Stop the server
$sudo apachectl -k stop
Gracefully restart the server
$sudo apachectl -k graceful
Restart the server now
$sudo apachectl -k restart
Need to use sudo for command line and gksudo for graphical applications. For example:
To restart the apache server, use...
$sudo /etc/init.d/apache2 restart
To edit the configuration file, use...
$gksudo gedit /etc/apache2/apache2.conf
To switch between production and development environments, type
$gksudo gedit /etc/apache2/httpd.conf
It should open up the httpd.conf file which contains the phusion configuration parameters.
Then, change the Rails environment from development to production.
So, instead of using RailsEnv production
use RailsEnv development
Then, restart the server
$ sudo apachectl -k restart
After installing MySql for Mac OSX only,
Use aliases to simplify this process:
$alias mysql=/usr/local/opt/mysql/bin/mysql
$alias mysqladmin=/usr/local/opt/mysql/bin/mysqladmin
Note: MySQL user root password = robots
Run this to secure mysql
$sudo /usr/bin/mysql_secure_installation
To start mysql command line tool:
$mysql --user=root --password=robots
OR
$mysql -u=root -pcurrentpassword
Commands to start, stop and restart the mysql server:
$mysql.server start
$mysql.server stop
$mysql.server restart
To show all databases...
> SHOW DATABASES;
To create a database...
> CREATE DATABASE databaseName;
Adding user priveleges...
mysql> CREATE USER 'ace_app_user'@'localhost' IDENTIFIED BY 'robots';
Query OK, 0 rows affected (0.00 sec)
mysql> grant usage on *.* to ace_app_user@localhost identified by 'robots';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on ace_app_development.* to ace_app_user@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on ace_app_test.* to ace_app_user@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on ace_app_production.* to ace_app_user@localhost;
Query OK, 0 rows affected (0.00 sec)
Changing root password (no space between p and currentpassword):
mysqladmin -u root -pcurrentpassword password 'newpassword'
To install MySQL on MAC OSX when ruby-1.9.2-p194 is already installed:
Install brew, then update it
brew update
Install mysql using brew
brew install mysql
Install mysql2 gem (In gemfile - 'mysql2', '~> 0.3')
gem install mysql2
username = adeptdev password = robots0
Apache commands:
Stop the server
$apachectl -k stop
Gracefully restart the server
$apachectl -k graceful
Restart the server now
$apachectl -k restart
To protect files/folders on linux
$sudo chmod 600 ~/.ssh/id_rsa
$sudo chmod 600 ~/.ssh/id_rsa.pub
To unprotect files/folders on linux:
$sudo chmod 777 ~/.ssh/id_rsa
$sudo chmod 777 ~/.ssh/id_rsa.pub
Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk.
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.
You can call this task on the server during deployment to create compiled versions of your assets directly on the server. If you do not have write access to your production file system, you can call this task locally and then deploy the compiled assets.
The rake task is:
bundle exec rake assets:precompile
For faster asset precompiles, you can partially load your application by setting config.assets.initialize_on_precompile to false in config/application.rb, though in that case templates cannot see application objects or methods. Heroku requires this to be false.
If you set config.assets.initialize_on_precompile to false, be sure to test rake assets:precompile locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag.
Important 'gotcha' while setting up mysql is the fact that the server only listens to localhost. This means that remote requests are not allowed. The way around this problem is to edit the /etc/mysql/my.conf file on Ubuntu.
Open /etc/mysql/my.conf for editing
$gksudo gedit /etc/mysql/my.cnf
By default, the fault looks like this
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
Comment out the bind-address property which is currently set to 127.0.0.1 (localhost)
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address = 127.0.0.1
If you wish to bind to only one remote host, instead of commenting out bind-address (which will allow all remote connections), you can choose to point the bind-address property to the remote IP
bind-address = 172.21.3.165
Restart the mysql server
sudo restart mysql
Login to mysql as root and grant privileges to remote user
mysql> GRANT ALL ON ace_app_test.* TO ace_app_user@172.21.3.165 IDENTIFIED BY 'robots';
mysql> GRANT ALL ON ace_app_development.* TO ace_app_user@172.21.3.165 IDENTIFIED BY 'robots';
mysql> GRANT ALL ON ace_app_production.* TO ace_app_user@172.21.3.165 IDENTIFIED BY 'robots';
mysql> FLUSH PRIVILEGES;
For more information, look for 'bind-address' in this blog
http://www.sitepoint.com/ubuntu-12-04-lts-precise-pangolin-introducing-mysql-server/ Always precompile assets on deployment machine
$bundle exec rake assets:precompile
Need to setup a webserver that will host the MySQL database for PackExpo