准备工作
服务器:centos7.6
证书:
域名:www.xxxx.com
注意:如果你的服务器需要通过域名进行访问,那么首先确认域名是否备案。
下面开始搭建服务器
一.安装nodejs https://blog.csdn.net/d81895606/article/details/89335215
二.安装mongndb https://blog.csdn.net/d81895606/article/details/89333958
三.安装Nginx
一键安装所有依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
在/usr/local/文件夹下创建nginx安装文件夹,
cd /usr/local
mkdir nginx
下载nginx并且解压缩
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
安装nginx
cd /usr/local/nginx-1.13.7
./configure
make
make install
测试nginx是否安装完毕及一些常用命令
./usr/local/nginx/sbin/nginx -t
四.配置https
配置nginx.conf文件 这个文件在你安装的nginx下的conf文件夹下
文件当中第一个server
server {
listen 80;
server_name localhost;#域名或者服务器ip
rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把http的域名请求转成https配置完成后以后再访问http会直接跳转到https
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
}
把最下面的HTTPS server 注释打开改成下面的样子
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;#域名或者服务器ip 注意需要和上面写的保持一致
ssl_certificate #你的证书pem文件;
ssl_certificate_key 你的证书key文件;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {#访问接口的url
proxy_pass http://127.0.0.1:3000/;
}
location ~ .*\.(gif|jpg|jpeg|png)$ { #访问图片的路径
expires 24h;
root /data/images/;#指定图片存放路径 我是将data文件夹创建在根目录下了
access_log /usr/local/nginx/logs/images.log;#图片 日志路径
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /data/images/;#代理临时路径
proxy_redirect off;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 1280k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
}
}
使用./usr/local/nginx/sbin/nginx -t测试文件是否正常
如果正常使用/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 开启nginx
五.常用命令
//启动命令
安装路径下的/nginx/sbin/nginx
//停止命令
安装路径下的/nginx/sbin/nginx -s stop
或者 : nginx -s quit
//重启命令
安装路径下的/nginx/sbin/nginx -s reload
//查看进程命令
ps -ef | grep nginx
//查看进程
ps -ef|grep nginx
//强制关闭
pkill -9 nginx
//平滑重启
kill -HUP Nginx主进程号
六.配置过程中遇到的问题
1.如果配置完成后nginx启动正常但是无法访问尝试使用
/sbin/iptables -I INPUT -p tcp --dport 3000 -j ACCEPT 开放端口
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf //开启nginx
2.配置完https后./nginx -t 报错
参考https://blog.csdn.net/Jioho_chen/article/details/81516581
七.安装pm2启动nodejs
npm install -g pm2