一、需要准备一台centos7服务器,能连接网络,掌握基本的操作命令
二、安装Python3.9
1 使用wget命令下载,后解压。
2.安装必要依赖
yum install openssl-devel -y
yum install zlib-devel -y
3.进入文件目录
cd /usr/local/Python-3.9
4.解压,编译安装
yum install -y gcc gcc-c++(编译代码得有编译器)
./configure --prefix=/usr/local/python3.9
make -j 4 && make install
5.做软连接(Centos自带python2.7,很多软件基于python2.7,所以不要覆盖python2.7的软连接(python)))
ln -s /usr/local/python3/bin/python3.9 /usr/local/bin/python3
如果/usr/local/python3/bin下有pip3文件,则
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3,然后跳过第四个步骤
如果没有,则需要安装pip3
三、安装virtualenv虚拟环境
1、相信大家在实际工作中难免遇到多项目开发或测试的情况,那么企业中的不同项目基本都是使用不同的python版本或者各个不同版本的第三方模块,所以当你对本机所安装的python第三方模块很难进行统一管理,这个时候虚拟环境就派上用场了,简单地说,你可以通过给每个项目都配置一个专属的python解释器以及所安装的第三方模块,这样就实现了对所有项目进行单独隔离管理,互不干扰。管理虚拟环境的方式有很多,根据你的项目具体情况具体分析,最常使用的便是virtualenv。
pip3 install virtualenv
2、建立软连接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
3、新建三个目录,用来存放python环境和项目文件
mkdir /usr/local/demo
mkdir /usr/local/demo/env
mkdir /usr/local/demo/pojects
4、进入env目录,并创建虚拟环境
cd /usr/local/demo/env
virtualenv --python=/usr/bin/python3 pyweb
pyweb是自行命名的。
5、激活环境
cd pyweb/bin
source activate #激活环境
deactivate #如果需要退出当前虚拟环境
查看当前环境的库 : pip3 list
6、在激活的虚拟环境中安装django和uwsgi
pip3 install django==1.11.7(版本号) (如果用于生产的话,则需要指定安装和你项目相同的版本)
pip3 install uwsgi
7、进入/usr/local/demo/pojects目录,创建django项目
cd /usr/local/demo/pojects
django-admin.py startproject mysite
修改访问权限:
vi mysite/settings.py
修改ALLOWED_HOSTS,['*'],可以让任何IP访问,保存退出
8、启动项目django:
python3 manage.py runserver 0.0.0.0:8888
如果sqlite版本过低会报错,需要升级版本
在此不做演示
9、为了解决负载均衡,动静分离即动态文件和静态文件分离,则需要使用nginx+uwsgi
首先要对django项目进行收集静态文件
在setting.py文件设置静态目录
STATIC_ROOT = os.path.join(BASE_DIR,"static")
进入到项目目录,执行命令
python3 manage.py collectstatic
接着配置uwsgi
在项目目录下
创建uwsgi.ini文件
# uwsgi.ini文件详情
[uwsgi]
# 项目目录
chdir=/usr/local/demo/projects/msgsite
wsgi-file=/usr/local/demo/projects/msgsite/msgsite/wsgi.py
#启动uwsgi的用户名和用户组
uid=root
gid=root
#指定项目的application,这个使用后会报错,找不到msgsite,使用wsgi-file的设置可以成功运行。
#module=msgsite.wsgi:application
#使用socket后要把http注释掉
#http=0:8001
#指定socket的文件路径
# 与nginx通讯用
socket=0.0.0.0:8002
#启用主进程
master=true
#进程个数
workers=4
pidfile=uwsgi.pid
#自动移除unix socket和pid文件当服务停止时候
vacuum=true
#序列化接受的内容,如果可能的话
thunder-lock=true
#启用线程
enable-threads=true
#设置中断时间
harakiri=30
#设置缓冲
post-buffering=1028
#设置日志目录
#日志保存地点
daemonize=/usr/local/demo/projects/msgsite/msgsite/uwsgi.log
运行uwsgi --ini uwsgi.ini 成功后会生成uwsgi.pid文件,使用uwsgi --stop uwsgi.pid 停止服务。
#运行
uwsgi --ini uwsgi.ini
#停止
uwsgi --stop uwsgi.pid
一般uwsgi都是在虚拟环境中进行安装和运行。
10 安装nginx ,nginx则不用在虚拟环境中安装
安装依赖
# 安装依赖
yum install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel -y
# 安装必须的pcre
wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
tar -xzvf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make && make install
安装nginx
wget http://nginx.org/download/nginx-1.19.5.tar.gz
tar -xzvf nginx-1.19.5.tar.gz
cd nginx-1.19.5
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
通过whereis nginx 命令找到nginx目录 usr/local/nginx
进入该目录,在conf该文件夹中找到nginx.conf配置文件,复制nginx.conf文件并命名为my_nginx.conf
打开my_nginx.conf并进行相应的修改
#user nobody;
worker_processes 1;
#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 django{
server 0.0.0.0:8002;#要与uwsgi.ini中的socket写的端口一致
}
server {
listen 8001;#监听端口,浏览器访问的就是这个端口
server_name 128.23.88.91;#服务器ip
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass django;
# root html;
# index index.html index.htm;
}
location /static/{
alias /usr/local/chenkworks/projetcs/msgsite/static/;
}
#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 / {
# '"$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 django{
server 0.0.0.0:8002;#要与uwsgi.ini中的socket写的端口一致
}
server {
listen 8001;#监听端口,浏览器访问的就是这个端口
server_name 128.23.88.91;#服务器ip
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass django;
# root html;
# index index.html index.htm;
}
location /static/{
alias /usr/local/chenkworks/projetcs/msgsite/static/;
}
#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;
# }
#}
}
至此,就配置好了。
启动uwsgi,nginx就可以了
nginx启动要进入usr/local/nginx/sbin中才能启动
uwsgi --ini uwsgi.ini
./nginx -c /usr/local/nginx/conf/my_nginx.conf # 使用-c命令来指定配置文件运行