標籤:http 使用 os io strong 檔案 資料 for
(60MB)
: http://pan.baidu.com/s/1kTieHzL
md5sum png.tar.gz 547e261ef268d267799add5e2ffa4f34
安裝依賴包:
sudo apt-get -y install \
build-essential \
autoconf \
libtool \
libxml2 \
libxml2-dev \
openssl \
libcurl4-openssl-dev \
libbz2-1.0 \
libbz2-dev \
libjpeg-dev \
libpng12-dev \
libfreetype6 \
libfreetype6-dev \
libldap-2.4-2 \
libldap2-dev \
libmcrypt4 \
libmcrypt-dev \
libmysqlclient-dev \
libxslt1.1 \
libxslt1-dev \
libxt-dev \
libpcre3-dev
大約需要下載24MB的安裝包.
建立運行使用者:
sudo addgroup png --system
sudo adduser png --system --disabled-login --ingroup png --no-create-home --home /nonexistent --gecos "png user" --shell /bin/false
建立解壓目錄:
sudo mkdir /png
sudo chown $USER:$USER /png
解壓到對應的目錄即可:
tar xzf png.tar.gz -C /png
/png/httpd/2.4.10/
/png/nginx/1.6.0/
/png/php/5.4.31/
網站根目錄:
/png/www/
配置位置:
/png/httpd/2.4.10/conf/httpd.conf
/png/nginx/1.6.0/conf/nginx.conf
/png/php/5.4.31/lib/php.ini
/png/php/5.4.31/etc/php-fpm.conf
啟動:
sudo /png/httpd/2.4.10/bin/apachectl start
sudo /png/nginx/1.6.0/png-nginx start
sudo /png/php/5.4.31/png-fpm start
top -n1 -b|egrep ‘httpd|nginx|fpm‘
測試:
curl -I 127.0.0.1/info.php
curl -I 127.0.0.1/fpm/info.php
curl -I 127.0.0.1:8080/info.php
開機自啟動:
sudo ln -s /png/httpd/2.4.10/bin/apachectl /etc/init.d/png-httpd
sudo ln -s /png/nginx/1.6.0/png-nginx /etc/init.d/
sudo ln -s /png/php/5.4.31/png-fpm /etc/init.d/
sudo update-rc.d png-httpd defaults
sudo update-rc.d png-nginx defaults
sudo update-rc.d png-fpm defaults
使用service管理這幾個服務:
sudo service png-httpd restart|stop|start
sudo service png-nginx restart|stop|start
sudo service png-fpm restart|stop|start
需要的話,可以這樣刪除啟動項:
sudo update-rc.d -f png-httpd remove
sudo update-rc.d -f png-nginx remove
sudo update-rc.d -f png-fpm remove
配置環境變數:
sudo nano /etc/profile 在末尾加入:
export PATH=/png/nginx/1.6.0/sbin:/png/httpd/2.4.10/bin:/png/php/5.4.31/bin:$PATH
source /etc/profile 在當前終端生效,重啟完全生效.
nginx -v && httpd -v && php -v
重啟測試:
sudo shutdown -r now
附: 編譯配置參考(授人以魚&授人以漁)
NGINX編譯配置:
編輯Nginx原始碼包裡的auto/cc/gcc,用"#"號注釋掉CFLAGS="$CFLAGS -g",去除Debug資訊,這樣編譯出來的Nginx體積小於1MB.
修改nginx的header資訊:
src/core/nginx.h
#define NGINX_VERSION "1.6.0"
#define NGINX_VER "nginx/" NGINX_VERSION 其中nginx可以改為nginx_ubuntu_server
其中ngx_cache_purge是一個第三方模組,要使用的話請自行下載並用--add-module指定位置.
configure-nginx.sh
#!/bin/bash
./configure \
--prefix=/png/nginx/1.6.0 \
--sbin-path=/png/nginx/1.6.0/sbin/nginx \
--conf-path=/png/nginx/1.6.0/conf/nginx.conf \
--error-log-path=/png/nginx/1.6.0/var/log/error.log \
--http-log-path=/png/nginx/1.6.0/var/log/access.log \
--pid-path=/png/nginx/1.6.0/var/run/nginx.pid \
--lock-path=/png/nginx/1.6.0/var/run/nginx.lock \
--http-client-body-temp-path=/png/nginx/1.6.0/var/cache/client_temp \
--http-proxy-temp-path=/png/nginx/1.6.0/var/cache/proxy_temp \
--http-fastcgi-temp-path=/png/nginx/1.6.0/var/cache/fastcgi_temp \
--http-uwsgi-temp-path=/png/nginx/1.6.0/var/cache/uwsgi_temp \
--http-scgi-temp-path=/png/nginx/1.6.0/var/cache/scgi_temp \
--user=png \
--group=png \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--add-module=/png/src/ngx_cache_purge-2.1
成功產生 Makefile 後便可以執行 make && make install 編譯安裝.
Nginx的服務管理指令碼可以參考Nginx官方提供的Deb包裡的指令碼/etc/init.d/nginx:
http://nginx.org/packages/ubuntu/pool/nginx/n/nginx/
只需要修改指令碼開始定義的幾個值:
CONFFILE=/png/nginx/1.6.0/conf/nginx.conf
DAEMON=/png/nginx/1.6.0/sbin/nginx
PIDFILE=/png/nginx/1.6.0/var/run/$NAME.pid
SCRIPTNAME=/png/nginx/1.6.0/$NAME
我把修改好的服務管理指令碼放到了/png/nginx/1.6.0/png-nginx
編輯 /png/nginx/1.6.0/conf/nginx.conf
去掉 location / 裡的兩行,然後在 location / 前添加:
# 定義根目錄和索引檔案
root /png/www;
index index.html index.htm index.php;
# 開啟目錄列表
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
在 location / 後添加:
# fpm目錄下的PHP請求交由PHP-FPM處理
location ^~ /fpm {
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# 其他PHP請求交由監聽8080連接埠的Apache處理
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
Apache編譯配置:
修改httpd的header資訊:
檔案1: include/ap_release.h
#define AP_SERVER_BASEVENDOR "Apache Software Foundation"
#define AP_SERVER_BASEPROJECT "Apache HTTP Server"
#define AP_SERVER_BASEPRODUCT "Apache" 其中Apache可以改為Apache_Ubuntu_Server
#define AP_SERVER_MAJORVERSION_NUMBER 2
#define AP_SERVER_MINORVERSION_NUMBER 4
#define AP_SERVER_PATCHLEVEL_NUMBER 9
檔案2: os/unix/os.h
#define PLATFORM "Unix" 其中Unix可以改為GNU/Linux
configure-httpd.sh
#!/bin/bash
./configure \
--prefix=/png/httpd/2.4.10 \
--enable-mods-shared=most \
--enable-ssl=shared \
--with-ssl=/usr \
--with-included-apr \
--with-mpm=prefork
成功產生 Makefile 後便可以執行 make && make install 編譯安裝.
編輯/png/httpd/2.4.10/conf/httpd.conf
在末尾添加:
# 把以.php結尾的檔案交由php模組處理,可以替代AddHandler application/x-httpd-php .php
<FilesMatch \.php$>
setHandler application/x-httpd-php
</FilesMatch>
# 添加index.php,不存在index.html時則載入index.php
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
# 修飾列表
Include conf/extra/httpd-autoindex.conf
# 設定列表編碼(預設是ISO-8859-1)
IndexOptions Charset=UTF-8
# 顯示詳盡的伺服器資訊
ServerSignature On
ServerTokens Full
# 隱藏版本資訊
#ServerSignature Off
#ServerTokens Prod
Include conf/extra/httpd-mpm.conf
修改運行使用者:
把:
User daemon
Group daemon
改為:
User png
Group png
因為Nginx和PHP編譯配置時用參數指定了Nginx和PHP-FPM的運行使用者,所以Nginx和PHP-FPM的運行使用者就不需要手動修改了.
修改Apache的根目錄:
搜尋 /png/httpd/2.4.10/htdocs, 替換為 /png/www, 有2處.
開啟.htaccess重寫支援:
把<Directory "/png/www">裡的AllowOverride None改為AllowOverride All
修改監聽連接埠:
把 Listen 80 改為 Listen 8080
把 ServerName www.example.com:80 改為 ServerName 127.0.0.1:8080
PHP編譯配置:
注意順序,先編譯Apache,後編譯PHP,因為編譯PHP時需要使用--with-apxs2=/png/httpd/2.4.10/bin/apxs構建Apache的PHP模組libphp5.so.
編譯PHP之前先做好ldap庫的軟連結,否則--with-ldap參數會導致configure和make失敗.
32位:
sudo ln -s /usr/lib/i386-linux-gnu/libldap.so /usr/lib/
sudo ln -s /usr/lib/i386-linux-gnu/liblber.so /usr/lib/
64位:
sudo ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/
configure-php.sh
#!/bin/bash
./configure \
--prefix=/png/php/5.4.31 \
--enable-fpm \
--enable-pdo \
--enable-sockets \
--enable-exif \
--enable-soap \
--enable-ftp \
--enable-wddx \
--enable-pcntl \
--enable-soap \
--enable-bcmath \
--enable-mbstring \
--enable-dba \
--enable-gd-native-ttf \
--enable-zip \
--enable-calendar \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-iconv \
--with-zlib \
--with-bz2 \
--with-gettext \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-mcrypt \
--with-xsl \
--with-curl \
--with-pcre-regex \
--with-gd \
--with-freetype-dir=/usr \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-ldap \
--with-pear \
--with-fpm-user=png \
--with-fpm-group=png \
--with-apxs2=/png/httpd/2.4.10/bin/apxs
成功產生 Makefile 後便可以執行 make && make install 編譯安裝.
編譯好PHP後,會自動在/png/httpd/2.4.10/conf/httpd.conf中載入PHP模組,不用手動添加:
LoadModule php5_module modules/libphp5.so
安裝常用擴充:
/png/php/5.4.31/bin/pecl install ZendOpcache-7.0.3 xdebug memcache redis
PHP設定檔php.ini:
cp /png/src/php-5.4.31/php.ini-* /png/php/5.4.31/lib/
cp /png/php/5.4.31/lib/php.ini-development /png/php/5.4.31/lib/php.ini
在/png/php/5.4.31/lib/php.ini末尾加入:
;zend_extension=/png/php/5.4.31/lib/php/extensions/no-debug-non-zts-20100525/opcache.so
;opcache.memory_consumption=128
;opcache.interned_strings_buffer=8
;opcache.max_accelerated_files=4000
;;每60秒驗證php檔案時間戳記是否更新
;;opcache.revalidate_freq=60
;opcache.fast_shutdown=1
;opcache.enable_cli=1
;;關閉PHP檔案驗證
;opcache.validate_timestamps=Off
;;設定不緩衝的黑名單
;opcache.blacklist_filename=/png/www/blacklist
;;預設opcache是開啟的,對應phpinfo()裡Zend OPcache下的Master Value值.
;opcache.enable=On
;使用權限設定 chmod 777 /png/xdebug
;使用nginx+php-fpm時要在php-fpm.conf裡配置request_terminate_timeout,使用httpd+php時要在php.ini裡配置max_execution_time,以免Debug逾時.
;xdebug(php-fpm)會串連Netbeans或Eclipse監聽的9001連接埠進行偵錯工作階段(session) 執行命令可見: sudo lsof -i :9001 或 sudo netstat -antp|grep 9001
;Netbeans在進行Xdebug調試時才會監聽9001連接埠,不調試時是不監聽這個連接埠的.
zend_extension=/png/php/5.4.31/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
xdebug.remote_enable = on
xdebug.remote_handler = dbgp
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9001
xdebug.remote_log="/png/xdebug/xdebug.log"
;可以只開分析器profiler
;xdebug.profiler_enable = 1
;xdebug.profiler_output_dir = "/png/xdebug/"
extension=memcache.so
extension=redis.so
把 date.timezone 的值設為 Asia/Shanghai
PHP-FPM設定檔和服務管理指令碼:
cp /png/php/5.4.31/etc/php-fpm.conf.default /png/php/5.4.31/etc/php-fpm.conf
cp /png/src/php-5.4.31/sapi/fpm/init.d.php-fpm /png/src/php-5.4.31/png-fpm
PHP-FPM預設監聽9000連接埠進行通訊,也可以改用Unix Sock通訊:
把 listen = 127.0.0.1:9000 改為 listen = /tmp/php-fpm.sock
並開啟:
listen.owner = png
listen.group = png
listen.mode = 0660
編譯時間對比:
time nice -20 make -j4 (Ubuntu 14.04 使用 i5-3230M 編譯 Nginx 1.6.0 耗時 14 秒, 編譯過程中 CPU 空閑值幾乎為 0)
time nice -20 make (Ubuntu 14.04 使用 i5-3230M 編譯 Nginx 1.6.0 耗時 32 秒, 編譯過程中 CPU 空閑值在 73% 左右)
time nice -20 make (Ubuntu 14.04 使用 i5-3230M 編譯 Apache 2.4.10 耗時 2 分鐘 20 秒, 編譯過程中 CPU 空閑值在 73% 左右)
time nice -20 make (Ubuntu 14.04 使用 i5-3230M 編譯 PHP 5.4.31 耗時 5 分鐘 50 秒, 編譯過程中 CPU 空閑值在 73% 左右)
像MySQL,Memcached這樣的資料存放區服務我一般使用apt-get安裝,方便快捷:
sudo apt-get install mysql-server memcached