假设你已经有了⼀台服务器,⽤ root 帐号通过 SSH 登陆服务器
出于安全考虑,不要使用root账号运行web应用。这里新建一个用户部署的用户,例如 zxx 或者其它你喜欢的名字。运⾏以下命令创建⽤户:
$ useradd -m -s /bin/bash zxx #添加⽤户
$ adduser zxx sudo # 将⽤户加⼊sudo 群组,以便使⽤ sudo 命令 #添加⽤户并赋予管理员权限
$ passwd zxx #为⽤户设置密码
退出当前 SSH 链接,⽤新建的帐号重新登陆
更新 apt并安装curl⽤来安装rvm
$ sudo apt-get update #更新 apt
$ sudo apt-get install curl #安装 curl
安装rvm
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ \curl -sSL https://get.rvm.io | bash -s stable
安装ruby
$ source /home/zxx/.rvm/scripts/rvm #让rvm配置⽣效
$ rvm use --install --default 2.5.1 #安装并默认ruby版本
$ ruby -v #安装完毕后,确认⽬前的 Ruby 版本:
$ gem source -r https://rubygems.org/
$ gem source -a https://gems.ruby-china.org/
$ gem sources -l
#https://gems.ruby-china.org 确保只有 gems.ruby-china.org
Passenger 是⼀个 app server,⽀持基于 Rack 框架的 Ruby app(包括 Rails)。
Passenger 的特点是需要作为模块编译到 Nginx 中,优点是配置简单,不需要⾃⼰写启动脚本。
$ gem install passenger
$ passenger-install-nginx-module
或者
$ rvmsudo passenger-install-nginx-module
#安装带passenger插件的nginx⾃动下载编译nginx
运⾏了这个命令后,按照提示⼀步步安装
遇到这个错误Error: Cannot find the curl-config command.
解决⽅法注意:加上sudo
sudo apt-get install libcurl4-openssl-dev
当出现 Permission problems表示不⾜ 给需要⽤户增加权限su root
切换到root用户注意不要给 /usr/ 赋予权限不然就用不了sudo了,很麻烦
chown -R zxx:zxx Document/ #更改⽂件所属权限,将Document/换成你想要授权的文件夹
注意不要给 /usr/ 赋予权限不然就用不了sudo了,很麻烦
1.Yes: download, compile and install Nginx for me. (recommended) The easiest
way to get started. A stock Nginx 1.0.10 with Passenger support, but with no
other additional third party modules, will be installed for you to a directory of
your choice.2.No: I want to customize my Nginx installation. (for advanced users) Choose this
if you want to compile Nginx with more third party modules besides Passenger,
or if you need to pass additional options to Nginx’s ‘configure’ script. This
installer will 1) ask you for the location of the Nginx source code, 2) run the
‘configure’ script according to your instructions, and 3) run ‘make install’.
Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.Enter your choice (1 or 2) or press Ctrl-C to abort:
当遇到这个选择时,建议选择1,1代表⾃动完整安装并配置nginx,2是代表根据⾃⼰需求定制nginx.
Nginx with Passenger support was successfully installed.
当你看到这个提示的时候表示已经成功安装,请直接进⾏第6步否则请继续
Your compiler failed with the exit status 4. This probably means that it ran out of memory. To solve this problem, try increasing your swap space: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04
当你看到这个提示表示编译pssenger所需空间不⾜,建⽴⼀个⼤⼀点的交换空间
请参考
$ sudo swapon -s #查看交换空间
$ df # 查看文件系统
使用dd命令创建交换文件“of = / swapfile”指定文件的名称。在这种情况下,名称是交换文件。
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=2000000
上面命令中的 count 即代表swap文件大小,可以设置大一点 设置成2000000 为2G
更改已有的swap大小请参考https://blog.csdn.net/yc461515457/article/details/53610412
$ sudo mkswap /swapfile # 随后,我们将通过创建一个Linux交换区域来准备交换文件:
$ sudo swapon /swapfile # 通过激活交换文件完成
当您查看交换摘要时,您将能够看到新的交换文件。
$ swapon -s
Filename Type Size Used Priority
/swapfile file 262140 0 -1
该文件将保留在虚拟专用服务器上,直到机器重新启动。您可以通过将交换添加到fstab文件来确保交换是永久的。
打开文件:
sudo vim /etc/fstab
将下面这行粘贴在其中:
/swapfile none swap sw 0 0
文件中的Swappiness应设置为10.跳过此步骤可能会导致性能下降,而将其设置为10会导致swap作为紧急缓冲区,从而防止内存不足崩溃。
你可以用下面的命令来做到这一点:
echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
为了防止文件被世界读取,您应该在交换文件上设置正确的权限:
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
git clone 你的项⽬(ubuntu 16.04 git安装与配置)
安装mysql
指定版本mysql 将ubuntu14.04 从mysql从5.5删除之后安装5.7遇到的一些问题
$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client
$ sudo apt-get install libmysqlclient-dev #安装mysql环境依赖
安装环境运⾏依赖
$ gem install bundler #安装包管理⼯具
$ sudo apt-get install nodejs #安装nodejs
$ cd blog #进⼊项⽬ blog换成你的项目路径
$ bundle install#运⾏bundle命令
创建数据库运⾏迁移及编译静态⽂件
$ RAILS_ENV=production bundle exec rake db:create
$ RAILS_ENV=production bundle exec rake db:migrate
$ RAILS_ENV=production bundle exec rake assets:precompile
先确保本地能运⾏
$ rails s -p 8080 -e production #访问8080端⼝浏览器可以正常打开项⽬
nginx配置 打开nginx的配置⽂件
$ sudo vim /opt/nginx/conf/nginx.conf
编辑配置⽂件找到该地方写⼊以下内容
server {
listen 80;
server_name example.com; # 这⾥填写你真实域名
root /var/www/example.com/current/public; #这⾥写下项⽬路径具体
到public
passenger_enabled on; #这个⼀定要开启,否则passenger不⼯作
}
nginx命令
$ sudo /opt/nginx/sbin/nginx #启动
$ sudo /opt/nginx/sbin/nginx -s stop #停⽌
$ sudo /opt/nginx/sbin/nginx -s reload #重启
$ sudo /opt/nginx/sbin/nginx -t #检查配置⽂件
$ tail -f sudo /opt/nginx/logs/error.log #查看错误⽇志
启动nginx输⼊ 在浏览器输入你的ip,就可以访问了, 若⽆法访问请检查配置是否正确,或者查看nginx
错误⽇志
问题1:
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
解决::
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn