1、安裝nginxjavascript
在安裝nginx前,須要先裝nginx的依賴包。php
一、若是沒有yum則先安裝yumcss
刪除原有的yumhtml
rpm -aq|grep yum|xargs rpm -e --nodepsjava
本身到下面網站下載對應的rpm包(這裏注意必定要對應你的系統,是32位仍是64位的,本人就曾犯過相似的錯誤,老是致使安裝失敗,緣由就是個人系統是32位的,而網上的資料大部分是64的rpm)node
官網地址:http://mirror.centos.org/centos/6/os/python
python-iniparse-0.3.1-2.1.el6.noarch.rpmnginx
yum-metadata-parser-1.1.2-14.1.el6.x86_64.rpmc++
yum-3.2.27-14.el6.centos.noarch.rpmgit
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更新下包
二、安裝編譯工具及庫文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
三、安裝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則安裝成功
四、安裝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所在位置,而後作個軟連接就能夠了
五、安裝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安裝成功
六、安裝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
七、更改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設備。
若是以爲本文對你有幫助,請賞個飯補