1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > CentOS+Nginx+uWSGI+Python多站点环境搭建

CentOS+Nginx+uWSGI+Python多站点环境搭建

时间:2019-10-24 01:19:26

相关推荐

CentOS+Nginx+uWSGI+Python多站点环境搭建

/usr/lib/python2.7/site-packages/django/bin/django-admin.py startproject myweb

但是又看了一下uwsgi 的官方文档,发现上面的配置有点旧,新的配置做了简化,

所以,根据Django和uWSGI 的文档,我重新整理了一个新的配置,如下:

[plain]view plaincopy[uwsgi] socket=127.0.0.1:50000 chdir=/home/foobar/myproject/ wsgi-file=myproject/wsgi.py processes=2 stats=192.168.1.18:9090 daemonize=/var/log/uwsgi/yourproject.log vacuum=true

● 解释一下常用选项:

socket: 地址和端口号,例如:socket = 127.0.0.1:50000

processes: 开启的进程数量

workers: 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes

chdir: 指定运行目录(chdir to specified directory before apps loading)

wsgi-file: 载入wsgi-file(load .wsgi file)

stats: 在指定的地址上,开启状态服务。注意,这里尽量用非127.0.0.1的IP。(enable the stats server on the specified address)

threads: 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)

master: 允许主进程存在(enable master process)

daemonize: 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)

vacuum: 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

■ 启动uwsgi:

把上面编辑好的 ini 文件,保存为【 uwsgi_conf.ini 】,注意为ANSI格式,如果是UTF-8,则报错。

理论上可以把这个ini文件,放到服务器任意位置。但还是推荐放到项目下,然后运行:

uwsgi uwsgi_conf.ini

■ Nginx配置:

nginx安装不在叙述,这里主要讲如何更改nginx.conf:

location / {

include uwsgi_params;

uwsgi_pass 127.0.0.1:50000;

}

■ 重启Nginx:

重启nginx,再次访问你的项目,一切OK!

■ 参考:

A,《你应该使用 Nginx + UWSGI》: (本文的主要参考,放弃使用uwsgi自带的负载均衡,由此而来)

/Linux/-07/87286.htm

B,《uwsgi其二》:

/academy/detail/1330331

C,《五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下) 》:

http://django-/topic/124/

D,《How to use Django with uWSGI》:(Django官网文档,本文的次要参考

/en/1.7/howto/deployment/wsgi/uwsgi/

CentOS X64 6.4

nginx 1.5.6

Python 2.7.5

环境:

CentOS X64 6.4

nginx 1.5.6

Python2.7.5

正文:

一:安装需要的类库及Python2.7.5

安装必要的开发包

yum groupinstall "Development tools"yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 自带Python2.6.6,但我们可以再安装Python2.7.5:

cd ~wget /ftp/python/2.7.5/Python-2.7.5.tar.bz2tar xvf Python-2.7.5.tar.bz2cd Python-2.7.5./configure --prefix=/usr/localmake && make altinstall

安装完毕后,可是使用”python2.7”命令进入python2.7的环境。

二:安装Python包管理

easy_install包/pypi/distribute

方便安装Python的开发包

cd ~wget /packages/source/d/distribute/distribute-0.6.49.tar.gztar xf distribute-0.6.49.tar.gzcd distribute-0.6.49python2.7 setup.py installeasy_install --version

红色部分必须是“python2.7”,否则将安装到默认的2.6环境内。

pip包/pypi/pip

安装pip的好处是可以pip list、pip uninstall 管理Python包, easy_install没有这个功能,只有uninstall

easy_install pippip --version

三:安装uwsgi

uwsgi:/pypi/uWSGI

uwsgi参数详解:http://uwsgi-/en/latest/Options.html

pip install uwsgiuwsgi --version

测试uwsgi是否正常:

新建test.py文件,内容如下:

def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return "Hello World"

然后在终端运行:

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

在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。

四:安装django

pip install django

测试django是否正常,运行:

django-admin.py startproject demositecd demositepython2.7 manage.py runserver 0.0.0.0:8002

在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。

五:安装nginx

cd ~wget /download/nginx-1.5.6.tar.gztar xf nginx-1.5.6.tar.gzcd nginx-1.5.6./configure --prefix=/usr/local/nginx-1.5.6 \--with-http_stub_status_module \--with-http_gzip_static_modulemake && make install

六:配置uwsgi

uwsgi支持ini、xml等多种配置方式,但个人感觉ini更方便:

在/ect/目录下新建uwsgi9090.ini,添加如下配置:

[uwsgi]socket = 127.0.0.1:9090master = true //主进程vhost = true//多站模式no-stie = true //多站模式时不设置入口模块和文件workers = 2 //子进程数reload-mercy = 10vacuum = true //退出、重启时清理文件max-requests = 1000 limit-as = 512buffer-sizi = 30000pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程daemonize = /website/uwsgi9090.log

设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9090文件,内容如下:

uwsgi9090

然后在终端执行:

-- 添加服务chkconfig --add uwsgi9090 -- 设置开机启动chkconfig uwsgi9090 on

七:设置nginx

找到nginx的安装目录,打开conf/nginx.conf文件,修改server配置

server {listen 80;server_name localhost;location / { include uwsgi_params;uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录uwsgi_param UWSGI_CHDIR /demosite; //项目根目录index index.html index.htm;client_max_body_size 35m;}}

设置nginx开机启动,在/ect/init.d/目录下新建nginx文件,内容如下:

nginx

然后在终端执行:

-- 添加服务chkconfig --add nginx -- 设置开机启动chkconfig nginx on

八:测试

OK,一切配置完毕,在终端运行

service uwsgi9090 startservice nginx start

在浏览器输入:http://127.0.0.1,恭喜你可以看到django的“It work”了~

九:多站配置

我采用运行多个uwsgi服务的方法来实现多个站点。

重复第六步,创建uwsgi9091.ini,并相应修改文件中的

socket = 127.0.0.1:9091pidfile = /var/run/uwsgi9091.piddaemonize = /website/uwsgi9091.log

并创建服务uwsgi9091,设置开机启动。

然后修改nginx的配置文件为:

nginx

然后我们就可以通过http://127.0.0.1:1300来访问新的网站了。

十:其他配置

防火墙设置

CentOS默认关闭外部对80、3306等端口的访问,所以要在其他计算机访问这台服务器,就必须修改防火墙配置,打开/etc/sysconfig/iptables

在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:

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

然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:

service iptables restart

安装Mysqldb

yum -y install mysql-develeasy_install-2.7 MySQL-python

注意红色部分,easy_install-2.7,否则它将默认安装到Python2.6环境内。

------------------------------------------------------------------------------------------------------------------

12月02日添加:

CentOS 7中默认使用Firewalld做防火墙,所以修改iptables后,在重启系统后,根本不管用。

Firewalld中添加端口方法如下:

firewall-cmd --zone=public --add-port=3306/tcp--permanent

firewall-cmd --reload

参考:/xiongpq/p/3381069.html

/fwenzhou/article/details/8772974

/boycycyzero/article/details/7339500

在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时,则会提示命令不存在的错误,下面我们详细介绍一下在linux下将php和mysql加入到环境变量中的方法(假设php和mysql分别安装在/usr/local/webserver/php/和/usr/local/webserver/mysql/中)。

方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/mysql/bin

使用这种方法,只会对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效,只是临时生效。

方法二:执行vi ~/.bash_profile修改文件中PATH一行,将/usr/local/webserver/php/bin 和 /usr/local/webserver/mysql/bin 加入到PATH=$PATH:$HOME/bin一行之后

这种方法只对当前登录用户生效

方法三:修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码

PATH=$PATH:/usr/local/webserver/php/bin:/usr/local/webserver/mysql/bin

export PATH

最后:执行 命令source /etc/profile或 执行点命令 ./profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。