├── README.markdown └── nginx └── nginx /README.markdown: -------------------------------------------------------------------------------- 1 | rails-nginx-passenger-ubuntu 2 | ============================ 3 | 4 | My notes on setting up a simple production server with ubuntu, nginx, passenger and mysql for rails. 5 | 6 | Aliases 7 | ------- 8 | 9 | echo "alias ll='ls -l'" >> ~/.bash_aliases 10 | 11 | edit .bashrc and uncomment the loading of .bash_aliases 12 | 13 | If you have trouble with PATH that changes when doing sudo, see http://stackoverflow.com/questions/257616/sudo-changes-path-why then add the following line to the same file 14 | 15 | echo "alias sudo='sudo env PATH=$PATH'" >> ~/.bash_aliases 16 | 17 | 18 | Update and upgrade the system 19 | ------------------------------- 20 | 21 | sudo apt-get update 22 | sudo apt-get upgrade 23 | 24 | Configure timezone 25 | ------------------- 26 | 27 | sudo dpkg-reconfigure tzdata 28 | sudo apt-get install ntp 29 | sudo ntpdate ntp.ubuntu.com # Update time 30 | 31 | Verify that you have to correct date and time with 32 | 33 | date 34 | 35 | Configure hostname 36 | ------------------- 37 | 38 | sudo hostname your-hostname 39 | 40 | Add 127.0.0.1 your-hostname 41 | 42 | sudo vim /etc/hosts 43 | 44 | Write your-hostname in 45 | 46 | sudo vim /etc/hostname 47 | 48 | Verify that hostname is set 49 | 50 | hostname 51 | 52 | Install mysql 53 | --------------- 54 | 55 | This should be installed before Ruby Enterprise Edition becouse that will install the mysql gem. 56 | 57 | sudo apt-get install mysql-server libmysqlclient15-dev 58 | 59 | 60 | Gemrc 61 | ------- 62 | 63 | Add the following lines to ~/.gemrc, this will speed up gem installation and prevent rdoc and ri from being generated, this is not nessesary in the production environment. 64 | 65 | --- 66 | :sources: 67 | - http://gems.rubyforge.org 68 | - http://gems.github.com 69 | gem: --no-ri --no-rdoc 70 | 71 | 72 | Ruby Enterprise Edition 73 | ------------------------ 74 | 75 | Check for newer version at http://www.rubyenterpriseedition.com/download.html 76 | 77 | Install package required by ruby enterprise, C compiler, Zlib development headers, OpenSSL development headers, GNU Readline development headers 78 | 79 | sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5-dev 80 | 81 | Download and install Ruby Enterprise Edition 82 | 83 | wget http://rubyforge.org/frs/download.php/66162/ruby-enterprise-X.X.X-ZZZZ.ZZ.tar.gz 84 | tar xvfz ruby-enterprise-X.X.X-ZZZZ.ZZ.tar.gz 85 | rm ruby-enterprise-X.X.X-ZZZZ.ZZ.tar.gz 86 | cd ruby-enterprise-X.X.X-ZZZZ.ZZ/ 87 | sudo ./installer 88 | 89 | 90 | Change target folder to /opt/ruby for easier upgrade later on 91 | 92 | Add Ruby Enterprise bin to PATH 93 | 94 | echo "export PATH=/opt/ruby/bin:$PATH" >> ~/.profile && . ~/.profile 95 | 96 | Verify the ruby installation 97 | 98 | ruby -v 99 | ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 20090928 100 | 101 | 102 | Installing git 103 | ---------------- 104 | 105 | sudo apt-get install git-core 106 | 107 | Nginx 108 | ------- 109 | 110 | sudo /opt/ruby/bin/passenger-install-nginx-module 111 | 112 | Select option 1. Yes: download, compile and install Nginx for me. (recommended) 113 | 114 | When finished, verify nginx source code is located under /tmp 115 | 116 | $ ll /tmp/ 117 | drwxr-xr-x 8 deploy deploy 4096 2009-04-18 17:48 nginx-0.6.36 118 | -rw-r--r-- 1 root root 528425 2009-04-02 08:49 nginx-0.6.36.tar.gz 119 | drwxrwxrwx 7 1169 1169 4096 2009-04-18 17:56 pcre-7.8 120 | -rw-r--r-- 1 root root 1168513 2009-04-18 17:51 pcre-7.8.tar.gz 121 | 122 | Run the passenger-install-nginx-module once more if you want to add --with-http_ssl_module 123 | 124 | $ sudo /opt/ruby/bin/passenger-install-nginx-module 125 | 126 | Select option 2. No: I want to customize my Nginx installation. (for advanced users) 127 | 128 | When installation script ask, "Where is your Nginx source code located?" Enter: 129 | 130 | /tmp/nginx-0.6.36 131 | 132 | On, extra arguments to pass to configure script add 133 | 134 | --with-http_ssl_module 135 | 136 | 137 | Nginx init script 138 | ------------------- 139 | 140 | More information on http://wiki.nginx.org/Nginx-init-ubuntu 141 | 142 | cd 143 | git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git 144 | sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx 145 | sudo chown root:root /etc/init.d/nginx 146 | 147 | Verify that you can start and stop nginx with init script 148 | 149 | sudo /etc/init.d/nginx start 150 | 151 | * Starting Nginx Server... 152 | ...done. 153 | 154 | sudo /etc/init.d/nginx status 155 | 156 | nginx found running with processes: 11511 11510 157 | 158 | sudo /etc/init.d/nginx stop 159 | 160 | * Stopping Nginx Server... 161 | ...done. 162 | 163 | sudo /usr/sbin/update-rc.d -f nginx defaults 164 | 165 | If you want, reboot and see so the webserver is starting as it should. 166 | 167 | Installning ImageMagick and RMagick 168 | ----------------------------------- 169 | 170 | If you want to install the latest version of ImageMagick. I used MiniMagick that shell-out to the mogrify command, worked really well for me. 171 | 172 | # If you already installed imagemagick from apt-get 173 | sudo apt-get remove imagemagick 174 | 175 | sudo apt-get install libperl-dev gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libz-dev libpng12-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config 176 | 177 | Use wget to grab the source from ImageMagick.org. 178 | 179 | Once the source is downloaded, uncompress it: 180 | 181 | 182 | tar xvfz ImageMagick.tar.gz 183 | 184 | 185 | Now configure and make: 186 | 187 | cd ImageMagick-6.5.0-0 188 | ./configure 189 | make 190 | sudo make install 191 | 192 | To avoid an error such as: 193 | 194 | convert: error while loading shared libraries: libMagickCore.so.2: cannot open shared object file: No such file or directory 195 | 196 | sudo ldconfig 197 | 198 | Install RMagick 199 | 200 | sudo /opt/ruby/bin/ruby /opt/ruby/bin/gem install rmagick 201 | 202 | Test a rails applicaton with nginx 203 | ---------------------------------- 204 | 205 | rails -d mysql testapp 206 | cd testapp 207 | 208 | Enter your mysql password 209 | 210 | vim database.yml 211 | rake db:create:all 212 | ruby script/generate scaffold post title:string body:text 213 | rake db:migrate RAILS_ENV=production 214 | 215 | Check so the rails app start as normal 216 | 217 | ruby script/server 218 | 219 | sudo vim /opt/nginx/conf/nginx.conf 220 | 221 | Add a new virutal host 222 | 223 | server { 224 | listen 80; 225 | # server_name www.mycook.com; 226 | root /home/deploy/testapp/public; 227 | passenger_enabled on; 228 | } 229 | 230 | Restart nginx 231 | 232 | sudo /etc/init.d/nginx restart 233 | 234 | Check you ipaddress and see if you can acess the rails application 235 | 236 | 237 | -------------------------------------------------------------------------------- /nginx/nginx: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: nginx 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: nginx init.d script for Ubuntu 8.10 and lesser versions. 9 | # Description: nginx init.d script for Ubuntu 8.10 and lesser versions. 10 | ### END INIT INFO 11 | #------------------------------------------------------------------------------ 12 | # nginx - this script, which starts and stops the nginx daemon for ubuntu. 13 | # 14 | # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 15 | # proxy and IMAP/POP3 proxy server. This \ 16 | # script will manage the initiation of the \ 17 | # server and it's process state. 18 | # 19 | # processname: nginx 20 | # config: /usr/local/nginx/conf/nginx.conf 21 | # pidfile: /acronymlabs/server/nginx.pid 22 | # Provides: nginx 23 | # 24 | # Author: Jason Giedymin 25 | # . 26 | # 27 | # Version: 1.0 01-Apr-2009 jason.giedymin AT gmail.com 28 | # Notes: nginx init.d script for Ubuntu 8.10 and lesser versions. 29 | # 30 | #------------------------------------------------------------------------------ 31 | # MIT X11 License 32 | #------------------------------------------------------------------------------ 33 | # 34 | # Copyright (c) 2009 Jason Giedymin, http://AcronymLabs.com 35 | # 36 | # Permission is hereby granted, free of charge, to any person obtaining 37 | # a copy of this software and associated documentation files (the 38 | # "Software"), to deal in the Software without restriction, including 39 | # without limitation the rights to use, copy, modify, merge, publish, 40 | # distribute, sublicense, and/or sell copies of the Software, and to 41 | # permit persons to whom the Software is furnished to do so, subject to 42 | # the following conditions: 43 | # 44 | # The above copyright notice and this permission notice shall be 45 | # included in all copies or substantial portions of the Software. 46 | # 47 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 48 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 49 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 50 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 51 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 52 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 53 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 54 | #------------------------------------------------------------------------------ 55 | 56 | #------------------------------------------------------------------------------ 57 | # Functions 58 | #------------------------------------------------------------------------------ 59 | . /lib/lsb/init-functions 60 | 61 | #------------------------------------------------------------------------------ 62 | # Consts 63 | #------------------------------------------------------------------------------ 64 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 65 | DAEMON=/opt/nginx/sbin/nginx 66 | 67 | PS="nginx" 68 | PIDNAME="nginx" #Lets you do $PS-Master or $PS-Slave 69 | PIDFILE=$PIDNAME.pid #pid file 70 | PIDSPATH=/opt/nginx/logs 71 | DESCRIPTION="Nginx Server..." 72 | 73 | RUNAS=root #user to run as 74 | 75 | SCRIPT_OK=0 #ala error codes 76 | SCRIPT_ERROR=1 #ala error codes 77 | TRUE=1 #boolean 78 | FALSE=0 #boolean 79 | 80 | lockfile=/var/lock/subsys/nginx 81 | NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf" 82 | 83 | #------------------------------------------------------------------------------ 84 | # Simple Tests 85 | #------------------------------------------------------------------------------ 86 | 87 | #test if nginx is a file and executable 88 | test -x $DAEMON || exit 0 89 | 90 | # Include nginx defaults if available 91 | if [ -f /etc/default/nginx ] ; then 92 | . /etc/default/nginx 93 | fi 94 | 95 | #set exit condition 96 | #set -e 97 | 98 | #------------------------------------------------------------------------------ 99 | # Functions 100 | #------------------------------------------------------------------------------ 101 | 102 | setFilePerms(){ 103 | 104 | if [ -f $PIDSPATH/$PIDFILE ]; then 105 | chmod -f 400 $PIDSPATH/$PIDFILE 106 | fi 107 | } 108 | 109 | configtest() { 110 | $DAEMON -t -c $NGINX_CONF_FILE 111 | } 112 | 113 | getPSCount() { 114 | return `pgrep -f $PS | wc -l` 115 | } 116 | 117 | isRunning(){ 118 | pidof_daemon 119 | PID=$? 120 | 121 | if [ $PID -gt 0 ]; then 122 | return 1 123 | else 124 | return 0 125 | fi 126 | } 127 | 128 | status(){ 129 | isRunning 130 | isAlive=$? 131 | 132 | if [ "${isAlive}" -eq $TRUE ]; then 133 | echo "$PIDNAME found running with processes: `pidof $PS`" 134 | else 135 | echo "$PIDNAME is NOT running." 136 | fi 137 | 138 | 139 | } 140 | 141 | removePIDFile(){ 142 | if [ -f $PIDSPATH/PIDFILE ]; then 143 | rm -f $PIDSPATH/$PIDFILE 144 | fi 145 | } 146 | 147 | start() { 148 | log_daemon_msg "Starting $DESCRIPTION" 149 | 150 | isRunning 151 | isAlive=$? 152 | 153 | if [ "${isAlive}" -eq $TRUE ]; then 154 | log_end_msg $SCRIPT_ERROR 155 | else 156 | start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON 157 | setFilePerms 158 | log_end_msg $SCRIPT_OK 159 | fi 160 | } 161 | 162 | stop() { 163 | log_daemon_msg "Stopping $DESCRIPTION" 164 | 165 | isRunning 166 | isAlive=$? 167 | if [ "${isAlive}" -eq $TRUE ]; then 168 | start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE 169 | 170 | removePIDFile 171 | 172 | log_end_msg $SCRIPT_OK 173 | else 174 | log_end_msg $SCRIPT_ERROR 175 | fi 176 | } 177 | 178 | reload() { 179 | configtest || return $? 180 | 181 | log_daemon_msg "Reloading (via HUP) $DESCRIPTION" 182 | 183 | isRunning 184 | if [ $? -eq $TRUE ]; then 185 | `killall -HUP $PS` #to be safe 186 | 187 | log_end_msg $SCRIPT_OK 188 | else 189 | log_end_msg $SCRIPT_ERROR 190 | fi 191 | } 192 | 193 | terminate() { 194 | log_daemon_msg "Force terminating (via KILL) $DESCRIPTION" 195 | 196 | PIDS=`pidof $PS` || true 197 | 198 | [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE` 199 | 200 | for i in $PIDS; do 201 | if [ "$i" = "$PIDS2" ]; then 202 | kill $i 203 | removePIDFile 204 | fi 205 | done 206 | 207 | log_end_msg $SCRIPT_OK 208 | 209 | } 210 | 211 | pidof_daemon() { 212 | PIDS=`pidof $PS` || true 213 | 214 | [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE` 215 | 216 | for i in $PIDS; do 217 | if [ "$i" = "$PIDS2" ]; then 218 | return 1 219 | fi 220 | done 221 | return 0 222 | } 223 | 224 | case "$1" in 225 | start) 226 | start 227 | ;; 228 | stop) 229 | stop 230 | ;; 231 | restart|force-reload) 232 | stop 233 | start 234 | ;; 235 | reload) 236 | $1 237 | ;; 238 | status) 239 | status 240 | ;; 241 | configtest) 242 | $1 243 | ;; 244 | terminate) 245 | $1 246 | ;; 247 | *) 248 | FULLPATH=/etc/init.d/$PIDNAME 249 | echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|terminate}" 250 | exit 1 251 | ;; 252 | esac 253 | 254 | exit 0 255 | --------------------------------------------------------------------------------