作者:chajian004_566_696 | 来源:互联网 | 2023-09-11 18:57
一、环境所需安装包与nginx配置yuminstallnginxsqlite-develsqlitezlib-devel openssl-develMySQL-python-ycatetcnginxconf.dpy.confs
一、环境所需安装包与nginx 配置
yum install nginx sqlite-devel sqlite zlib-devel openssl-devel MySQL-python -y
cat /etc/nginx/conf.d/py.conf
server {
listen 80;
server_name 192.168.1.160;
client_max_body_size 100M;
location / {
index index.html;
include uwsgi_params;
uwsgi_pass 127.0.0.1:9999;
uwsgi_param UWSGI_SCRIPT demosite.wsgi;
uwsgi_param UWSGI_CHDIR /opt/demosite;
}
}
重启生效
systemctl restart nginx
二、配置Python3.6.8
解压python文件
tar -xvf Python-3.6.8.tar.xz
cd /opt/Python-3.6.8
./configure --prefix=/opt/python-3-6-8/
make && make install
配置python 环境变量
vim /etc/profile
export PYTHON_HOME=/opt/python-3-6-8
export PATH=$PYTHON_HOME/bin:$PATH
source /etc/profile
三、安装Django框架和uwsgi
配置pip安装源
mkdir /root/.pip/
vim /root/.pip/pip.conf
[global]
timeout = 6000
index-url = http://mirrors.aliyun.com/pypi/simple
trusted-host = mirrors.aliyun.com
pip 安装所需要的安装包
cat uwsgi.txt
asn1crypto==0.24.0
beautifulsoup4==4.6.3
bs4==0.0.1
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
Click==7.0
cryptography==2.3.1
Django==1.11.9
Flask==1.0.2
Flask-Cors==3.0.6
gevent==1.3.6
greenlet==0.4.15
idna==2.7
ItsDangerous==1.1.0
Jinja2==2.10
lxml==4.2.6
MarkupSafe==1.0
numpy==1.15.3
Pillow==5.3.0
pycparser==2.18
PyMySQL==0.9.2
pytz==2018.7
requests==2.19.1
selenium==3.141.0
six==1.11.0
urllib3==1.23
virtualenv==16.1.0
Werkzeug==0.14.1
wordcloud==1.5.0
uwsgi
pip3 install -r uwsgi.txt
配置uwsgi 测试页
cat /opt/test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello Django"]
运行测试程序页
uwsgi --http :8001 --wsgi-file test.py &
四、配置uwsgi项目
cat uwsgi.ini
[uwsgi]
socket = 127.0.0.1:9999
master = true
workers = 2
max-requests = 1000
buffer-size = 30000
pidfile = /run/uwsgi.pid
daemonize = /var/log/uwsgi.log
启动uwsgi
uwsgi --ini uwsgi.ini &
修改访问权限:
cd /opt/demosite/demosite
vim settings.py
ALLOWED_HOSTS = ['*']
添加mysql支持
cat __init__.py
import pymysql
pymysql.install_as_MySQLdb()
配置数据库(需要提前创建数据库,数据库名为,'bbs')
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs',
'HOST':'127.0.0.1',
'USER':'root',
'PORT':3306,
'PASSWORD':'y',
}
}
五、创建与启动django项目
django-admin.py startproject demosite
cd demosite
python3 manage.py runserver 0.0.0.0:8002
测试:
在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。
最后整体测试:
http://192.168.1.160/