nginx+uwsgi配置django项目
一、配置与安装nginx环境
为企业提供成都网站建设、成都网站设计、网站优化、成都全网营销、竞价托管、品牌运营等营销获客服务。成都创新互联拥有网络营销运营团队,以丰富的互联网营销经验助力企业精准获客,真正落地解决中小企业营销获客难题,做到“让获客更简单”。自创立至今,成功用技术实力解决了企业“网站建设、网络品牌塑造、网络营销”三大难题,同时降低了营销成本,提高了有效客户转化率,获得了众多企业客户的高度认可!
yum install nginx sqlite-devel sqlite zlib-devel openssl-devel MySQL-python unzip mariadb-server -y
cat /etc/nginx/conf.d/py.conf
server { listen 80; server_name 172.24.92.14; client_max_body_size 100M; location /static { alias /opt/BBS/static/; } location /media { alias /opt/BBS/media/; } location / { index index.html; include uwsgi_params; uwsgi_pass 127.0.0.1:9999; uwsgi_param UWSGI_SCRIPT BBS.wsgi; uwsgi_param UWSGI_CHDIR /opt/BBS; } }
重启生效
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/ --enable-optimization
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 &
访问页面:http://172.24.92.14:8001/
测试成功,关闭进程
pkill uwsgi
四、配置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 &
检查启动uwsgi程序
检查端口:
创建项目:(我这里已有写好的项目了,这里只是为了演示)
django-admin.py startproject BBS
修改访问权限:
vim /opt/BBS/BBS/settings.py
添加mysql支持
cat /opt/BBS/BBS/__init__.py
import pymysql pymysql.install_as_MySQLdb()
配置数据库(需要提前创建数据库,数据库名为,'bbs')
cat /opt/BBS/BBS/settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '127.0.0.1', 'PORT': 3306, 'USER': 'root', 'PASSWORD': '123456', 'NAME': 'bbs', } }
启动数据库
systemctl restart mariadb
systemctl enable mariadb
写入数据库表:
python3 manage.py makemigrations
python3 manage.py migrate
五、运行项目:
cd /opt/BBS/ python3 manage.py runserver 0.0.0.0:8000
六、访问测试
http://172.24.92.14/
网站栏目:nginx+uwsgi配置django项目
网页链接:http://ybzwz.com/article/pseiii.html