发布时间:2019-09-24 08:20:11编辑:auto阅读(1620)
1 环境介绍和准备
Python3+django+nginx+uwsgi使用场景部署
当我们写完一套完整的网站功能,我们需要实时在生产环境上线,并需要容纳高并发的访问请求。所以我们需要部署以上来保证网站的服务质量。
1.1基本环境准备:
echo 'LANG="en_GB.utf8"' > /etc/sysconfig/i18n
LANG="en_GB.utf8"
1.2依赖环境:
(env) [root@reboot5 nginx]# python -V
Python 3.6.2
(env) [root@reboot5 nginx]# cat /etc/redhat-release
CentOS release 6.8 (Final)
(env) [root@reboot5 nginx]# pip list|grep Django
Django 1.11.4
(env) [root@reboot5 opsweb]# mysql -V
mysql Ver 14.14 Distrib 5.6.37
1.3修改yum源地址:
yum install wget
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
1.4 环境安装:
yum install wget openssl-devel -y # gcc编译要用
yum install gcc zlib-devel openssl-devel -y
2 安装python
yum -y install openssl-devel readline-devel unzip
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -xzf Python-3.6.2.tgz
cd Python-3.6.2
sudo ./configure --prefix=/usr/local/python36
sudo make
sudo make install
2.1配置pip源
sudo tee /etc/pip.conf <<-'EOF'
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns
EOF
2.2安装virtualenv虚拟环境
sudo /usr/local/python36/bin/pip3 install virtualenv
4 mysql环境准备:
sudo rpm -ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
sudo yum install -y mysql mysql-server mysql-devel zlib-devel
source /home/djangoenv/pythonenv/bin/activate
#python依赖:
pip install pymysql
[mysqld]
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
sudo service mysqld start
4.1 安全设置:
mysql_upgrade -uroot # 如果是升级需要执行(卸载原有mysql低版本也需要)
mysql_secure_installation # 命令行执行设置账号密码、限制远程连接等
4.2 创建基础数据库:
create database cmdb CHARACTER SET utf8; # 创建一个cmdb库
CREATE USER 'cmdb'@'%' IDENTIFIED BY 'nice'; # 创建一个cmdb用户,密码cmdb
GRANT ALL ON cmdb.* TO 'cmdb'@'%'; # 给cmdb用户所有cmdb库权限
4.3 mysql基本备份还原
导出:mysqldump -h127.0.0.1 -uroot -p123456 django > /backupfile.sql
还原:mysql -h127.0.0.1 -uroot -pruanchunxia2324 django < backupfile.sql
-----------------------------------uwsgi------------------------------------------------------
5 安装:uwsgi
source /root/reboot/env/bin/activate
pip install uwsgi --no-cache-dir
5.1 uwsgi安装完毕查询版本
(env) [root@reboot5 /]# pip list|grep uWSGI
uWSGI 2.0.15
5.2 将配置文件写到项目目录下
(env) [root@reboot5 nginx]# cd /root/reboot/opsweb
(env) [root@reboot5 opsweb]# vim uwsgi.ini
[uwsgi]
chdir = /root/reboot/opsweb
module=opsweb.wsgi
workers=2
pidfile=/var/log/uwsgi/uwsgi.pid
http=10.10.40.112:8001
static=/root/reboot/opsweb/static
uid=root
gid=root
master=true
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize=/var/log/uwsgi/opsweb.log
socket=/var/log/uwsgi/uwsgi.sock
5.3 uwsgi启动命令:
sudo uwsgi --ini uwsgi.ini
uwsgi --reload /var/log/uwsgi/uwsgi.pid
uwsgi --start /var/log/uwsgi/uwsgi.pid
uwsgi --stop /var/log/uwsgi/uwsgi.pid
-----------------------------------nginx------------------------------------------------------
6 安装nginx:
yum install nginx -y
server {
listen 80;
server_name 10.10.40.112;
location /static {
alias /root/reboot/opsweb/static;
}
location / {
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass unix:/var/log/uwsgi/uwsgi.sock;
}
}
7 django配置
7.1 django配置修改:
django可以在自己的templates下面放静态资源 那个命令可以吧所有静态资源收集起来
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#然后到项目目录下执行:
python manage.py collectstatic
DEBUG = False ALLOWED_HOSTS = ['*']
此处需要对nginx配置文件重新加载
/etc/init.d/nginx reload
8.4 最终代理效果
注:没做代理前如访问时,IP后面需要跟端口,代理成功后只需输入代理的ip即可访问后端网站,从而提高安全性和并发访问量。
上一篇: 1)python的基础2和3的区别,以及
下一篇: Python练习3:判断学生成绩等级
47776
46290
37179
34669
29262
25920
24808
19896
19456
17952
5747°
6352°
5865°
5917°
7018°
5854°
5878°
6384°
6343°
7712°