Deploy your own Rails application on Ubuntu cloud server

I have been learning rails for a while. I only used heroku to deploy the website before. I wanted to try to present the website in a more "serious" way, so I bought an Alibaba Cloud server. Referring to some online rails deployment tutorials, I also encountered some problems in the process, so I summarized it after completion, and wrote this article to facilitate other beginners like me to quickly deploy a rails website to a cloud server in the future. Do not seek the principle, only talk about the operation, and strive to be simple and easy to understand.

Note: This article makes a lot of reference to another article in ruby ​​china: "Rapid Deployment of Ruby on Rails on Aliyun" , and makes minor supplements based on some of the problems I encountered

my local environment

OS: Ubuntu 16.04 32 bit
Ruby: 2.3.0
Rails: 4.2.6

Step1: Buy a cloud server

Take the Alibaba Cloud server as an example. I bought the minimum configuration, Ubuntu 16.04 operating system. After success, it will give you a public IP. Use ssh to connect to this IP to configure this server ubuntu. Alibaba Cloud console Configurable cloud server password. Now suppose the cloud server information I bought is as follows:

OS: Ubuntu 16.04 32 bit
Public IP: 190.74.8.177
login password:******
Database: MySQL

Note: I bought a virtual host for the first time, and then I found that the virtual host cannot be configured through ssh, and the rails environment cannot be installed... Fortunately, I can get a refund...

Step2: Connect to cloud server

Now it is equivalent to buying a computer back. This computer has no physical body, but I can log in remotely from anywhere. Login and enter your password:

$ ssh [email protected]
[email protected]'s password: ******

Then it will enter the server account, but it is not safe to enter the root user forever, so after logging in, we create a new user on the server side:

~# adduser sofly

Then we ctrl+D to exit the server and log in locally as the sofly user:

$ ssh [email protected]
[email protected]'s password: ******

Note: The process of entering passwords can be omitted by configuring ssh-keygen here, which will not be introduced here. You can refer to other articles

Step3: Install Ruby on the cloud server

The following basic reference "Rapidly deploy Ruby on Rails on Aliyun" will not be described in detail. First install RVM:

$ \curl -L https://get.rvm.io | bash -s stable
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >>~/.bashrc $ source ~/.bashrc $ rvm -v 

Replace the RVM source with the taobao source:

$ sed -i -e 's/ftp\.ruby-lang\.org\/pub\/ruby/ruby\.taobao\.org\/mirrors\/ruby/g' ~/.rvm/config/db 

Install RVM dependencies, etc.

$ rvm requirements
$ rvm pkg install readline
$ rvm pkg install openssl 

Install Ruby, the version is the same as my local ruby

$ rvm install 2.3.0
$ rvm use 2.3.0 --default

After installation, change the source of Rubygems to the source of Alibaba Cloud

$ gem source -r https://rubygems.org/
$ gem source -a http://mirrors.aliyun.com/rubygems/ 

Step4: Install Rails and database on cloud server

Continue to plagiarize this article. Install Rails, the version is the same as my local rails

$ gem install rails -v 4.2.6

Install MySQL

$ sudo apt-get install mysql-server

or PostgreSQL

$ sudo apt-get install postgresql postgresql-client libpq-dev

Step5: Upload the local Rails project to the cloud server

Because my rails projects have been submitted to github or bitbucket before (how to submit to git will not be repeated here, please refer to related articles), so I can directly download the code to the server through git.

$ git clone https://XXXXXX/project.git (在github或bitbucket上面就可以找到,直接复制)

This way the code is downloaded to the server, and then the gem is installed

$ cd project
$ bundle install

Create production database and perform migrations

$ RAILS_ENV=production rake db:create
$ RAILS_ENV=production rake db:migrate

Otherwise, the final website page will be displayed (it was often encountered when deploying on heroku before)

We're sorry, but something went wrong

Recompile assets so all images, CSS, scripts will load

$ RAILS_ENV=production rake assets:precompile

Step6: 安装Passenger for Nginx

Nginx is an HTTP server. Running nginx is similar to opening a rails server locally to achieve website access. First, install passenger:

$ gem install passenger

Then install Nginx by compiling from source

$ rvmsudo passenger-install-nginx-module

Just press Enter all the way, choose 1 Enter here:

Automatically download and install Nginx?Nginx doesn't support loadable modules such as some other web servers do, so in order to install Nginx with Passenger support, it must be recompiled.Do you want this installer to download, compile and install Nginx for you?
1. Yes: download, compile and install Nginx for me. (recommended) The easiest way to get started. A stock Nginx 1.4.4 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[ENTER]

Finally, when you see this sentence, the installation is successful

Nginx with Passenger support was successfully installed.

Step7: Say a few words about the pits of Nginx

Many tutorials install nginx through apt, and we installed it through recompile Nginx from source. The nginx directory of the former is /usr/sbin/nginx, the configuration file directory is /etc/nginx/nginx.conf, the nginx directory of the latter is /opt/nginx/sbin/nginx, and the configuration file directory is /opt/nginx/conf /nginx.conf is different, it is best to confirm the location of these files before proceeding with subsequent operations. Another difference is that nginx starts and stops differently. The nginx installed by apt-get can be started by the initialization script /etc/init.d/nginx or the command service nginx restart, but the current source installation method cannot be started by these two methods. . The startup method is as follows:

$ sudo /opt/nginx/sbin/nginx

The way to stop is as follows:

$ ps auxw | grep nginx
root     29743  0.0  0.0  10192   564 ?        Ss   13:39   0:00 nginx: master process /opt/nginx/sbin/nginx
sofly    29744  0.0  0.4  10428  4500 ?        S    13:39   0:00 nginx: worker process
sofly    30080  0.0  0.0   5108   792 pts/1    S+   13:42   0:00 grep --color=auto nginx $ sudo kill 29743 

Therefore, if you want to restart nginx, you must first kill it at startup. Although it is a bit cumbersome, it is available for personal testing.

Reference here: [ https://www.phusionpassenger.com/library/install/nginx/install/oss/rubygems_rvm/ ]

After starting Nginx, enter your public IP in the browser, namely: 190.74.8.177, see

Welcome to nginx page

Indicates that the nginx server has been successfully started, but the rails has not been connected

Step8: Configure Nginx to start rails

Edit the nginx configuration file

$ sudo vim /opt/nginx/conf/nginx.conf

My configuration file is like this, just pay attention to a few comments

user  sofly; #此处设置为部署时的用户名
worker_processes  1; #此处为云服务器核数 error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid /run/nginx.pid; #此处为nginx.pid的目录,位置应该是在这里 events { worker_connections 1024; } http { passenger_root /home/sofly/.rvm/gems/ruby-2.3.0/gems/passenger-5.1.2; #去掉这两处前面的注释符号# passenger_ruby /home/sofly/.rvm/gems/ruby-2.3.0/wrappers/ruby; #去掉这两处前面的注释符号# include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; gzip_disable "msie6"; server { listen 80; server_name localhost; passenger_enabled on; root /home/sofly/project/public; #此处设着为rails工程public文件夹位置 } } 

Restart nginx after configuration is complete

$ ps auxw | grep nginx
root     29743  0.0  0.0  10192   564 ?        Ss   13:39   0:00 nginx: master process /opt/nginx/sbin/nginx
sofly    29744  0.0  0.4  10428  4500 ?        S    13:39   0:00 nginx: worker process
sofly    30080  0.0  0.0   5108   792 pts/1    S+   13:42   0:00 grep --color=auto nginx $ sudo kill 29743 $ sudo /opt/nginx/sbin/nginx 

Enter the server IP in the browser and find that the web page is not displayed, but the following words appear

incomplete response received from application

What happened...

Step9: Configure the production secret_key_base of the rails project

The reason for the above problem is that the secret_key_base variable is not configured in the rails production environment. The solution:

$ cd project
$ bundle exec rake secret # rails 4.2.6还需要bundle exec,请根据rails版本自行匹配 

Paste the output string of characters into /config/secrets.yml in the rails project, and replace <%= ENV["SECRET_KEY_BASE"] %> in the file, as follows:

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Then restart the passenger, (must have it or it will not take effect)

$ touch project/tmp/restart.txt

Now refresh your browser and you will see that the website is displayed successfully.

Reference here: [ http://stackoverflow.com/questions/29241053/incomplete-response-received-from-application-from-nginx-passenger ]

Note: If it still fails, please check the error log in /opt/nginx/logs/error.log

PS. The first post after switching from hardware to back-end, I hope you will correct me if there are any problems. above.

=====================================================

Continuous addition: pits encountered during deployment

  1. The Rails production environment uploads and resizes the image and reports an error:

"We're sorry, but something went wrong."

Reason: imagemagick is not installed

$ sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325360829&siteId=291194637