ubuntu-18更新源
apt update
apt upgrade -y
# centos
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh
# ubuntu
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
# 安装成功结果
==================================================================
外网面板地址: http://114.57.256.213:8888/a6725a86
内网面板地址: http://172.31.156.189:8888/a6725a86
username: xxxxxxxx
password: xxxxxxxx
If you cannot access the panel,
release the following panel port [8888] in the security group
若无法访问面板,请检查防火墙/安全组是否有放行面板[8888]端口
==================================================================
阿里云官网->控制台->概览->实例->本实例安全组->查看8888方向安全组是否已开
安装nginx和mysql(5.7版本,默认不选择时安装的是5.6,我们要安装5.7)
等待20分钟左右安装完毕
①点击网站进入根目录
②上传后解压
③将来setting文件里的数据库配置信息,按照宝塔里的数据库信息配置
centos7: https://blog.csdn.net/weixin_43110554/article/details/106837002
ubuntu: sudo apt install python3-dev python3-pip python-pip
安装成功结果
[root@ubuntu-18:~]# python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
准备一个放置所有虚拟环境的文件夹
mkdir -p /pythonUser/env && cd /pythonUser/env
---------------------------------------------------------------------创建虚拟环境方法一---------------------------------------------------------------------
安装virtualenv
apt install virtualenv -y # ubuntu18
yum install virtualenv -y # centos7
查询python3所在位置
[root@pystar:~]$ whereis python3
python3: /python3.7.3/bin/python3.7m-config /python3.7.3/bin/python3.7-config /python3.7.3/bin/python3 /python3.7.3/bin/python3.7 /python3.7.3/bin/python3.7m
virtualenv -p djangoweb /python3.7.3/bin/python3 # djangoweb: 虚拟环境名
[root@ubuntu-18:~]$ whereis python3
python3: /usr/bin/python3.6-config /usr/bin/python3 /usr/bin/python3.6m-config /usr/bin/python3.6m /usr/bin/python3.6 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3 /usr/lib/python3.6 /etc/python3 /etc/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/include/python3.6 /usr/share/python3 /usr/share/man/man1/python3.1.gz
virtualenv djangoweb -p /usr/bin/python3 # djangoweb: 虚拟环境名
---------------------------------------------------------------------创建虚拟环境方法二---------------------------------------------------------------------
一定要是pip3,因为pip被linux自带的python2.7占用
pip3 install virtualenv
在当前目录下创建虚拟环境
virtualenv djangoweb --python=python3.7 # python3.7: 你系统里python的版本号
激活虚拟环境
source /pythonUser/env/djangoweb/bin/active
激活成功显示
[root@ubuntu-18:~]$ source /pythonUser/env/djangoweb/bin/active
(djangoweb) [root@ubuntu-18:~]$
安装python库
在原项目下输入 pip freeze >require.txt
将require.txt文件上传的服务器后
在require.txt文件所在目录下
pip install -r require.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip list # 检查所有的库是否都安装完毕
安装gunicorn(在当前虚拟环境下)
pip install gunicorn
配置gunicorn文件
vim /pythonUser/env/gunicorn_start.sh # 建议创建后wq,然后在宝塔里编辑文件,vim可能会导致缩进混乱
#!/bin/bash
NAME='sleepAnywhere' #应用的名称i
DJANGODIR=/www/wwwroot/www.sleepwhere.top/sleepAnywhere #django项目的目录
USER=root #运行此应用的用户
GROUP=root #运行此应用的组
NUM_WORKERS=1 #gunicorn使用的工作进程数,购买的是单核服务器
DJANGO_SETTINGS_MODULE=sleepAnywhere.settings #django的配置文件了; 路径: /www/wwwroot/www.sleepwhere.top/sleepAnywhere/sleepAnywhere.settings
DJANGO_WSGI_MODULE=sleepAnywhere.wsgi #wsgi模块; 路径: /www/wwwroot/www.sleepwhere.top/sleepAnywhere/sleepAnywhere.wsgi
echo "starting $NAME as `whoami`"
#激活python虚拟运行环境
cd $DJANGODIR
source /root/python/env/djangoweb/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
#如果gunicorn.sock所在目录不存在则创建
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
#启动Django
exec /root/python/env/djangoweb/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
在宝塔编辑完文件后回xshell
给文件加执行权限
chmod +x /pythonUser/env/gunicorn_start.sh
sh /pythonUser/env/gunicorn_start.sh
配置成功后结果
server
{
listen 80;
# 有域名写域名,没域名写ip地址
# ip地址
server_name xxx.xxx.xxx.xxx;
# 域名
# server_name shuinane.top;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/www.sleepwhere.top;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-00.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/www.sleepwhere.top.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
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_set_header X-NginX-Proxy true;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
# 禁止加载缓存
# location ~.*\.(js|css|html|png|jpg)$ {
# add_header Cache-Control no-cache;
# }
# 清除缓存
# location ~ /purge(/.*){
# proxy_cache_purge resource $host$1$is_args$args;
# }
# 静态文件路由配置
location /static/ {
# 你的静态文件路径
alias /www/wwwroot/www.sleepwhere.top/sleepAnywhere/static/;
}
access_log /www/wwwlogs/www.sleepwhere.top.log;
error_log /www/wwwlogs/www.sleepwhere.top.error.log;
}
写完之后点击编辑框左下角的保存
重启nginx
/etc/init.d/nginx restart
退出虚拟环境
deactivate
安装supervisor
pip install supervisor
创建并配置.conf文件
vim /etc/supervisor/conf.d/djangoweb.conf
[program:djangoweb]
# 要与你之前编写degunicorn_start.sh路径一致,写绝对路径
command=/pythonUser/env/gunicorn_start.sh
user=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/djangoweb.log
重载supervisor
supervisor update
supervisor reload
重启nginx
/etc/init.d/nginx restart
重启服务器
reboot
重启之后
访问成功