一、Django
1.拉取代码
git clone https://gitee.com/XXX/XXX.git
2.根据requirement.txt安装依赖
pip install -r requirements.txt
注:requirements.txt 由如下指令生成
pip freeze > requirements.txt
3.删除源码中的迁移记录文件
cd project_name
删除 migrations\0001_initial.py
4.配置数据库mysql
4.1 创建数据库
终端进入数据库
mysql -u 用户名 -p
Enter password:******
然后创建
create database dsmis charset utf8;
4.2 django配置数据库
打开setting.py, 修改数据库配置(用户名和密码)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dsmis',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}
4.3. 数据库迁移库
删除 migrations\0001_initial.py 然后运行如下代码
python manage.py makemigrates
python manage.py migrate
二、Gunicorn
1.新建 gunicorn.conf.py
在项目根目录新建gunicorn.conf.py, 内容如下
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = "127.0.0.1:8000" #绑定的ip与端口
backlog = 512 #监听队列数量,64-2048
worker_class = 'sync' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
workers = 4 # multiprocessing.cpu_count() #进程数
threads = 16 #multiprocessing.cpu_count()*4 #指定每个进程开启的线程数
loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
accesslog = "-" #访问日志文件,"-" 表示标准输出
errorlog = "-" #错误日志文件,"-" 表示标准输出
proc_name = 'eashop' #进程名
2.启动 Gunicorn
gunicorn -c gunicorn.conf.py dsmis.wsgi:application
#dsmis:项目名 是有wsgi.py的目录层次:dsmis/wsgi.py
也可以用守护进程的方式运行 (一直在后台运行)
gunicorn -c gunicorn.conf.py -D dsmis.wsgi:application
3. 如果报错
gunicorn: command not found
原因:未配置环境变量
解决:
vim ~/.bashrc,在末尾加入:export PATH=$PATH:/usr/local/python3/bin
4. 查看
ps - ef | grep python
netstat -tpln | grep 端口号
三、nginx
1. nginx 安装
切换至root用户
sudo su root
apt-get install nginx
查看
nginx -v
启动
service nginx start
启动后,在网页重输入ip地址,即可看到nginx的欢迎页面。至此nginx安装成功
1.1 nginx的路径索引:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
2. nginx配置
进入nginx配置目录
cd /etc/nginx/
打开nginx.conf
vim nginx.conf
整个配置文件如下:
user root;
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;
server {
listen 80;
server_name 121.41.89.89;
#charset koi8-r;
#access_log logs/host.access.log main;
location /static {
root /root/Program/test-demo;
autoindex on;
}
location / {
proxy_pass http://127.0.0.1:8000;
autoindex on;
}
# }
#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;
# }
#}
}
修改其中
server {
listen 80;# 这是nginx监听的端口
server_name 47.92.152.185;# 这是服务器的ip
client_max_body_size 50m;
location /static {
alias /root/项目名/static;
}
location / {
proxy_set_header x-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8000;# 转发的地址,意思是监听到80端口就转发到127.0.0.1:8000端口(反向代理)
#include uwsgi_params;
#uwsgi_pass 127.0.0.1:8000;
#proxy_pass_header Server;
##### 支持websocket
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
配置完成后保存退出
在/usr/nginx/sbin 目录下执行(有的在/usr/local/nginx/sbin),重启nginx
./nginx -s reload
完成
可以通过公网访问:http://121.41.89.89/(示例)