nginx
一、先安装python 版本管理器
```
# yum -y install virtualenv
# yum -y install python-virtualenv
```
默认安装的是python2.7
```
# cd /data
默认安装的是python2.7
# virtualenv uwsgi-tutorial
# ls -l uwsgi-tutorial/bin/
total 52
-rw-r--r-- 1 root root 2079 Apr 21 23:35 activate
-rw-r--r-- 1 root root 1021 Apr 21 23:35 activate.csh
-rw-r--r-- 1 root root 2175 Apr 21 23:35 activate.fish
-rw-r--r-- 1 root root 1137 Apr 21 23:35 activate_this.py
-rwxr-xr-x 1 root root 249 Apr 21 23:35 easy_install
-rwxr-xr-x 1 root root 249 Apr 21 23:35 easy_install-2.7
-rwxr-xr-x 1 root root 221 Apr 21 23:35 pip
-rwxr-xr-x 1 root root 221 Apr 21 23:35 pip2
-rwxr-xr-x 1 root root 221 Apr 21 23:35 pip2.7
-rwxr-xr-x 1 root root 7136 Apr 21 23:35 python
-rwxr-xr-x 1 root root 2338 Apr 21 23:35 python-config
lrwxrwxrwx 1 root root 6 Apr 21 23:35 python2 -> python
lrwxrwxrwx 1 root root 6 Apr 21 23:35 python2.7 -> python
-rwxr-xr-x 1 root root 228 Apr 21 23:35 wheel
```
安装python3.6,需依赖系统本身的python
```
# yum -y install python36
# virtualenv uwsgi-tutorial-python3.6 -p python3.6
```
使用 uwsgi-tutorial 的python 环境
```
# source /data/uwsgi-tutorial/bin/activate
(uwsgi-tutorial) [root@centos7]#
```
切换到python3.6环境下
```
# source /data/uwsgi-tutorial-python3.6/bin/activate
# python
```
二、安装django
查看python 和pip的位置
```
# which python
# which pip
```
更新pip
```
# pip install --upgrade pip
```
制定Django版本下载
```
# pip install Django==2.0.1
```
查看django版本
```
(uwsgi-tutorial) [root@centos7 data]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 11, 0, u'final', 1)
>>> django.get_version()
'1.11'
```
创建django project例如:
```
# django-admin.py startproject mysite
# cd mysite
```
三、Django配置
创建一个django project
```
cd /data
django-admin.py startproject mysite
ls
```
新建app
```
cd mysite
python manage.py startapp app01
```
生成数据库文件并创建更新数据库(默认使用sqlite3)
```
python manage.py makemigrations
python manage.py migrate
```
```
用于测试
```
python manage.py createsuperuser
```
监听所有可用 ip (电脑可能有一个或多个内网ip,一个或多个外网ip,即有多个ip地址)
```
python manage.py runserver 0.0.0.0:8000
```
```
vi mysite/settings.py
```
添加 'app01',
```python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
]
```
定义视图函数
```python
vi views.py
```
视图
```python
from django.http import HttpResponse
def index(request):
return HttpResponse(u"Welcome to tianmm's first Django project!")
```
定义视图函数相关的URL(网址) (即规定 访问什么网址对应什么内容)
```python
(uwsgi-tutorial) [root@centos7 mysite]# vi mysite/urls.py
from django.conf.urls import url
from django.contrib import admin
from myapp1 import views
urlpatterns = [
url(r'', views.index),
url(r'^admin/', admin.site.urls),
]
```
浏览器访问:47.104.9.240:8000 访问浏览器出现: You may need to add u'47.104.9.240' to ALLOWED_HOSTS.
```python
vi mysite/settings.py
ALLOWED_HOSTS = ['47.104.9.240']
```
四、安装使用python webserver 之uwsgi和nginx
```
yum -y install gcc gcc-c++
yum -y install python36-devel
yum -y install nginx
pip install uwsgi
vi uwsgi.ini
```
uwsgi.ini配置
```
[uwsgi]
socket = 127.0.0.1:8001
chdir = /data/bms
module = bms_1.wsgi
processes = 1
threads = 2
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
```
nginx.conf配置
```
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 47.104.9.240;
root /var/www/daily;
access_log logs/host.access.log;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static {
alias /data/bms/static;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
```
urls需要增加的
```python
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
```
setting需要增加的
```python
STATIC_ROOT = '/data/bms/static'
```
启动程序
```
uwsgi --ini uwsgi.ini
nginx
```
查询命令
```
ps -le | grep uwsgi
ps -le | grep nginx
netstat -tunlp
```
静态文件的copy
```
python manage.py collectstatic
```