1.python3基础环境安装
tar xzvf Python-3.6.8.tgz
cd Python-3.6.8
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
2.python3安装虚拟环境
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
3.创建虚拟环境
# virtualenv -p (指定python版本的解释器) (创建的虚拟环境存放目录+虚拟环境名称)
virtualenv -p /usr/bin/python3 /data/.envs/hn_portal
# 虚拟环境存放目录:/data/.envs/
# 虚拟环境名称: hn_portal
4.项目代码存放
# 将项目放置到某一个目录下 样例:/data/apps/projects/
# 进入到项目目录下
cd /data/apps/projects/hn_portal
5.在项目目录下进入到创建好的虚拟环境 hn_portal
source /data/.envs/hn_portal/bin/activate
6.安装项目依赖包到虚拟环境
# 进入虚拟环境hn_portal后,安装依赖包
pip3 install -r requirements.txt
7.确定端口是否被占用(准备好端口18085和一个备用端口18084)
# 避免启动项目时,使用已被占用的端口
lsof -i:18085
# 如果端口被占用,使用备用端口
8.启动项目
# 0.0.0.0:18085 端口18085
python manage runserver 0.0.0.0:18085
# 命令窗口关闭不影响项目一直运行
nohup python manage runserver 0.0.0.0:18085
9.查找nginx的配置目录
nginx -V
结果:
nginx version: nginx/1.18.0
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-
配置的目录:
--conf-path=/etc/nginx/nginx.conf
命令执行的目录:
--sbin-path=/usr/sbin/nginx
10.配置nginx.conf(找到nginx.conf所在目录)
nginx.conf配置
# user nginx;
user root;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 在nginx目录下建立conf.d目录 在conf.d目录下配置多个.conf文件进行项目区分管理
include /etc/nginx/conf.d/*.conf;
}
11.hn_portal.conf文件内容配置
存放目录位于:/etc/nginx/conf.d/
server {
# 对外端口
listen 18085;
# 对外域名
server_name xxxx;
location / {
root /data/apps/projects/hn_portal/templates/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
# 代表项目主机端口构成的url
proxy_pass http://xxxx:18085/;
proxy_set_header Host $host:$server_port;
}
}
12.查看配置hn_protal.conf语法是否错误
/usr/sbin/nginx -t
错误:
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/hn_portal.conf:4
nginx: configuration file /etc/nginx/nginx.conf test failed
正常:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
13.hn_portal.conf 配置完成后重新加载nginx服务器(配置检查语法正常后再执行)
# 服务不间断加载
/usr/sbin/nginx -s reload