一、安装 nginx
在安装 nginx 前,需要先装 nginx 的依赖包。
1、如果没有 yum 则先安装 yum
删除原有的 yum
rpm -aq|grep yum|xargs rpm -e --nodeps
自己到下面网站下载对应的 rpm 包(这里注意一定要对应你的系统,是 32 位还是 64 位的,本人就曾犯过类似的错误,总是导致安装失败,原因就是我的系统是 32 位的,而网上的资料大部分是 64 的 rpm)
官网地址:
python-iniparse-0.3.1-2.1.el6.noarch.rpm
yum-metadata-parser-1.1.2-14.1.el6.x86_64.rpm
yum-3.2.27-14.el6.centos.noarch.rpm
yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
下载上面的 4 个 rpm
安装的时候需要按照顺序安装,因为 yum 是依赖 python 的
rpm -ivh python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm -ivh yum-metadata-parser-1.1.2-14.1.el6.x86_64.rpm
rpm -ivh yum-3.2.27-14.el6.centos.noarch.rpm yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
注意最后两个包必需同时安装,相互依赖.
安装好后记得 yum update 更新下包
2、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3、安装 PCRE,PCRE 作用是让 Nginx 支持 Rewrite 功能
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
成功后执行:pcre-config --version, 如果显示 8.35 则安装成功
4、安装 nginx
到 下载自己需要的版本,版本不一定越高越好,还是搞个稳定的吧
wget http://nginx.org/download/nginx-0.1.10.tar.gz
tar zxvf nginx-0.1.10.tar.gz
cd nginx-0.1.10.tar.gz
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
安装成功后运行:/usr/local/webserver/nginx/sbin/nginx -v,显示版本号则安装成功
添加用户:
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
下面附上 nginx 的说明,来自 w3c
- 1 user www www;
- 2 worker_processes 2; #设置值和CPU核心数一致
- 3 error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
- 4 pid /usr/local/webserver/nginx/nginx.pid;
- 5 #Specifies the value for maximum file descriptors that can be opened by this process.
- 6 worker_rlimit_nofile 65535;
- 7 events
- 8 {
- 9 use epoll;
- 10 worker_connections 65535;
- 11 }
- 12 http
- 13 {
- 14 include mime.types;
- 15 default_type application/octet-stream;
- 16 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- 17 '$status $body_bytes_sent "$http_referer" '
- 18 '"$http_user_agent" $http_x_forwarded_for';
- 19
- 20 #charset gb2312;
- 21
- 22 server_names_hash_bucket_size 128;
- 23 client_header_buffer_size 32k;
- 24 large_client_header_buffers 4 32k;
- 25 client_max_body_size 8m;
- 26
- 27 sendfile on;
- 28 tcp_nopush on;
- 29 keepalive_timeout 60;
- 30 tcp_nodelay on;
- 31 fastcgi_connect_timeout 300;
- 32 fastcgi_send_timeout 300;
- 33 fastcgi_read_timeout 300;
- 34 fastcgi_buffer_size 64k;
- 35 fastcgi_buffers 4 64k;
- 36 fastcgi_busy_buffers_size 128k;
- 37 fastcgi_temp_file_write_size 128k;
- 38 gzip on;
- 39 gzip_min_length 1k;
- 40 gzip_buffers 4 16k;
- 41 gzip_http_version 1.0;
- 42 gzip_comp_level 2;
- 43 gzip_types text/plain application/x-javascript text/CSS application/xml;
- 44 gzip_vary on;
- 45
- 46 #limit_zone crawler $binary_remote_addr 10m;
- 47 #下面是server虚拟主机的配置
- 48 server
- 49 {
- 50 listen 80;#监听端口
- 51 server_name localhost;#域名
- 52 index index.html index.htm index.php;
- 53 root /usr/local/webserver/nginx/html;#站点目录
- 54 location ~ .*\.(php|php5)?$
- 55 {
- 56 #fastcgi_pass unix:/tmp/php-cgi.sock;
- 57 fastcgi_pass 127.0.0.1:9000;
- 58 fastcgi_index index.php;
- 59 include fastcgi.conf;
- 60 }
- 61 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
- 62 {
- 63 expires 30d;
- 64 # access_log off;
- 65 }
- 66 location ~ .*\.(js|css)?$
- 67 {
- 68 expires 15d;
- 69 # access_log off;
- 70 }
- 71 access_log off;
- 72 }
- 73
- 74 }
检查配置文件 ngnix.conf 的正确性命令:
/usr/local/webserver/nginx/sbin/nginx -t
Nginx 启动命令如下:
/usr/local/webserver/nginx/sbin/nginx
从浏览器访问我们配置的站点 ip:如果出现下图,则表示 nginx 安装成功
以下包含了 Nginx 常用的几个命令:
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
如果在启动过程出现以下错误,请按照下面的方法解决
启动命令:/usr/local/nginx/sbin/nginx
发现报错了:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
经网上查询,这是 Linux 的通病
[root@localhost nginx]# sbin/nginx
sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
先找到 libpcre.so.1 所在位置,然后做个软链接就可以了
5、安装 uWSGI
最好通过 pip install 来安装 uWSGI
pip install uwgsi
安装成功后,执行 uwsgi --version #查看版本
有时候 pip 会执行不成功,原因是 pip 默认执行的是系统 python2.6 的版本,自己需要重新制定一下,用 python2.7 的 pip 来执行
可以通过
which pip
/usr/local/bin/pip
cat /usr/local/bin/pip
找到头部执行 python 重新指定到 python2.7
测试 uwsgi 是否正常:
新建 server.py 文件,内容如下:
- 1 def application(env, start_response):
- 2 start_response('200 OK', [('Content-Type','text/html')])
- 3 return "Hello World"
启动:uwsgi --http :8001 --wsgi-file server.py
浏览器访问,如果出现 Hello World 则表示 uwsgi 安装成功
6、安装 web.py
在安装之前,对 web.py 做下简单的介绍,web.py 是一个 Python 的 web 框架,它简单而且功能强大。web.py 实现了 WSGI 并能在任何兼容它的服务器上运行。 WSGI 是一个 web 服务器与应用程序之间的通用 API, 就如 Java 的 Servlet 接口。 你需要安装 flup 使 web.py 支持 with CGI, FastCGI 或 SCGI, flup 提供了这些 API 的 WSGI 接口。但绝大多数网站还是需要更加专业一些的 web 服务器。很多框架都自带了 WSGI server ,比如 Flask,webpy,Django、CherryPy 等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server 或者是联合 nginx 做 uwsgi 。
安装 web.py, 请先下载:
http://webpy.org/static/web.py-0.37.tar.gz
或者获取最新的开发版:
https://github.com/webpy/webpy/tarball/master
解压并拷贝 web 文件夹到你的应用程序目录下。 或者,为了让所有的应用程序都可以使用,运行:
python setup.py install
注意: 在某些类 unix 系统上你可能需要切换到 root 用户或者运行:
sudo python setup.py install
查看 推荐设置.
另外一个选择是使用 Easy Install. Easy Install 使用如下:
easy_install web.py
或者 PIP
sudo pip install web.py
以一个简单的 webpy 程序作为示例。以下代码是一个完整的 webpy 程序 (webpyserv.py)
- 1 #!/usr/bin/env python
- 2 # -*- coding:utf-8 -*-
- 3 import web
- 4
- 5 urls = (
- 6 '/(.*)', 'myweb'
- 7 )
- 8
- 9 app = web.application(urls, globals())
- 10
- 11 class myweb:
- 12 def GET(self, name):
- 13 if not name:
- 14 name = "World"
- 15 return "Hello %s, this is my first web!" % name
- 16
- 17 application = app.wsgifunc()
- 18
- 19 #最后一句的application = app.wsgifunc()是关键,此时才可以通过wsgi进行访问
uwsgi -s 127.0.0.1:9000 -w webpyserv
7、更改 nginx 配置
- 1 server {
- 2 listen 80;
- 3 server_name 104.224.128.197;
- 4
- 5 #charset koi8-r;
- 6
- 7 #access_log logs/host.access.log main;
- 8
- 9 location / {
- 10 #root html;
- 11 #index index.html index.htm;
- 12 include uwsgi_params;
- 13 uwsgi_pass 127.0.0.1:9000; #这里配置必须和启动uwsgi时的一致
- 14 uwsgi_param UWSGI_CHDIR /home/www; #程序所在的目录
- 15 uwsgi_param UWSGI_SCRIPT webpyserv; #
- 16 }
注意:uwsgi_pass 的相关配置必须和启动 uwsgi 时的一致!UWSGI_CHDIR 是指程序所在的目录,UWSGI_SCRIPT 是指启动哪个程序(注意,这里必须去掉 py 后缀)
重启 nginx 命令为 (/usr/local/nginx/sbin/nginx -s realod)
通过 http 即可访问
http://104.224.128.197/eric
页面打印:Hello eric, this is my first web!
OVER!
来源: http://www.cnblogs.com/gide/p/6576781.html