一、综合命令重启
进程管理
nginx:
杀掉进程: killall nginx
找到配置文件: nginx -t
启动:nginx安装目录 -c nginx.conf配置文件目录
/usr/local/nginx/sbin/nginx -参数
Nginx 的参数包括:
-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
uwsgi进程 :
ps -ef | grep uwsgi
pkill -f uwsgi -9
二、安装uwsgi
1、多版本python指向选择
1、pip install uwsgi
2、报错:[uwsgi: command not found] 解决方案:建立软链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
1、简单配置
[uwsgi]
socket = 127.0.0.1:8081
pythonpath = /usr/bin/python
chdir= /root/KolDataPortal
chdir= /project/VSPN_DataPortal
wsgi_file=manage.py
callable = app
master = true
vacuum = true
chmod-socket=664
stats=0.0.0.0:9191
listen=128
processes=5
thunder-lock=true
harakiri=90
daemonize= /project/VSPN_DataPortal/logs/uwsgi.log
pidfile= /project/VSPN_DataPortal/uwsgi.pid
2、详细参数
socket : 地址和端口号,例如:socket = 127.0.0.1:50000
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
log-maxsize :以固定的文件大小(单位KB),切割日志文件。 例如:log-maxsize = 50000000 就是50M一个日志文件。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
log-maxsize: 日志大小,当大于这个大小会进行切分 (Byte)
log-truncate: 当启动时切分日志
uwsgi详细配置
uwsgi开头当然少不了通信的接口。
有3种方式:
socket=127.0.0.1:8888
http=0.0.0.0:8888
http-socket=127.0.0.1:8888
三个方式看起来十分的相似,阅读了很多博客和官方文档,下面是自己的一些理解(可能不完全正确)!
图一是socket方式,现在大部分web服务器(如nginx)支持uwsgi, 这是这三种方式最高效的一种形式,socket通信速度会比http快
图二是http-socket方式,这个适用于web服务器不支持uwsgi时。
后面2个图都是http方式,使用http启动uwsgi,系统会额外启动一个http进程,从级别上来说,它和nginx是同一级别的,所以客户端和uwsgi通信,完全可以绕过nginx,不需要额外进行一个转发(如图三一样),但很显然,这是并不是一个很明智但选择,这样会失去了nginx很多优秀的功能。
官方推荐的方式为socket以及http-socket方式,显然使用http方式会额外产生一个http进程,如果还通过nginx转发,那么效率上来说是相对比较低的。
二、安装nginx
1、nginx安装
1、安装依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2、下载安装包,解压并cd目录
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
3、配置
./configure --with-http_ssl_module --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_realip_module --with-http_image_filter_module=dynamic
4、编译、安装
make
make && make install
5、查找按照路径
whereis nginx
6、命令管理
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
2、nginx简单http配置单应用配置
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
server {
listen 80 default_server;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3306;
uwsgi_param UWSGI_CHDIR /root/Website;
uwsgi_param UWSGI_SCRIPT demo:app;
uwsgi_read_timeout 300;
uwsgi_connect_timeout 300;
uwsgi_send_timeout 300;
}
}
}
3、前后端分离配置
server {
listen 80;
server_name xxxx.com www.xxxx.com;
location / {
root /root/works_app/dist; # 生成的静态文件的地址
index index.html index.htm;
try_files $uri /index.html;
}
location /api {
proxy_pass http://127.0.0.1:81; # 反向代理到后端
proxy_set_header Host $host;
}
}
server {
listen 81;
server_name xxxx.com; # 域名,更换成自己的域名
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
uwsgi_param UWSGI_CHDIR /code/DataPortal;
uwsgi_param UWSGI_SCRIPT manage:app;
uwsgi_param UWSGI_SCRIPT manage:app;
uwsgi_read_timeout 300;
uwsgi_connect_timeout 300;
uwsgi_send_timeout 300;
}
}
2、https待更新
小伙伴们若果觉得不错,可以给一波关注,谢谢