How to deploy ruby on rails with thin on Ubuntu
Create a deploy user
sudo adduser deploy
sudo adduser deploy sudo
su deploy
update and install packages
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties
Install RVM
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.5
rvm use 2.1.5 --default
ruby -v
echo "gem: --no-ri --no-rdoc" > ~/.gemrc
Git Server
RoR比较流行的是使用git,我们来配置一下Git Server,首先我们需要新建一个名为git的用户并且为它添加公钥。
$ sudo adduser git
$ sudo vi /home/git/.ssh/authorized_keys
我这里假设你的项目名称是project和准备把git仓库的目录设定为/opt/git,如果你没有一个/opt/git目录,需要切换到root新建并且把这个目录的所有者改为git:
$ su - root
# cd /opt
# mkdir git
# chown git git
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init
本地电脑
$cd /git/project
$git remote add alt alt-machine:/path/to/repo
$git push alt master
这样会把本地项目同步到git server
Installing MySQL
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
创建用户
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
更改用户密码
mysqladmin -u USER -p password NEWPASSWORD
创建数据库
CREATE DATABASE database name;
mysql.sock
vi /etc/mysql/my.cnf
You will find the lines below top in your configuration file
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
install thin
gem install thin
thin config -C /etc/thin/myapp.example.com -c /var/www/myapp.example.com --servers 3 -e development # or: -e production for caching, etc
install nginx
sudo apt-get install nginx
nginx configuration
#/etc/nginx/sites-enabled/myapp.example.com
upstream myapp {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
server_name .example.com;
access_log /var/www/myapp.example.com/log/access.log;
error_log /var/www/myapp.example.com/log/error.log;
root /var/www/myapp.example.com;
index index.html;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
}
location @ruby {
proxy_pass http://myapp;
}
}
ln -nfs /etc/nginx/sites-available/myapp.example.com /etc/nginx/sites-enabled/myapp.example.com
Start app
thin start -C ~/myapp.example.com
/etc/init.d/nginx reload
/etc/init.d/nginx restart
用git更新项目
ps -aux
cd your_project_dir
git fetch https://github.com/xxx/yyy.git
git pull https://github.com/xxx/yyy.git
thin start -C ~/myapp.example.com
/etc/init.d/nginx reload
/etc/init.d/nginx restart