centos7下部署Django(nginx+uwsgi+python3+django)
centos7下部署Django(nginx+uwsgi+python3+django)
安装各类基础模块
yum install gcc-c++ yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel yum install libxml* yum -y install zlib* yum remove lrzsz -y yum install lrzsz -y 关闭防火墙 systemctl stop firewalld
- yum install gcc-c++
- yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
- yum install libxml*
- yum -y install zlib*
- yum remove lrzsz -y
- yum install lrzsz -y
- 关闭防火墙
- systemctl stop firewalld
yum install gcc-c++ yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel yum install libxml* yum -y install zlib* yum remove lrzsz -y yum install lrzsz -y 关闭防火墙 systemctl stop firewalld
———————————————————————————————-
编译安装python3
mkdir /application/ cd /application/ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar xf Python-3.6.3.tgz -C /usr/local/src/ cd /usr/local/src/Python-3.6.3/ ./configure --prefix=/usr/local/python3 make && make install 将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾 source /etc/profile 给python3安装django和uwsgi以及配置启动项目的xml文件 注:如果出现一片红,那就多执行几遍 pip3 install django pip3 install uwsgi 查看安装了哪些包 pip3 freeze or pip list 挂软链接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
- mkdir /application/
- cd /application/
- wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
- tar xf Python-3.6.3.tgz -C /usr/local/src/
- cd /usr/local/src/Python-3.6.3/
- ./configure --prefix=/usr/local/python3
- make && make install
- 将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾
- source /etc/profile
- 给python3安装django和uwsgi以及配置启动项目的xml文件
- 注:如果出现一片红,那就多执行几遍
- pip3 install django
- pip3 install uwsgi
- 查看安装了哪些包
- pip3 freeze or pip list
- 挂软链接
- ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
mkdir /application/ cd /application/ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar xf Python-3.6.3.tgz -C /usr/local/src/ cd /usr/local/src/Python-3.6.3/ ./configure --prefix=/usr/local/python3 make && make install 将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾 source /etc/profile 给python3安装django和uwsgi以及配置启动项目的xml文件 注:如果出现一片红,那就多执行几遍 pip3 install django pip3 install uwsgi 查看安装了哪些包 pip3 freeze or pip list 挂软链接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
————————————————————————————————-
uwsgi配置
上传项目目录 cd /application/ mkdir 项目 cd 项目,上传项目 runserver测试 python3 项目目录/manage.py runserver 10.0.0.8:80 mkdir script cd script vim uwsgi.ini [uwsgi] # 项目目录 chdir=/application/opt/yb # 启动uwsgi的用户名和用户组 uid=root gid=root # 指定项目的application module=yb.wsgi:application # 指定sock的文件路径 socket=/application/opt/script/uwsgi.sock # 启用主进程 master=true # 进程个数 workers=5 pidfile=/application/opt/script/uwsgi.pid # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/application/opt/script/uwsgi.log ######################################## 启动uwsgi uwsgi --ini uwsgi.ini 关闭uwsgi uwsgi --stop uwsgi.pid 查看uwsgi日志 tail -f /application/opt/script/uwsgi.log
- 上传项目目录
- cd /application/
- mkdir 项目
- cd 项目,上传项目
- runserver测试
- python3 项目目录/manage.py runserver 10.0.0.8:80
- mkdir script
- cd script
- vim uwsgi.ini
- [uwsgi]
- # 项目目录
- chdir=/application/opt/yb
- # 启动uwsgi的用户名和用户组
- uid=root
- gid=root
- # 指定项目的application
- module=yb.wsgi:application
- # 指定sock的文件路径
- socket=/application/opt/script/uwsgi.sock
- # 启用主进程
- master=true
- # 进程个数
- workers=5
- pidfile=/application/opt/script/uwsgi.pid
- # 自动移除unix Socket和pid文件当服务停止的时候
- vacuum=true
- # 序列化接受的内容,如果可能的话
- thunder-lock=true
- # 启用线程
- enable-threads=true
- # 设置自中断时间
- harakiri=30
- # 设置缓冲
- post-buffering=4096
- # 设置日志目录
- daemonize=/application/opt/script/uwsgi.log
- ########################################
- 启动uwsgi
- uwsgi --ini uwsgi.ini
- 关闭uwsgi
- uwsgi --stop uwsgi.pid
- 查看uwsgi日志
- tail -f /application/opt/script/uwsgi.log
上传项目目录 cd /application/ mkdir 项目 cd 项目,上传项目 runserver测试 python3 项目目录/manage.py runserver 10.0.0.8:80 mkdir script cd script vim uwsgi.ini [uwsgi] # 项目目录 chdir=/application/opt/yb # 启动uwsgi的用户名和用户组 uid=root gid=root # 指定项目的application module=yb.wsgi:application # 指定sock的文件路径 socket=/application/opt/script/uwsgi.sock # 启用主进程 master=true # 进程个数 workers=5 pidfile=/application/opt/script/uwsgi.pid # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/application/opt/script/uwsgi.log ######################################## 启动uwsgi uwsgi --ini uwsgi.ini 关闭uwsgi uwsgi --stop uwsgi.pid 查看uwsgi日志 tail -f /application/opt/script/uwsgi.log
安装nginx
yum install nginx /usr/sbin/nginx ps -ef |grep nginx
- yum install nginx
- /usr/sbin/nginx
- ps -ef |grep nginx
yum install nginx /usr/sbin/nginx ps -ef |grep nginx
nginx配置
>/etc/nginx/nginx.conf mkdir /etc/nginx/logs touch /etc/nginx/logs/django.log touch /etc/nginx/logs/errordjango.log #nginx配置 worker_processes 8; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #django配置 server { listen 80; server_name dh.gcav.me; access_log /etc/nginx/logs/django.log; error_log /etc/nginx/logs/errordjango.log; charset utf-8; gzip on; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定项目路径uwsgi location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/application/opt/script/uwsgi.sock; } # 指定静态文件路径 location /static/{ alias /application/opt/yb/static/; index index.html index.htm; } } #php配置 # server { # listen 80; # server_name gcav.me; # return 301 http://www.gcav.me$request_uri; # index index.php index.html index.htm; # } # # server { # listen 80; # server_name www.gcav.me; # access_log /var/log/nginx/video.log; # error_log /var/log/nginx/videoerror.log; # root /usr/share/nginx/html/video; # index index.php index.html index.htm; # location ~ .*\.(php|php5)?$ { # root /usr/share/nginx/html/video; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; # } # } # }
- >/etc/nginx/nginx.conf
- mkdir /etc/nginx/logs
- touch /etc/nginx/logs/django.log
- touch /etc/nginx/logs/errordjango.log
- #nginx配置
- worker_processes 8;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- #django配置
- server {
- listen 80;
- server_name dh.gcav.me;
- access_log /etc/nginx/logs/django.log;
- error_log /etc/nginx/logs/errordjango.log;
- charset utf-8;
- gzip on;
- error_page 404 /404.html;
- error_page 500502503504 /50x.html;
- # 指定项目路径uwsgi
- location / {
- include uwsgi_params;
- uwsgi_connect_timeout 30;
- uwsgi_pass unix:/application/opt/script/uwsgi.sock;
- }
- # 指定静态文件路径
- location /static/{
- alias /application/opt/yb/static/;
- index index.html index.htm;
- }
- }
- #php配置
- # server {
- # listen 80;
- # server_name gcav.me;
- # return 301 http://www.gcav.me$request_uri;
- # index index.php index.html index.htm;
- # }
- #
- # server {
- # listen 80;
- # server_name www.gcav.me;
- # access_log /var/log/nginx/video.log;
- # error_log /var/log/nginx/videoerror.log;
- # root /usr/share/nginx/html/video;
- # index index.php index.html index.htm;
- # location ~ .*\.(php|php5)?$ {
- # root /usr/share/nginx/html/video;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # include fastcgi.conf;
- # }
- # }
- #
- }
>/etc/nginx/nginx.conf mkdir /etc/nginx/logs touch /etc/nginx/logs/django.log touch /etc/nginx/logs/errordjango.log #nginx配置 worker_processes 8; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #django配置 server { listen 80; server_name dh.gcav.me; access_log /etc/nginx/logs/django.log; error_log /etc/nginx/logs/errordjango.log; charset utf-8; gzip on; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定项目路径uwsgi location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/application/opt/script/uwsgi.sock; } # 指定静态文件路径 location /static/{ alias /application/opt/yb/static/; index index.html index.htm; } } #php配置 # server { # listen 80; # server_name gcav.me; # return 301 http://www.gcav.me$request_uri; # index index.php index.html index.htm; # } # # server { # listen 80; # server_name www.gcav.me; # access_log /var/log/nginx/video.log; # error_log /var/log/nginx/videoerror.log; # root /usr/share/nginx/html/video; # index index.php index.html index.htm; # location ~ .*\.(php|php5)?$ { # root /usr/share/nginx/html/video; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; # } # } # }
修改静态文件路径
1.将项目的settings.py文件底部添加 STATIC_ROOT = os.path.join(BASE_DIR, "static_all") 2.执行命令生效 python3 manage.py collectstatic 3.修改nginx.conf静态文件路径 # 指定静态文件路径 location /static/{ alias /application/opt/yb/static_all/; index index.html index.htm; } 4.重启服务
- 1.将项目的settings.py文件底部添加
- STATIC_ROOT = os.path.join(BASE_DIR, "static_all")
- 2.执行命令生效
- python3 manage.py collectstatic
- 3.修改nginx.conf静态文件路径
- # 指定静态文件路径
- location /static/{
- alias /application/opt/yb/static_all/;
- index index.html index.htm;
- }
- 4.重启服务
1.将项目的settings.py文件底部添加 STATIC_ROOT = os.path.join(BASE_DIR, "static_all") 2.执行命令生效 python3 manage.py collectstatic 3.修改nginx.conf静态文件路径 # 指定静态文件路径 location /static/{ alias /application/opt/yb/static_all/; index index.html index.htm; } 4.重启服务
启动nginx
/usr/sbin/nginx -t /usr/sbin/nginx
- /usr/sbin/nginx -t
- /usr/sbin/nginx
/usr/sbin/nginx -t /usr/sbin/nginx
查看nginx日志
tail -f /etc/nginx/logs/django.log
- tail -f /etc/nginx/logs/django.log
tail -f /etc/nginx/logs/django.log
关闭uwsgi和nginx
uwsgi --stop uwsgi.pid /usr/sbin/nginx -s stop
- uwsgi --stop uwsgi.pid
- /usr/sbin/nginx -s stop
uwsgi --stop uwsgi.pid /usr/sbin/nginx -s stop
创建admin
cd 到项目目录下创建admin python3 manage.py migrate 创建admin超级用户 python3 manage.py createsuperuser python3 manage.py collectstatic --noinput
- cd 到项目目录下创建admin
- python3 manage.py migrate
- 创建admin超级用户
- python3 manage.py createsuperuser
- python3 manage.py collectstatic --noinput
cd 到项目目录下创建admin python3 manage.py migrate 创建admin超级用户 python3 manage.py createsuperuser python3 manage.py collectstatic --noinput
连接数据库
编辑app目录下__init__.py文件 import pymysql pymysql.install_as_MySQLdb() 修改settings.py文件中的DATABASES django.db.backends.postgresql 连接 PostgreSQL django.db.backends.mysql 连接 mysql django.db.backends.sqlite3 连接 sqlite django.db.backends.oracle 连接 oracle 若是要连接mysql之类的,需要账户密码的,连接配置应该这样写: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '库名', 'USER': '用户名', 'PASSWORD': '密码', 'HOST': '127.0.0.1', 'PORT': '3306', } } 第一个命令:创建迁移文件 python3 manage.py makemigrations 第二个命令:同步到数据库 python3 manage.py migrate
- 编辑app目录下__init__.py文件
- import pymysql
- pymysql.install_as_MySQLdb()
- 修改settings.py文件中的DATABASES
- django.db.backends.postgresql 连接 PostgreSQL
- django.db.backends.mysql 连接 mysql
- django.db.backends.sqlite3 连接 sqlite
- django.db.backends.oracle 连接 oracle
- 若是要连接mysql之类的,需要账户密码的,连接配置应该这样写:
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'NAME': '库名',
- 'USER': '用户名',
- 'PASSWORD': '密码',
- 'HOST': '127.0.0.1',
- 'PORT': '3306',
- }
- }
- 第一个命令:创建迁移文件
- python3 manage.py makemigrations
- 第二个命令:同步到数据库
- python3 manage.py migrate
编辑app目录下__init__.py文件 import pymysql pymysql.install_as_MySQLdb() 修改settings.py文件中的DATABASES django.db.backends.postgresql 连接 PostgreSQL django.db.backends.mysql 连接 mysql django.db.backends.sqlite3 连接 sqlite django.db.backends.oracle 连接 oracle 若是要连接mysql之类的,需要账户密码的,连接配置应该这样写: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '库名', 'USER': '用户名', 'PASSWORD': '密码', 'HOST': '127.0.0.1', 'PORT': '3306', } } 第一个命令:创建迁移文件 python3 manage.py makemigrations 第二个命令:同步到数据库 python3 manage.py migrate
更改数据库编码为utf8
mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行: character_set_server=utf8 然后保存退出, 并重启mysql服务即可。
- mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:
- character_set_server=utf8
- 然后保存退出, 并重启mysql服务即可。
mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行: character_set_server=utf8 然后保存退出, 并重启mysql服务即可。