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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
服务器部署Python项目通常有以下步骤: 1. 准备服务器: 首先,您需要选择一台服务器,并确保已经安装了操作系统(如Linux)和所需的软件,例如Python解释器、数据库等。 2. 上传代码: 将您的Python项目代码上传到服务器。您可以使用FTP、SCP或其他文件传输协议将代码从本地计算机复制到服务器上。 3. 创建虚拟环境: 为了隔离不同项目的依赖关系,建议在服务器上为您的项目创建一个虚拟环境。虚拟环境可以使用Python的venv模块创建,或者使用第三方工具如virtualenv。 4. 安装依赖: 在虚拟环境中,使用pip安装项目所需的Python包和依赖项。可以在项目的requirements.txt文件中列出这些依赖项,并运行`pip install -r requirements.txt`命令来安装它们。 5. 配置服务器: 根据您的项目需求,配置服务器以支持您的应用程序。例如,设置环境变量、配置数据库连接等。 6. 运行应用程序: 使用命令行或脚本运行您的Python应用程序。您可以使用gunicorn、uWSGI等服务器软件来运行Python应用程序,并设置相应的配置选项。 7. 设置反向代理: 使用Nginx或Apache等Web服务器作为反向代理,将请求转发到运行在服务器上的Python应用程序。 8. 配置域名和HTTPS: 如果您打算使用自定义域名和HTTPS,您需要设置DNS解析和SSL证书等。 这些是一般的步骤,具体的部署过程可能因项目需求和服务器环境而有所不同。建议在部署前阅读相关文档,并根据项目的特定要求进行配置和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值