Install Nginx
0.通过ssh命令行登陆阿里云服务器
ssh root@your_ip_address
1.下载源码包
wget http://nginx.org/download/nginx-1.9.10.tar.gz
2.进入下载路径
$ ./configure
$ make
$ make install
其中configure
文件是Nginx软件的自动脚本程序。运行configure自动脚本会完成两项工作:
一是检查环境,根据环境检查结果生成C代码;
二是生成编译代码需要的Makefile文件。
Start Nginx
安装后程序在usr/local下面,执行
$ ./sbin/nginx
查看是否启动成功
可以使用Linux自带的ps
工具
ps -ef | grep nginx
root 14358 1 0 11:20 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 14359 14358 0 11:20 ? 00:00:00 nginx: worker process
nobody 14360 14358 0 11:20 ? 00:00:00 nginx: worker process
nobody 14361 14358 0 11:20 ? 00:00:00 nginx: worker process
root 14433 14278 0 12:32 pts/1 00:00:00 grep --color=auto nginx
显示启动成功,包括一个主线程和三个worker线程。
配置nginx.conf
下面是整个nginx.conf配置文件
#user nobody;
worker_processes 3;
.
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream nodejs__upstream {
server 127.0.0.1:3000;
eepalive 64;
}
server {
listen 80;
server_name jianqunzhang.com www.jianqunzhang.com;
#charset koi8-r;
#access_log logs/host.access.log main;
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_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://nodejs__upstream;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
其中主要增加修改如下配置
upstream nodejs__upstream {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 80;
server_name jianqunzhang.com www.jianqunzhang.com;
#charset koi8-r;
#access_log logs/host.access.log main;
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_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://nodejs__upstream;
}
其中proxy_set_header
指令可以更改Nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给被代理的服务器。
在服务器启动nodejs,注意监听的端口与配置的nginx.conf要一致。本配置直接监听3000端口。
如果proxy_pass http://nodejs__upstream;
在不设置http://
时,要注意
upstream nodejs__upstream {
server 127.0.0.1:3000;
keepalive 64;
}
的server 要加上http://
,如
upstream nodejs__upstream {
server http://127.0.0.1:3000;
keepalive 64;
}
启动nodejs后,访问所设置的域名即可。
Install PM2
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
npm i pm2 -g
start app
pm2 start bin/www
到目前为止,整个项目算是跑起来了。记录一下同时也希望对小伙伴们有所帮助。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。