CentOS7下部署Django(nginx+uWSGI+Python3+Django)
部署代码后uWSGI需要重新启动,关闭系统防火墙或者开放端口
- 系统版本:CentOS7.0
- Python版本:Python3.6.3
- Django版本:2.0.5
- uWSGI版本:2.0.17
- nginx版本1.4.4
1.安装需要的依赖
yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
yum install libxml*
yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt libmcrypt-devel cmake
安装libxml模块是为了让uwsig支持使用“-x"选项,能通过xml文件启动项目
2.编译安装python3
进入目录,依次执行以下命令:
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
#下载完成后,执行解压命令:
tar -zxvf Python-3.6.3.tar.gz
cd Python-3.6.3
#将python3安装到/usr/local/python3/路径下
./configure --prefix=/usr/local/python3
make && make install
#创建软连接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
3.安装Django和uWSGI配置启动项目xml文件
pip3 install django
pip3 install uwsgi
创建uWSGI软连接:
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
将你的Django项目放到你想放的路径下,并在项目里新建 django_study.xml,内容如下:
<uwsgi>
<!-- 内部端口,自定义 -->
<socket>127.0.0.1:8000</socket>
<!-- 项目路径 -->
<chdir>项目的路径</chdir>
<module>django_study.wsgi</module>
<!-- 进程数 -->
<processes>4</processes>
<!-- 日志文件 -->
<daemonize>uwsgi.log</daemonize>
</uwsgi>
4.安装nginx和配置nginx.conf文件
- 安装nginx
wget http://nginx.org/download/nginx-1.4.4.tar.gz /usr/local/src
cd /usr/local/src
tar xf nginx-1.4.4.tar.gz
cd nginx-1.4.4
创建用户www和用户组www
groupadd www
useradd -g www www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
make && make install
- 配置nginx
server {
#暴露给外部访问的端口
listen 80;
server_name localhost;
charset utf-8;
location / {
include uwsgi_params;
#外部访问80就转发到内部8000
uwsgi_pass 127.0.0.1:8000;
}
}
#检查配置文件是否有误
/usr/local/nginx/sbin/nginx -t
提示nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful的话说明配置文件无误,启动nginx
/usr/local/nginx/sbin/nginx
5.访问项目页面
进入Django项目路径,执行以下命令:
uwsgi3 -x django_study.xml
一个勤勤恳恳码代码的码农,爱生活,爱代码!