本文共 9914 字,大约阅读时间需要 33 分钟。
Django ???????? Web ????????? Python ????????????Django ?????????????????????????????????????????????????? Web ?????????????????? CentOS 7 ????????????????? Django ?????????? uWSGI ????????????????Nginx ?????? uWSGI???????????????????????????????
????????????????? CentOS 7 ??????????? root ??????? sudo ??????????????????????????????????????????????? Django?????????????????????????????????????????????????????????????????????????? uWSGI ?????????????????????????? HTTP ????????????????????? Python ?????????? uWSGI ???? Nginx??????????????????????????
??????????????????? Django ????????????????????????? Python ????? virtualenv??? virtualenvwrapper???? virtualenv ??????????????????? pip?Python ????????????????? pip????????? EPEL ???????????????????
sudo yum install epel-release
???? EPEL???????????????? pip?
sudo yum install python-pip
???????? pip?????????????????? virtualenv ? virtualenvwrapper?
sudo pip install virtualenv virtualenvwrapper
??????????????????? shell ????????? virtualenvwrapper ?????????????????????????????? Env ?????????????????????? WORKON_HOME ?????????????????????? shell ?????????????????????
??????????? shell ?????????????????
echo "export WORKON_HOME=~/Env" >> ~/.bashrcecho "source /usr/bin/virtualenvwrapper.sh" >> ~/.bashrc
???????? shell ???????????????????????
source ~/.bashrc
???????? home ????????? Env ???????????????
???????????????????????????????? Django?????????
???????? virtualenvwrapper ???????????????????????????????????????????????????????????
mkvirtualenv firstsite
???????????????? Python ? pip???????????????????????????????firstsite?user @ hostname??$????????????????????????????????????????????????????????????????????????????????
????????? Django ????????????????????????????????? sudo ?????? pip?
pip install django
?? Django ???????????????????????????
cd ~django-admin.py startproject firstsite
????????? firstsite ????????????????????????????????????????????????????
????????????????????????????
cd ~/firstsite
???????????????????? SQLite ???????????????????????????????????????????
./manage.py migrate
????????????????? db.sqlite 3 ???????????????????????????????
./manage.py createsuperuser
???????????????????????????????????????
?????????????????????
nano firstsite/settings.py
??????? Nginx ??????????????????????????????????? Nginx ???????????????????????????? Django ????????????????????????????????????????
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
?????????????????????????????????????????????
./manage.py collectstatic
?????????????????????????????????
./manage.py runserver 0.0.0.0:8080
????? 8080 ??????????????????????? IP ??????? 8080?
?????????????????????????????????????????????
????????????????????????????????????? Django?
cd ~mkvirtualenv secondsitepip install django
???????????????????????? Django ??????????????????????????????????
???????????????
django-admin.py startproject secondsitecd ~/secondsite
????????????????
./manage.py migrate./manage.py createsuperuser
???????
nano secondsite/settings.py
????????? [uwsgi] ??????????????????????????????????????????????????????????????????? project ??????????????????????????????? base ???????????????????????
[uwsgi] project = firstsite username = user base = /home/%(username)
?????????? uWSGI????????????????????? chdir ???????????????????? %(variable_name) ??????????????????????????????????
??????????????? Django ??????????????????????
deactivate
??????????? Django ?????????????????????????? workon ?????????
workon firstsite
???
workon secondsite
???????????????????
deactivate
??????? Django ??????????????? uWSGI?uWSGI ????????????????? WSGI ??????????????????????????????? Ubuntu 14.04 ??? uWSGI ? Nginx ????
???????????????????????? uWSGI????????? Django ???????????? uWSGI ???????????? Python ????????????????????????????? yum?
sudo yum install python-devel gcc
?????????????????????????????? uWSGI?
sudo pip install uwsgi
?????????????????????????????????????????????????????????????
uwsgi --http :8080 --home /home/user/Env/firstsite --chdir /home/user/firstsite -w firstsite.wsgi
???????? uWSGI ?????? ?/ Env ?????????????????????????????????????? wsgi.py ???????????????????????? 8080 ??? HTTP ???????????????????? IP ???????8080????????????/ admin ??????????? ??????????????????? CTRL-C?
?????? uWSGI ?????????????????????????????Emperor????? uWSGI????????????????????????????????
??????????????????????????????????????? / etc / uwsgi / sites ???????????????????????????
sudo mkdir -p /etc/uwsgi/sitescd /etc/uwsgi/sites
???????????????????????????????????????????uWSGI ????????????????????????????? .ini ???
???????????????????????????
sudo nano firstsite.ini
????????? [uwsgi] ??????????????????????????????????????????????????????????????????? project ??????????????????????????????? base ???????????????????????
[uwsgi] project = firstsite username = user base = /home/%(username)
?????????? uWSGI????????????????????? chdir ???????????????????? %(variable_name) ??????????????????????????????????
[uwsgi] project = firstsite username = user base = /home/%(username) chdir = %(base)/%(project) home = %(base)/Env/%(project) module = %(project).wsgi:application master = true processes = 5
?????????? uWSGI ????????????? uWSGI ?????????? HTTP ??????????????? Nginx ????????????????
???????????????????????????????????? Unix ????????????????????????????? HTTP????? uWSGI ? uwsgi ????????????????????????????Nginx ???? uwsgi ???????????????????
???????????????????????????????????????? Web ????????????????? / run / uwsgi ??????????????uWSGI ? Nginx ???????????? vacuum ?????????????????????
[uwsgi] project = firstsite username = user base = /home/%(username) chdir = %(base)/%(project) home = %(base)/Env/%(project) module = %(project).wsgi:application master = true processes = 5 uid = %(username) socket = /run/uwsgi/%(project).sock chown-socket = %(username):nginx chmod-socket = 660 vacuum = true
?????????????? uWSGI ???????????????
??????????????????????????????????????????????????
sudo cp /etc/uwsgi/sites/firstsite.ini /etc/uwsgi/sites/secondsite.ini
??????????????????
sudo nano /etc/uwsgi/sites/secondsite.ini
???????????????????????????????????????????????????????
[uwsgi] project = firstsite username = user base = /home/%(username) chdir = %(base)/%(project) home = %(base)/Env/%(project) module = %(project).wsgi:application master = true processes = 5 uid = %(username) socket = /run/uwsgi/%(project).sock chown-socket = %(username):nginx chmod-socket = 660 vacuum = true
???????????????????????
???????????? Django ??????????????????????????????????? systemd ?????????????? uWSGI?
???????????????? / etc / systemd / system ???????????????????? uwsgi.service?
sudo nano /etc/systemd/system/uwsgi.service
?????????????????????????????????????
[Unit] Description=uWSGI Emperor service
????????? [Service] ???????? ExecStartPre ??????????????????????? / run / uwsgi ????????????????? Nginx ??????????????????? -p ??? mkdir ? chown ?????????????????
??? ExecStart ????????????????? uwsgi ??????????????Emperor????????????? / etc / uwsgi / sites ??????????????????????????? systemd ???????????? uWSGI ???
[Service] ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown user:nginx /run/uwsgi' ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites Restart=always KillSignal=SIGQUIT Type=notify NotifyAccess=all
????????????? [Install] ?????????????????????????????????????????????????????????????????????????
[Install] WantedBy=multi-user.target
????????????
??????????????????????? nginx ???????????????? nginx ?? uWSGI ???
???? uWSGI ????????????????? Nginx ?????????????? yum ??????
sudo yum install nginx
?? Nginx ???????????????????
http { include /etc/nginx/conf.d/*.conf; server { } server { } server { listen 80 default_server; server_name localhost; . . . }} ??????????? uWSGI ???????????????????????????
???????????????????????????????????????????
server { listen 80; server_name firstsite.com www.firstsite.com;} ????????? Nginx???????????????????????????????????????????????? Nginx ??????????????????
server { listen 80; server_name firstsite.com www.firstsite.com; location = favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/user/firstsite; }} ????????????????????????????? uWSGI?????? / etc / nginx / uwsgi_params ???? uwsgi ?????????? uWSGI ??????????
server { listen 80; server_name firstsite.com www.firstsite.com; location = favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/user/firstsite; } location / { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/firstsite.sock; }} ??????????????????
??????????????????????????????????????????????????????????????????????????????
server { listen 80; server_name secondsite.com www.secondsite.com; location = favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/user/secondsite; } location / { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/secondsite.sock; }} ???????????????
?? Nginx ????????????????
sudo nginx -t
???????????????????
??????????????????????????? Nginx ?????????????????????????????????????????????????????
???????? Nginx ????????????????????????????
sudo usermod -a -G user nginxchmod 710 /home/user
????????? Nginx ???? uWSGI ???
sudo systemctl start nginxsudo systemctl start uwsgi
???????????????????????????? Blond ?????????????
???????????????????????????
sudo systemctl enable nginxsudo systemctl enable uwsgi
转载地址:http://kqcfk.baihongyu.com/