python项目服务器部署文档
1、安装依赖库
sudo yum -y groupinstall "Development tools"
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2、安装python
1、将本地的python安装包通过fxtp上传到虚拟机
2、通过命令安装python
tar -xvJf Python-3.7.3.tar.xz #解压
cd Python-3.7.3
./configure --prefix=/usr/local/bin/python3
sudo make
sudo make install
3、建立软连接
ln -s /usr/local/bin/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/bin/python3/bin/pip3 /usr/bin/pip3
3、安装python虚拟环境
1、通过命令安装
pip install virtualenv
pip install virtualenvwrapper
pip3 install virtualenvwrapper
2、寻找virtualenvwrapper.sh路径
which virtualenvwrapper.sh
3、更改配置文件
使用 vim ~/.bashrc 命令打开文件,添加如下代码
export WORKON_HOME=/home/env
source /usr/bin/virtualenvwrapper.sh
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
4、运行测试
source ~/.bashrc
5、创建虚拟环境
mkvirtualenv -p python3 虚拟环境名称
workon 虚拟环境名称 #进入虚拟环境
deactivate #退出虚拟环境
4、安装mysql数据库
1、通过命令安装
yum -y install mariadb mariadb-server
2、启动服务
systemctl start mariadb
3、进入数据库
mysql -u root -p
4、创建项目数据库
create database Team default charset utf8 collate utf8_general_ci;
5、将本地项目数据库迁移
source air.sql
5、项目迁移
本地项目修改配置
#在上传之前需要将静态文件打包,在settings中设置
STATIC_ROOT = os.path.join(BASE_DIR, "static_new/")
ALLOWED_HOSTS = ["*"]
在终端输入python manage.py collectstatic 将静态文件打包
然后将项目通过fxtp上传到/home/src下
切换到manage.py下同步数据库
6、uwsgi部署
1、进入虚拟环境安装模块(根据自己项目所需要的进行安装)
pymysql django pillow …
2、使用uwsgi部署
uwsgi --chdir=/home/src/My_movie --home /home/env/my_movie/ --http :8005 --module My_movie.wsgi:application
3、查看效果
7、安装nginx
1、将安装包发送到虚拟机
2、通过命令进行安装
tar -zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
sudo ./configure # 以 root 身份运行
sudo make && make install # 以 root 身份运行
3、启动nginx
进入目录 cd /usr/local/nginx/sbin
./nginx #启动
./nginx -s stop # 快速停止
./nginx -s quit # 优雅停止
./nginx -s reload # 重新加载配置文件
./nginx -s reopen # 重启日志文件,备份日志文件时常用
./nginx -t # 测试 Nginx 配置文件
./nginx -v # 查看 Nginx 版本
./nginx -V # 查看 Nginx 版本、编译版本和编译参数
8、配置nginx
1、进入nginx目录
cd /usr/local/nginx修改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;
server {
listen 80;
server_name Team_3;
charset utf-8;
client_max_body_size 75M;
location / {
# nginx转发动态请求到uWSGI
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:8006;
}
# 如果写成/static/,nginx无法找到项目静态文件路径
location /static {
alias /home/src/Team_3/static_new;
}
# 如果写成/media/,nginx无法找到项目媒体文件路径
location /media {
alias /home/src/Team_3/media;
}
}
#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;
# }
#}
9、创建ini启动文件
切换到home目录下,创建xx_uwsgi文件夹,然后创建xx.ini文件,写入如下代码:
[uwsgi]
chdir=/home/src/My_movie
home=/home/env/my_pro
module=My_movie.wsgi:application
master=True
processes=4
harakiri=60
max-requests=5000
socket=127.0.0.1:8004·
uid=1000
gid=2000
pidfile=/home/my_movie_uwsgi/master.pid
daemonize=/home/my_movie_uwsgi/movie.log
vacuum=True
然后输入如下代码
uwsgi --ini /home/my_movie_uwsgi/my_movie.ini
pkill -f uwsgi -9
10、重启nginx
python项目服务器部署文档
在centos下启动nginx出现Failed to start nginx.service:unit not found
1、用命令在根目录下执行:# vim /etc/init.d/nginx (注意vim旁边有一个空格)
插入以下代码
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
2、用命令进入此目录
cd /etc/init.d
3、依此执行以下命令
chmod 755 /etc/init.d/nginx
chkconfig --add nginx (注意add前面是两个短横线-)
11、字体权限问题
将使用的验证码字体传输到centos中,然后修改权限
cd /usr/share/fonts/windows
chmod 755 *.ttf
12、文件上传权限问题
sudo chmod -R 777 /home/src/My_movie/