markdown 如何部署Rails应用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如何部署Rails应用程序相关的知识,希望对你有一定的参考价值。

## Components
### Server (computers that serve data)
* **HTTP/web server**: to recieve incoming requests
* **application server** to run Rails

### **VPS**: Virtual Private Server (current way to set up a Rails app online)
* Runs its own OS
* Super-user (admin) level access 
    * More easier to configure
    * Popular (and cheap) VPS is OceanWave
    * Alternative is using Heroku

### Database
* DigitalOcean uses MySQL
* Heroku uses PgSQL (postgresql)

### Management
* Managing the servers (Rails is managed by using **SSH**, not shared hosting)
    * Other sites may be managed by _shared hosting_: web hosting service that provides hosting to 
    different websites.
* Two types of hosting:
    * managed: example is heroku
        * Most managed hosting runs the LAMP stack (Linux/Apache/MySQL/PHP) 
        * The free tier makes your site sleep after a certain time of inactivity, so its not 
        good for production
    * unmanaged: example is digitalOcean

---

### Web Works?
* Internet = TCP/IP (a communications protocol to connect 2 systems together via an IP address)
* Domains were created to mask IP addresses
* Web hosting is basically creating a web server to make your IP address public and display your
files (web app) to the public
    * _The job of a web hosting company is to provide access to servers so that you don't need a
    computer running in your basement!_
    * Web hosting works by providing a public IP to which you point our domains to via their
    DNS listing. For example: IP address: 198.33.443.234 points to www.xyz.com
* Rails works outside the scope of HTTP (web), because it doesn't have HTML files, it cannot
rely on web server software to deliver responses. It needs another piece of software to help
it process requests _**application server software**_ (example Phusion Passenger)
    * This runs on the server and forms a response for HTTP requests, mimicking what HTML
    files are meant to do.
    * Passenger is not installed on most shared hosts, it cannot run Rails.
    * Needs other dependencies to get a Rails-compatible server running => **stack**

---

### STACK
* Includes:
    * Public IP (DNS)
    * VPS/ Server (physical box)
    * OS
    * Programming Language/Libraries
    * Web Server
    * Application Server
    * Database Server
* All hosting companies provide IP & server, the other components in the list are determined by
the type of hosting
* Every web app has to adhere to the HTTP protocol
* Rails stacks needs the following:
    * SSH access(to push code)
    * Root level dependency installation
    * Ruby implementation
* Crucial element is `Passenger`: application server that run Rails

---

## Server Setup
* Set up a server (physical box)
    * Either managed or unmanaged (2 types of hosting)
    * For Rails, the only managed hosting is Heroku
    * Use heroku for **staging** and another server for **production**
  
### Heroku (STAGING)
* Has 2 servers: _build_ and _runtime_
    * When you push to heroku, you push to heroku's _build server_
        * Compiles into what heroku calls a slug
    * Slug is then placed on to a runtime server (cedar-14 and heroku-16)
    
### DigitalOcean (PRODUCTION)
* Can choose your own OS
* Size of the server
* Location for highest traffic for your datacenter (ex. NYC)
* Set the name for your primary domain of your website.
    * This will then provide you with an IP address, which is what you route your domains to
* Next, point your domain to the server via its DNS records: requires using another provider to
handle this routing. (example Namecheap)

### Setting up DNS
* Managed services provide name servers whihc are a simple way to route domain name traffic.
* DNS Zone File (if using a private VPS)
    * Number of DNS records you can set (A, CNAME, MX)
    * `A` records return a 32bit IPv4 address, most commonly use to map hostnames to an IP address
    of the host
    * You'll need to create 2 `A` records, one for `www` and the other for `@`, both will point
    to the IP of the server.
* Most VPS provide OS images when you set up a server.
* Need to also set up SSH: protocol used to connect to the newly created VPS

### Dependencies
* Install the dependencies and build tools for Rails (on the OS)

```
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 libffi-dev nodejs imagemagick 
libmagickwand-dev
```

### Install Ruby (on the OS)

```
cd
wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
tar -xzvf ruby-2.4.1.tar.gz
cd ruby-2.4.1/
./configure
make
sudo make install
ruby -v
```

* Next, install `rails` and `bundler`
* Install them on a global/system level to make them accessible to all apps


### Web Server
* Need to install web server software (on OS)
* `nginx` is used, faster than `Apache` and comes bundled with Passenger

```
sudo apt-get install -y dirmngr gnupg
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

# Add our APT repository
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger zesty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

# Install Passenger + Nginx module
sudo apt-get install -y libnginx-mod-http-passenger libnginx-mod-http-headers-more-filter nginx

```

* Test nginx works: `sudo nginx -t`
* If successful, restart the `nginx` service

### Set up a GIT Repo (on the OS)
* Create a bare GIT repo
* Use a `post-receive` hook to push code to the deploy folder
* Push code
* Bundle
* Restart
* Test
* Deploy using the GIT repo and a `post-receive` hook

[More Info](https://medium.com/ruby-on-rails-web-application-development/how-to-deploy-ruby-on-rails-apps-to-the-internet-production-staging-49efc503c91d)

以上是关于markdown 如何部署Rails应用程序的主要内容,如果未能解决你的问题,请参考以下文章

markdown 如何解决Rails(OSX)中的OpenSSL错误?

如何在 OpsWorks 部署到 Rails 堆栈期间始终运行迁移

如何将 Rails + Webpacker 应用程序部署到 Heroku?

如何使用 Redcarpet for Rails 在 Markdown 中嵌入 YouTube 视频?

部署了两个 Rails 应用程序 - 如何更改默认应用程序?

如何将 Rails 应用程序部署到 EC2(亚马逊)