Tengine的官網:http://tengine.taobao.org/php
wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz` tar -zxvf tengine-2.1.0.tar.gz cd tengine-2.1.0
須要注意的是,我但願用Jemalloc
來分配和釋放tengine內存(比默認的內存管理好多了),因此編譯選項設置了--with-jemalloc
的源碼路徑,源碼包的得到具體經過如下方式:html
官網 http://www.canonware.com/jemalloc/node
wget https://github.com/jemalloc/jemalloc/releases/download/3.6.0/jemalloc-3.6.0.tar.bz2 tar -jxvf jemalloc-3.6.0.tar.bz2 -C /usr/local/src
./configure \ --prefix=/usr/local/nginx \ --with-jemalloc=/usr/local/src/jemalloc-3.6.0/ \ –-user=www-data \ –-group=www-data
若是想知道更多詳細的編譯參數的話能夠執行./configure --help
,會打印出全部的編譯選項,能夠按需添加或者禁用一些tengine
模塊,也能夠在之後的使用中按需添加編譯屬性而後從新編譯安裝便可。
執行configure
時,可能會報各類依賴包不存在的提示,好比openssl
,若是是開發環境或者對這些依賴程序的版本沒有要求的話,能夠直接經過apt-get install libxxx
的方式安裝這些依賴包。mysql
make make test make install
make install
以後會安裝在/usr/local/nginx
目錄下,其中conf
爲tengine
的配置文件目錄,咱們修改一下/usr/local/nginx/conf/nginx.conf
這個配置文件:
若是有像Apache
的vhost
需求的話,能夠在conf
目錄下新建一個文件夾vhost
,而後全部虛擬主機的配置文件都放到vhost
這裏面:nginx
cd /usr/local/nginx/conf mkdir vhost vim nginx.conf
打開nginx.conf
配置文件後,最基礎的須要作如下幾個地方的修改(所提到的地方都要去除前面的#
號註釋,只修改提到的地方,其它地方保留默認便可):git
#Tengine的進程以www-data用戶啓動 user www-data; #自動以CPU核心數啓動相應數量的進程 worker_processes auto; #開啓gzip支持 gzip on #全局的錯誤日誌地址,方便調試 error_log /home/jason/BigDisk/nginx/error.log; server { #設置默認訪問的server(default是指經過ip或者servername未定義的域名進行訪問時就走到這個server) listen 80 default; #默認訪問的路徑 root /home/jason/Work/nginx-default; location / { index index.php index.html index.htm; } #默認錯誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #抓發到php-fpm location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #引用其餘的vhost配置文件 include vhost/*.conf;
安裝好Tengine
後須要啓動它,github
上有人已經作了一份比較完美的nginx啓動腳本,咱們直接拿下來用:wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
github
賦予可執行的權限:chmod +x /etc/init.d/nginx
web
啓動Tengine:/etc/init.d/nginx start
sql
這樣基本上就配置好了Tenginenpm
PHP的官網:http://php.net/
安裝的php版本爲5.6.8
wget http://cn2.php.net/distributions/php-5.6.8.tar.bz2 tar jxvf php-5.6.8.tar.bz2 cd php-5.6.8
./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-config-file-scan-dir=/usr/local/php/etc/extends \ --enable-fpm \ --with-fpm-user=www-data \ --with-fpm-group=www-data \ --with-mysql=shared,mysqlnd \ --enable-sockets \ --with-mcrypt \ --with-curl \ --with-openssl \ --with-mysql \ --enable-mbstring
編譯參數帶上了--enable-fpm
,這樣編譯時就會自動爲咱們編譯php-fpm
--with-config-file-path
指定php的配置文件目錄--with-config-file-scan-dir
指定php的其它配置文件放到哪一個目錄下
一樣,執行configure
的時候會出現各類依賴庫未找到的錯誤,仍是能夠經過apt-get install libxxx的方式添加這些庫,或者本身下載源碼編譯安裝這些庫。
make make test make install
把php的一些二進制執行文件軟連接到系統執行目錄中ln -s /usr/local/php/bin/php /usr/local/bin/php
ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
進入php的配置文件cd /usr/local/php/etc
從源碼包中複製php.ini-development
或者php.ini-production
文件到此目錄下,並修更名字爲php.ini
其中php.ini-development
適合開發環境下的php配置文件php.ini-production
適合生產環境下的php配置文件
打開php-fpm
的配置文件,作以下修改:vim php-fpm.conf
//php-fpm程序運行的pid記錄文件 pid = /var/run/php-fpm.pid //在[www]區域的配置 user = www-data group = www-data //這樣設置的目的是讓tengine與php-fpm經過這個socket地址進行通訊 listen = /var/run/php-fpm.socket //建立socket地址時的用戶、組、權限, //最好以tengine運行的用戶一致,這樣tengine纔有權限與php-fpm進行通訊 listen.owner = www-data listen.group = www-data listen.mode = 0660
進入php源碼包中的ext目錄cd php-5.6.8/ext
舉個例子吧,假如說咱們要編譯一個mcrypt
擴展
進入mcrypt模塊的源碼目錄cd mcrypt
執行phpize
來動態添加php擴展phpize
編譯安裝./configure
make
make install
安裝好後,咱們以前編譯php時設置的--with-config-file-scan-dir
就派上用場了,
進入設置的/usr/local/php/etc/extends
目錄,若是沒有extends
目錄能夠mkdir
一個。
新建一個mcrypt.conf
文件vim mcrypt.conf
寫入
extension=mcrypt.so
這樣的話,若是之後咱們不須要mcrypt
擴展時,能夠直接刪除/usr/local/php/etc/extends/mcrypt.conf
配置文件便可。
php源碼包中已自帶php-fpm的啓動腳本,咱們將它複製到/etc/init.d/
:cp php-5.6.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
修改php-fpm文件vim /etc/init.d/php-fpm
prefix=/usr/local/php //這裏的pid文件路徑就是上面(php-fpm.conf)配置文件中所設置的pid php_fpm_PID=/var/run/php-fpm.pid
賦予可執行的權限:chmod +x /etc/init.d/php-fpm
啓動php-fpm:/etc/init.d/php-fpm start
這樣php
和php-fpm
的配置基本就完成了,關於如何配合Tengine
使用在最下面會說。
wget http://nodejs.org/dist/v0.12.2/node-v0.12.2.tar.gz tar zxvf node-v0.12.2.tar.gz cd node-v0.12.2
nodejs的編譯安裝不須要太多配置
./configure make make install
這樣npm
和 node
就一併安裝好了
進入先前設置好的Tengine的vhost配置目錄:cd /usr/local/nginx/conf/vhost
假設咱們的web目錄是在/opt/www
,域名爲test.com
新建一個test.com.conf
配置文件
server { #訪問端口 listen 80; #所綁定的域名 server_name test.com; #access_log logs/host.access.log main; location / { root /opt/www; index index.php index.html; } #設置php文件的代理轉發 location ~ \.php$ { root /opt/www; #轉發至php-fpm配置文件中設置的socket通訊地址 fastcgi_pass unix:/var/run/php-fpm.socket; fastcgi_index index.php; #所訪問的php文件路徑地址 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
配置文件建立成功後執行/etc/init.d/nginx reload
便可
假設咱們運行了一個端口號3000的node.js的web應用
咱們的域名爲testnode.com
新建一個testnode.com.conf
配置文件
#設置轉發池 upstream testnode { #咱們的nodejs應用地址,可添加多個 #這塊的意義是,咱們能夠啓動多個相同的nodejs應用,並設置不一樣的端口號,或者有多臺同服務的nodejs服務器,負載均衡 server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; .... keepalive 64; } server { listen 80; server_name testnode.com; location / { #這塊設置的意義是當全部請求轉發至nodejs應用時,除了客戶端發來的請求頭以外再攜帶如下這些頭信息 proxy_set_header X-Real-IP remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header Connection ""; #http1.1比http1.0的效率要高的多 proxy_http_version 1.1; #填上面定義的轉發池名稱 proxy_pass http://testnode; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
配置文件建立成功後執行/etc/init.d/nginx reload
便可