python程序环境配置

1.程序包括一个虚拟环境目录 venvs 里面是对应程序对应的环境目录

 

 

 

 例如 :env_center是对应程序的虚拟环境

 

还应包含程序目录和配置目录

例如:unidairy-center 为程序目录 unidairy_center_conf为配置目录

2.配置目录 包含三个部分

 

 

a.nginx配置

nginx 软连接
ln -s /data/web/unidairy_center_conf/unidairy_center.conf   /etc/nginx/conf.d/unidairy_center.conf

配置 /etc/nginx/conf.d/unidairy_center.conf

 

upstream boxcenter{ # 名字boxcenter可以被DIY  一个程序命名一个
# connect to this socket
server unix:///data/web/unidairy-center/uwsgi.sock; # 通过socket访问
# server 127.0.0.1:8001; # 通过http访问
# -.-! 如果运行在云平台, 此处应使用内网IP地址
}

 

server {
# 指定浏览器或api要访问的接口端口
# 你可以在浏览器输入 example.com:8000 访问网站
listen 30002;
# the domain name it will serve for
server_name sz.xxxxxxxx.net; # 代替为你的IP或域名
# -.-! 如果是运行在云平台,此处应使用内网IP地址
charset utf-8;

 

proxy_read_timeout 12000;
proxy_send_timeout 12000;
#Max upload size
client_max_body_size 75M; # adjust to taste

 


# Django media
location /media {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
alias /data/web/unidairy-center/media; # Django项目的media files路径
}

 

location /static {
alias /data/web/unidairy-center/static; # Django项目的static files路径
}

 

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass boxcenter; #名字boxcenter要和上面对应上
include /etc/nginx/uwsgi_params; #
uwsgi_read_timeout 12000;
}
}

 

 

b.uwsgi配置 例如:unidairy_center.ini

[uwsgi]

# master
master = true

# maximum number of processes
processes = 10

# the socket (use the full path to be safe)
# socket 的具体放置位置
# 服务器开启时自动生成
socket = /data/web/unidairy-center/uwsgi.sock
#socket = 10.160.4.104:8081

# with appropriate permissions - *may* be needed
# 重要!对socket设置666权限,增删改除
# 如果没有设置,有可能网络访问不了,因为没有权限创建
chmod-socket = 666

# the base directory  程序目录
chdir = /data/web/unidairy-center

# Django's wsgi file
# 因为 加了 chdir ,所有直接输入 project文件内的路径
# 注意! project.wsgi 中间是 “.” 而不是 “/”  aladdin_plus_auth为你的程序setting.py  ROOT_URLCONF = 'aladdin_plus_auth.urls'中的配置
module = aladdin_plus_auth.wsgi:application

# the virtualenv
home = /data/venvs/env_center

# clear environment on exit
vacuum = true

procname =unidairy_center_uwsgi #可执行文件的名字 必须对应

 

c.uwsgi可执行文件

#可执行文件名字 unidairy_center_uwsgi

touch  unidairy_center_uwsgi 

uwsgi配置 vim   unidairy_center_uwsgi 

NAME="unidairy_center_uwsgi" 
if [ ! -n "$NAME" ];then
echo "no arguments"
exit;
fi

echo $NAME
ID=`ps -ef | grep -w "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
echo "################################################"
for id in $ID
do
kill -9 $id
echo "kill $id"
done
echo "################################################"
#uwsgi配置目录 log目录 虚拟环境的激活
uwsgi --ini /data/web/unidairy_center_conf/unidairy_center.ini --daemonize /var/log/unidairy_center_uwsgi.log
cd /data/venvs/env_center/bin
source activate
#python /opt/aladdin-v10-backend/manage.py crontab remove
#python /opt/aladdin-v10-backend/manage.py crontab add
#python /opt/aladdin-v10-backend/manage.py crontab show

保存后给可执行文件权限

chmod u+x unidairy_center_uwsgi

执行:进入unidairy_center_uwsgi所在目录 ./unidairy_center_uwsgi

3.vue等静态文件的配置

例如front静态文件 放入给程序同目录下面

配置nginx   /etc/nginx/conf.d/front.conf

server {
listen 30004;
server_name sz.xxxxxx.net;

#access_log /var/log/nginx/host.access.log main;

location / {
root /data/web/front;
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 /usr/share/nginx/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;
#}
}

 

posted @ 2022-11-28 14:50  左闯  阅读(102)  评论(0编辑  收藏  举报