django+nginx+uwsgi项目部署文档整理

django+nginx+uwsgi项目部署文档整理

参考文章:https://blog.csdn.net/qq_42314550/article/details/81805328

一、python安装

1、首先,在根目录底下建一个目录,存放python3.6.3版本,linux自带的为python2版本,命令如下:

mkdir python3.6.3 

cd python3.6.3

2、接着,使用下面的命令下载Python-3.6.3.tar.xz的安装包,对应版本可以去 https://www.python.org/ftp/python/对应起来:

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz

3、接着解压这个压缩包,命令如下:

tar xvf Python-3.6.3.tar.xz

4、接着编译安装,进入解压后的目录,执行底下的一个shell脚本configure进行检查,接着编译安装,编译安装过程有点慢,命令如下:

cd Python-3.6.3

./configure --prefix=/usr/local/python3.6.3 --enable-shared CFLAGS=-fPIC

make

make install

 

5、但是当输入python –V时,发现python版本还是2.7,那是因为没有把python快捷链接的还是原来的python2.7版本,我们要改一下

(1)、先备份python, 这里插一句,备份后要删除,不然会报错:

mv /usr/bin/python /usr/bin/python2.7.bak

(2)、删除旧的python的软连接,创建新的python2和python3的软连接,命令如下:

cd /usr/bin   

rm -f python2

ln -s python2.7 python2

(3)、链接到python3.6.3

ln -s /usr/local/python3.6.3/bin/python3 /usr/bin/python

注:这里如果执行命令错误的话,会说不存在python这个目录,可以新建一个目录,后面再删掉,之后再重新执行命令即可,不过通常新的服务器不会出现这个问题

(4)、到这里还有一个问题,就是python3.6.3安装完成后,你会发现yum用不了了,这是yum用的是原来2.7的版本,而刚刚我们把python改为3.6.3了,所以用不了了,需要编辑yum文件:

vi /usr/bin/yum

把第一行的usr/bin/python改成/usr/bin/python2.7

Esc->:wq保存,就可以了。

vi /usr/libexec/urlgrabber-ext-down

把第一行的usr/bin/python改成/usr/bin/python2.7

Esc->:wq保存,就可以了。

(5)、如果输入pip命令不存在时,需设置软链接:

ln -s /usr/local/python3.6.3/bin/pip3 /usr/bin/pip

二、uwsgi安装

1、安装 pip install uwsgi

2、测试uwsgi

在你的同项目名目录中写一个test.py ,执行:vim test.py ,填写如下内容:

python2写法:

# test.py

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    return "Hello World"



python3写法:(我用的是这个)

# test.py

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    return [b"Hello World"]

3、然后执行shell命令,前提是服务器已经开放了这个端口,不过默认服务器都会开放80这个默认端口的: 

uwsgi --http :80 --wsgi-file test.py

4、访问网页:http://110.108.64.23:80/ 看在网页上是否有Hello World 
注意IP地址写你自己的主机地址,如在本地请写127.0.0.1

三、django安装:

1、安装命令:

pip install Django==2.0.3

2、查看django是否安装成功,并是否为所安装的版本,输入下面命令,没报错显示django版本即可:

python
import django
django.VERSION

四、部署项目,把项目代码放入服务器当中,这里以test项目为例:

1、进入test项目中:

cd /project/test

2、新建一个test_wsgi.py:vi test_wsgi.py,代码如下:

"""

WSGI config for test project.



It exposes the WSGI callable as a module-level variable named ``application``.



For more information on this file, see

https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/

"""



import os

# 线上配置Linux需要

import sys

from django.core.wsgi import get_wsgi_application



# 线上配置Linux需要

sys.path.append('/project/test') # 项目路径

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")



application = get_wsgi_application()

3、连接django和uwsgi,实现简单的web服务器,在test工作目录底下,执行命令:

uwsgi --http :80 --module test_wsgi

访问网页:http://110.108.64.23:80/

五、nginx安装,Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

1、安装编译nginx源代码,命令各自如下:

cd /usr/local/src

wget http://nginx.org/download/nginx-1.16.0.tar.gz

tar -zxvf nginx-1.16.0.tar.gz

cd nginx-1.16.0

./configure

make

make install

注:如果编译错误,可能缺少openssl包,使用命令yum install openssl openssl-devel即可

2、编译成功之后,便可以启动nginx:

(1)、查看nginx配置文件是否正常:/usr/local/nginx/sbin/nginx -t

(2)、启动nginx:/usr/local/nginx/sbin/nginx

(3)、重启nginx:/usr/local/nginx/sbin/nginx -s reload

(4)、启动成功后可以访问:http://110.108.64.23:80/ 看是否出现nginx访问成功的页面,如报错可查看/usr/local/nginx/logs/error.log命令

六、实现Nginx与uWSGI的连接,为了实现Nginx与uWSGI的连接,两者之间将采用soket来通讯方式。在本节中,我们将使用uWSGI配置文件的方式来改进uWSGI的启动方式。 假定你的项目目录是 /project/test我们将要让Nginx采用8000端口与uWSGI通讯,请确保此端口没有被其它程序采用。 注意,请确定你在上面步骤中 test_wsgi.py 文件已经存在了。

1、新建一个XML文件:test_django_socket.xml,其中配置了连接端口,项目目录,进程数,日志名,内容如下:

<!-- 若以socket方式访问 -->

<uwsgi>

    <socket>:8000</socket>

    <chdir>/project/test</chdir>

    <module>test_wsgi</module>

    <processes>4</processes> <!-- 进程数 -->

    <daemonize>test_uwsgi.log</daemonize>

</uwsgi>



<!-- 若以http方式访问 -->

<uwsgi>

    <http>:8000</http>

    <chdir>/project/test</chdir>

    <module>test_wsgi</module>

    <processes>4</processes> <!-- 进程数 -->

    <daemonize>test_uwsgi.log</daemonize>

</uwsgi>

注:如果后面使用命令uwsgi -x 解析这个xml,出现 parser error : Extra content at the end of the document 错误时,在首行加入<document>,末行加入</document>,即可!

2、配置nginx文件,使用命令vi /usr/local/nginx/conf/nginx.conf,其中只需要改其相关的部分即可,如果多个项目的话就增多一个server节点即可,其中把static存放静态资源文件目录,media存放上传下载文件目录,templates存放模板目录都配置了:

server {

        listen       80;

        #server_name  localhost;

        server_name 110.108.64.23;



        #charset koi8-r;



        #access_log  logs/host.access.log  main;



        location / {

            include uwsgi_params;

            uwsgi_pass 110.108.64.23:8000;

            uwsgi_param UWSGI_CHDIR /project/test;

            uwsgi_param UWSGI_SCRIPT /project/test;

            uwsgi_param UWSGI_MODULE /project/test.test_wsgi:application;

            uwsgi_read_timeout 1800;

            uwsgi_send_timeout 300;

            proxy_read_timeout 300;

            root   html;

            index  index.html index.htm;

        }



        location /static/ {
            # 所有静态资源文件都放在collected_static文件目录底下了
            # 用命令python manage.py collectstatic 即可,故下面一行注释
            # alias /project/test/static/;

            alias /project/test/collected_static/;

            index index.html index.htm;

        }





        location /media/ {

            alias /project/test/media/;

        }



        location /templates/ {

            alias /project/test/templates/;

        }



        #error_page  404              /404.html;



        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }



        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}



        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}



        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

3、配置完成后,需启动uwsgi服务器:

cd /project/test

uwsgi -x test_django_socket.xml

然后查看底下配置的test_uwsgi.log有没有报错信息,比如缺少库呀,如果没错就证明正常启动了。

比如缺少PyMySQL的库,就使用pip install PyMySQL==0.8.0

4、貌似好像成功了,但是其实不然,不能访问。

原来是这个端口8000没开放,如果部署多个项目的话,nginx的端口还有uwsgi的端口都需要在阿里云服务器底下添加并开放。

如果端口开放了还不行,那就是防火墙限制了,需在iptables底下加上一条:

vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT

注:这里默认的nginx的80端口也要添加到防火墙底下去,不然也会访问不到:

vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启防火墙:

service iptables restart

5、其中还需要改一个settings.py文件,不然会报Invalid HTTP_HOST header:错误,这个settings.py数据库连接也需要改成线上的数据库连接信息,不然连接不上数据库,会报错。

6、接着,访问http://110.108.64.23网址即可访问系统了,如果访问不到的话,要看下nginx错误日志还有uwsgi的日志,排除问题。

7、添加多一个django项目的话类似上面的操作, 即第六步的相关操作,特别注意:加多个端口,防火墙和阿里云服务器都要开放多个对应的端口,不然有可能你日志什么都没报错,就是访问不了,如果访问不了,可重启服务器,然后重新开启nginx、uwsgi服务,看nginx配置文件和uwsgi的日志会不会报错。

8、每次修改项目代码之后,都需要重新启动nginx和uwsgi

(1)、nginx重启命令:

/usr/local/nginx/sbin/nginx -s reload

(2)、uwsgi不太一样,需杀死uwsgi进程再重新开启,有4个相关联的uwsgi服务,需全部杀死再重启:

ps -ef | grep uwsgi

kill -s 9 uwsgi的进程号

cd /project/test

uwsgi -x project_test_socket.xml

七、总结:

(1)、在这之前我是安装过apache服务器还有相关的一些依赖库,其中如果想用apache部署django项目的可以参考下面的链接,其中也有不少坑:

Linux下安装Apache服务:https://www.cnblogs.com/wcwnina/p/8029156.html

django项目利用mod_wsgi+Apache部署到linux服务器上:https://blog.csdn.net/yufen9987/article/details/76718290

Centos pip 安装uwsgi 报错“fatal error: Python.h: No such file or directory”: https://www.cnblogs.com/eating-gourd/p/8578007.html ,其中这篇文章后面用到的命令要根据你python版本来,我这边是用了yum install python36-devel命令,但是最后会导致python版本升级到对应3.6最新的版本,不过没多大影响。

(2)、如果遇到这种情况,centos7中python3.6报错ModuleNotFoundError: No module named '_ssl',或者pip安装库时出现:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

Could not fetch URL https:*******: There was a problem confirming the ssl certificate: 
Can't connect to HTTPS URL because the SSL module is not available. - skipping

可以参考下面的文章:https://blog.csdn.net/qq_23889009/article/details/100887640

需要改下/python3.6.3/Python-3.6.3/Modules 安装目录底下的Setup.dist,找到下面几行,注释掉:

接着重新编译,注意 ./configure --prefix=/usr/local/python3.6.3 这里无须加参数,具体参考下面的链接:

https://blog.csdn.net/qq_23889009/article/details/100887640

(3)、这是我部署好之后整理的,可能在实际操作中有很多问题,我一个开发,搞到运维去了,我也是醉了!

   以上内容,仅供参考,如有问题,敬请见谅并提出来,谢谢!

发布了276 篇原创文章 · 获赞 200 · 访问量 72万+

猜你喜欢

转载自blog.csdn.net/u012561176/article/details/93893816
今日推荐