CentOS部署Python Django项目生产环境
【摘要】 记录搭配生产环境
Django 各版本以及所兼容的Python版本对照表:
Django 版本 | Python版本 |
1.4 | 2.5/2.6/2.7 |
1.7/1.8 |
2.7 and 3.2/3.3/3.4 |
1.9 | 2.7/3.4/35 |
1.11.11 | 3.6.8 |
一、安装python
1、下载python安装包
[qiaoyingxiao001@iot-master Python]$ wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
2、解压下载好的安装包
[qiaoyingxiao001@iot-master Python-3.6.8]$ tar -xvf Python-3.6.8.tgz
3、进入解压缩文件夹
[qiaoyingxiao001@iot-master Python]$ cd Python-3.6.8
4、安装python之前需要安装相关的依赖
[root@iot-master Python-3.6.8]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel python-devel mysql-devel gcc make
Last metadata expiration check: 2:21:03 ago on Sun 16 Aug 2020 06:44:36 AM UTC.
No match for argument: python-devel
Error: Unable to find a match: python-devel
[root@iot-master Python-3.6.8]#
The solution on Centos is to install python-devel package; In Centos 8 however this package was renamed due to the fact python 2.7 is no longer the default one, it has become 3. And this package was named w/o any suggestions;
yum install python3-devel
[root@iot-master Python-3.6.8]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel python3-devel mysql-devel gcc make
5、继续安装python3.6.8
[root@iot-master Python-3.6.8]# ./configure
[root@iot-master Python-3.6.8]# make
[root@iot-master Python-3.6.8]# make install
[root@iot-master bin]# ln -s /usr/local/bin/python3 python
[root@iot-master bin]# ln -s /usr/local/bin/pip3 pip
[root@iot-master bin]# ll pip
lrwxrwxrwx. 1 root root 19 Aug 16 09:25 pip -> /usr/local/bin/pip3
[root@iot-master Python]# pip install --upgrade pip
6.安装和使用virtualenv:
前提: python3和pip3都已经安装,执行命令:
pip3 install virtualenv
创建名字为django的虚拟环境,执行命令:
python3 -m venv /home/virtualenv/django
进入虚拟环境,执行命令:
cd /home/virtualenv/django/bin
source activate
7.安装第三方库:
安装Django第三方库列表(requirements.txt),如下:
cd /tmp
vim requirements.txt # 复制第三方库列表到此文件下,退出并保存:wq!
pip install -r requirements.txt # 安装第三方库列表
安装 setuptools
命令:
yum install python-setuptools
完成之后,就可以使用 easy_install 命令安装 django
easy_install django
8、安装MySQL数据库
[root@iot-master MySQL]# yum install mysql-server
[qiaoyingxiao001@iot-master MySQL]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[qiaoyingxiao001@iot-master MySQL]$ systemctl start mysqld.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to start 'mysqld.service'.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ====
[qiaoyingxiao001@iot-master MySQL]$
[qiaoyingxiao001@iot-master MySQL]$
[qiaoyingxiao001@iot-master MySQL]$ systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-08-16 09:40:27 UTC; 33s ago
Process: 161440 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 161313 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Process: 161289 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 161397 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 9312)
Memory: 498.4M
CGroup: /system.slice/mysqld.service
└─161397 /usr/libexec/mysqld --basedir=/usr
Aug 16 09:40:15 iot-master systemd[1]: Starting MySQL 8.0 database server...
Aug 16 09:40:16 iot-master mysql-prepare-db-dir[161313]: Initializing MySQL database
Aug 16 09:40:27 iot-master systemd[1]: Started MySQL 8.0 database server.
[qiaoyingxiao001@iot-master MySQL]$
[qiaoyingxiao001@iot-master MySQL]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql>
1)、修改root密码:
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '新密码';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2)、创建一个用户并授权:
mysql> create user 'managerdb'@'%' identified by '密码';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to 'managerdb'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
9、安装mysqlclient
CentOS 安装处理:
[qiaoyingxiao001@iot-master MySQL]$ pip install mysqlclient
如安装mysqlclient第三方库出现报错(“OSError: mysql_config not found”),修改配置文件并手动安装的解决方案:
cd /tmp
wget http://mirrors.163.com/pypi/packages/6f/86/bad31f1c1bb0cc99e88ca2adb7cb5c71f7a6540c1bb001480513de76a931/mysqlclient-1.3.12.tar.gz#sha256=2d9ec33de39f4d9c64ad7322ede0521d85829ce36a76f9dd3d6ab76a9c8648e5
tar -xzvf mysqlclient-1.3.12.tar.gz
cd mysqlclient-1.3.12
vim setup_posix.py
修改setup_posix.py下,mysql_config.path = "mysql_config" 改成 mysql_config.path = "/usr/local/mysql/bin/mysql_config",保存并退出:wq!
python setup.py install
Ubuntu 安装处理:
(env) tester@tester-master:~/MasterData/temp$ pip install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-2.0.1.tar.gz (87 kB)
ERROR: Command errored out with exit status 1:
command: /home/tester/bin/PycharmENV/env/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-xfgvb6rd/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-xfgvb6rd/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-2iefr5u4
cwd: /tmp/pip-install-xfgvb6rd/mysqlclient/
Complete output (12 lines):
/bin/sh: 1: mysql_config: not found
/bin/sh: 1: mariadb_config: not found
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-xfgvb6rd/mysqlclient/setup.py", line 15, in <module>
metadata, options = get_config()
File "/tmp/pip-install-xfgvb6rd/mysqlclient/setup_posix.py", line 65, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-xfgvb6rd/mysqlclient/setup_posix.py", line 31, in mysql_config
raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
解决方案:
(env) tester@tester-master:~/MasterData/temp$ sudo apt-get install python-dev default-libmysqlclient-dev
(env) tester@tester-master:~/MasterData/temp$ sudo apt-get install python3-dev
10、安装django1.11.11
[qiaoyingxiao001@iot-master MySQL]$ pip install django==1.11.11
11、测试创建django项目
修改settings.py文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'iot_db',
'USER': 'managerdb',
'PASSWORD': '密码',
}
}
12.Django项目使用uwsgi
安装uwsgi
pip install uwsgi
一、命令行启动
uwsgi --http 10.0.0.5:8080 --file xuanyuaniotpro/wsgi.py --static-map=/static=static
二、文件启动
在项目同级目录下面创建script目录存放项目相关的配置文件
在script目录下创建uwsgi.ini文件
[root-yuchuan@hy-iot script]$ vim uwsgi.ini
文件内容:
[uwsgi]
#项目目录
chdir=/home/root-yuchuan/project/xuanyuaniotpro
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 指定项目的application
module=xuanyuaniotpro.wsgi:application
# 指定sock的文件路径
socket=/home/root-yuchuan/project/script/uwsgi.sock
# 启用主进程
master=true
# 进程个数
workers=5
pidfile=/home/root-yuchuan/project/script/uwsgi.pid
# 自动移除unix Socket和pid文档当服务器停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/home/root-yuchuan/project/script/uwsgi.log
# 设置IP和端口
http=10.0.0.5:8080
# 设置静态文件
static-map=/static=/home/root-yuchuan/project/xuanyuaniotpro/static
使用文件启动的命令:
uwsgi --ini uwsgi.ini
12.Django项目使用Nginx
安装Nginx
[root@hy-iot conf.d]# yum -y install nginx
查看Nginx
[root@hy-iot conf.d]# ps -ef | grep -i nginx
[root@hy-iot conf.d]# cd /etc/nginx/conf.d/
[root@hy-iot conf.d]# vim xuanyuaniotpro.conf
server {# 这个server标识我要配置了
listen 80; # 我要监听那个端口
server_name 10.0.0.5; # 你访问路劲前面的url名称
access_log /var/log/nginx/access.log main; # Nginx 日志配置
charset utf-8; # Nginx 编码
gzip on; # 启用压缩,这个的作用是给用户一个网页,比如3M压缩后1M这样的传输速度就好很高的提高
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; #支持压缩的类型
error_page 404 /404.html; # 错误页面
error_page 500 502 503 504 /50x.html; # 错误页面
#指定项目路径uwsgi
location / { # 这个location就和咱们Django 的url(r'^admin/',admin.site.urls),
include uwsgi_params; # 导入一个Nginx模块他是用来和uwsgi继续通讯的
uwsgi_connect_timeout 30; # 设置连接uwsgi超时时间
uwsgi_pass unix:/home/root-yuchuan/project/script/uwsgi.sock; #指定uwsgi的sock文件所有动态请求就会直接丢给他
}
# 指定静态文件路径
location /static/ {
alias /home/root-yuchuan/project/xuanyuaniotpro/static/;
index login.html login.htm;
}
}
启动Nginx
[root@hy-iot conf.d]# /etc/init.d/nginx restart
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)