|
| 1 | +## Setup own server, without Docker and cloud-based solutions |
| 2 | + |
| 3 | +This approach is relevant if you want to: |
| 4 | + |
| 5 | +* speed up your dev-computer |
| 6 | +* deploy application on a dedicated server (or vps) |
| 7 | +* run app in corporate network |
| 8 | + |
| 9 | +General scheme will looks like: |
| 10 | +``` |
| 11 | ++------+ +------------+ +----------------+ |
| 12 | +| user +---> | web server +----> | express server | |
| 13 | ++------+ +------------+ +----------------+ |
| 14 | +``` |
| 15 | + |
| 16 | +First you need pre-installed and configured linux/nix system (not windows/osx). |
| 17 | +I will consider the example of ubuntu-server. |
| 18 | + |
| 19 | +Install nvm (node version manager): |
| 20 | + |
| 21 | +``` |
| 22 | +$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash |
| 23 | +``` |
| 24 | + |
| 25 | +Then install Node.js 5.x, and setup default node version: |
| 26 | +``` |
| 27 | +$ nvm install 5.4 |
| 28 | +$ nvm alias default 5.4 |
| 29 | +``` |
| 30 | + |
| 31 | +Then you need process manager for Node.js app server. This may be one of the: |
| 32 | + |
| 33 | +* [pm2](https://github.com/Unitech/pm2) |
| 34 | +* [forever](https://github.com/foreverjs/forever) |
| 35 | +* other managers... |
| 36 | + |
| 37 | +I chose `pm2`: |
| 38 | +``` |
| 39 | +$ npm install pm2 -g |
| 40 | +``` |
| 41 | + |
| 42 | +Next you need build you app and upload on server, for example to `/var/www/myapp` |
| 43 | +(better to use git hooks or npm task for this). |
| 44 | + |
| 45 | +``` |
| 46 | +$ BABEL_ENV=production npm run build -- --release && rsync . to@my.host:/var/www/myapp |
| 47 | +``` |
| 48 | + |
| 49 | +If you app successfully loaded, configure you process manager and start application: |
| 50 | + |
| 51 | +```shell |
| 52 | +web@server:/var/www/myapp# NODE_ENV=production PORT=5000 NAME=awesomeapp.com pm2 add --name "my-awesome-app" ./build/server.js |
| 53 | + |
| 54 | +┌─────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐ |
| 55 | +│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │ |
| 56 | +├─────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤ |
| 57 | +│ my-awesome-app │ 1 │ fork │ 777 │ online │ 0 │ 0m │ 7 MB │ disabled │ |
| 58 | +└─────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘ |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +Please note that we have specified `NODE_ENV=production` and other environment |
| 63 | +variables (NAME and PORT). |
| 64 | + |
| 65 | +Next you need web server, such as: |
| 66 | + |
| 67 | +* nginx |
| 68 | +* apache2 |
| 69 | +* lighttpd |
| 70 | +* other web-servers.. |
| 71 | + |
| 72 | +Once you have installed the server, you will need to configure it. |
| 73 | + |
| 74 | +Basic apache2 (with mod_proxy) configuration could look like this: |
| 75 | +``` |
| 76 | +<VirtualHost *:80> |
| 77 | + ServerAdmin i@awesomeapp.com |
| 78 | + ServerName awesomeapp.com |
| 79 | + ServerAlias www.awesomeapp.com |
| 80 | +
|
| 81 | + ProxyRequests off |
| 82 | +
|
| 83 | + <Proxy *> |
| 84 | + Order deny,allow |
| 85 | + Allow from all |
| 86 | + </Proxy> |
| 87 | +
|
| 88 | + <Location /> |
| 89 | + ProxyPass http://127.0.0.1:5000/ |
| 90 | + ProxyPassReverse http://127.0.0.1:5000/ |
| 91 | + </Location> |
| 92 | +
|
| 93 | +</VirtualHost> |
| 94 | +``` |
| 95 | + |
| 96 | +Basic nginx configuration: |
| 97 | +``` |
| 98 | +upstream nodeserver { |
| 99 | + server 127.0.0.1:5000; |
| 100 | +} |
| 101 | +
|
| 102 | +server { |
| 103 | + listen 0.0.0.0:80; |
| 104 | + server_name awesomeapp.com; |
| 105 | + access_log /var/log/nginx/awesomeapp.com.log; |
| 106 | +
|
| 107 | + location / { |
| 108 | + proxy_set_header X-Real-IP $remote_addr; |
| 109 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 110 | + proxy_set_header Host $http_host; |
| 111 | + proxy_set_header X-NginX-Proxy true; |
| 112 | +
|
| 113 | + proxy_pass http://nodeserver/; |
| 114 | + proxy_redirect off; |
| 115 | + } |
| 116 | + } |
| 117 | +``` |
| 118 | + |
| 119 | +Setup and reload web server - now your application server is ready. |
0 commit comments