ubuntu14装django2.2,uwsgi,nginx

Ddcc 2018年8月3日 19:50 1021916684@qq.com
django2.2 uwsgi nginx

如需转载请注明出处:http://zczzxz.top,整理不易请谅解。

1、 升级python3.5

sudo apt-get install python3.5
sudo cp /usr/bin/python /usr/bin/python_bak 先备份原来的链接
sudo rm /usr/bin/python 删除原来默认指向python2.7版本的链接
sudo ln -s /usr/bin/python3.5 /usr/bin/python 重新指定新的链接给python3.5版本

ls –la 查看软链接命令
2、 PiP升级之后报错:
报错内容:
suanfa@suanfa:/usr/bin$ pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

修改pip文件:suanfa@suanfa:/usr/bin$ sudo vim pip
修改前:
from pip import main
if __name__ == '__main__':
    sys.exit(main()) 

修改后:
from pip import __main__  //这行也要修改
if __name__ == '__main__':
sys.exit(__main__._main())//增加__main__._

成功之后:
suanfa@suanfa:/usr/bin$ pip --version
pip 10.0.1 from /home/suanfa/.local/lib/python3.5/site-packages/pip (python 3.5)

3、 安装virtualenv
apt-get install python-virtualenv
suanfa@suanfa:~/django$ virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate  #开启虚拟环境
pip3 install django==2.0  #安装django

这里不详细讲解,最后保证可以开启服务
在开启虚拟环境的情况下cd到项目目录下,输入:
python manage.py runserver 0.0.0.0:8000
将开启django。
4、 正式环境注意
切换到正式环境后,后台的静态文件丢失。
解决办法:
修改settings.py
STATIC_URL = '/static/'
STATIC_ROOT= os.path.join(BASE_DIR, 'staticfiles')     #所有的静态文件将拷贝到这里
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'myfastreasch/static'),  #从这里拷贝,开发环境下的静态文件目录
)
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'tempates'),
)

Cd到项目目录,输入命令python manage.py collectstatic  #迁移命令
Static目录下的文件将全部拷贝到staticfiles下
5、 安装nginx和uwsgi
 
安装:sudo pip install uwsgi
测试安装是不是成功:新建文件test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"  #py2
return [Hello World]  #py3

运行:sudo uwsgi --http 0.0.0.0:8000 --wsgi-file test.py
成功出现hello world
如果端口占用:sudo lsof -i:80
通过应用杀掉进程:sudo pkill –f nginx
通过pid杀掉进程:sudo kill -9 pid
关闭uwsgi命令:sudo pkill -f -9 uwsgi

安装:sudo apt-get install nginx
开启:nginx start
浏览器输入:127.0.0.1  安装成功时将会出现欢迎界面
查看配置文件目录:nginx -t

至此,uwsgi和nginx安装完毕
接下来,配置uwsgi和nginx
文件目录如下:
    
Uwsgi.ini文件的内容
[uwsgi]
socket = 0.0.0.0:8000
chdir=/home/suanfa/workplace/fastreasch
wsgi-file = /home/suanfa/workplace/fastreasch/fastreasch/wsgi.py
module= fastreasch.wsgi:application
master = true 
vhost = true
pythonpath = /home/suanfa/django/py3env/lib/python3.5/site-packages
workers=2 
vacuum=true 
thunder-lock=true 
enable-threads=true 
harakiri=30 
post-buffering=10240 
daemonize =/home/suanfa/workplace/fastreasch/uwsgi.log


nginx.conf文件的内容如下
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 768;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;
 
   server {
      listen 80;
      server_name 127.0.0.1;
      charset     utf-8;
      access_log      /home/suanfa/workplace/fastreasch/nginx_access.log;
      error_log       /home/suanfa/workplace/fastreasch/nginx_error.log;
      client_max_body_size 75M;
 
      location /static {
          alias /home/suanfa/workplace/fastreasch/staticfiles;
      }
 
      location / {
          include     /etc/nginx/uwsgi_params;
          uwsgi_pass  0.0.0.0:8000;
      }
  }
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /home/suanfa/workplace/fastreasch/nginx_access.log;
error_log /home/suanfa/workplace/fastreasch/nginx_error.log;
gzip on;
gzip_disable "msie6";
}

开启:

sudo uwsgi --ini uwsgi.ini    #开启uwsgi
sudo nginx -c  /home/suanfa/workplace/fastreasch/nginx.conf      #开启nginx

关闭:
sudo service nginx stop  或者 sudo pkill -f -9 nginx
sudo pkill -f -9 uwsgi