1、配置與安裝nginx環境html
yum install nginx sqlite-devel sqlite zlib-devel openssl-devel MySQL-python unzip mariadb-server -y
python
cat /etc/nginx/conf.d/py.confmysql
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; } }
重啓生效
nginx
systemctl restart nginxsql
2、配置Python3.6.8
數據庫
解壓python文件django
tar -xvf Python-3.6.8.tar.xzvim
cd /opt/Python-3.6.8app
./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
3、安裝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
4、配置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
5、運行項目:
cd /opt/BBS/ python3 manage.py runserver 0.0.0.0:8000
6、訪問測試
http://172.24.92.14/