Nginx uWSGI web.py 站點搭建


一、安裝nginx

  在安裝nginx前,需要先裝nginx的依賴包。

  1、如果沒有yum則先安裝yum

     刪除原有的yum

     rpm -aq|grep yum|xargs rpm -e --nodeps

       自己到下面網站下載對應的rpm包(這里注意一定要對應你的系統,是32位還是64位的,本人就曾犯過類似的錯誤,總是導致安裝失敗,原因就是我的系統是32位的,而網上的資料大部分是64的rpm)

          官網地址:http://mirror.centos.org/centos/6/os/

     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

      到http://nginx.org/download/ 下載自己需要的版本,版本不一定越高越好,還是搞個穩定的吧

      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 reload)

通過http即可訪問
http://104.224.128.197/eric

頁面打印:Hello eric, this is my first web!

OVER!

本文為本人原創,轉載時請注明出處,Thanks!

另外喜歡用kindle讀書的朋友可以關注本人的kindle公眾號(生活在別處),可以輸入需要找的書,點擊進入直接推送到kindle設備。

如果覺得本文對你有幫助,請賞個飯補


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM