適用環境
: 64位Ubuntu14.04
下載地址(
22MB
) :
http://pan.baidu.com/s/1o6FlEUQ
md5sum png.tar.xz aaa62279d036c3248fa503ce6e3cb87f
解壓即用,跨Linux發行版
PHP7 打包
http://my.oschina.net/eechen/blog/411534
解壓即用,跨Linux發行版
HHVM 打包
http://my.oschina.net/eechen/blog/371643
安裝依賴包:
sudo apt-get -y install \
build-essential \
autoconf \
libtool \
libxml2-dev \
openssl \
libcurl4-openssl-dev \
libbz2-dev \
libjpeg-dev \
libpng12-dev \
libfreetype6-dev \
libldap2-dev \
libmcrypt-dev \
libmysqlclient-dev \
libxslt1-dev \
libxt-dev \
libpcre3-dev \
libreadline-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
解壓到目錄:
xz -d png.tar.xz && tar xf png.tar -C /
幾個程序對應的目錄:
/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 編譯安裝.
mkdir -p /png/nginx/1.6.0/var/cache/nginx
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 10
文件2: os/unix/os.h
#define PLATFORM "Unix" 其中Unix能夠改成GNU/Linux
到 http://apr.apache.org/download.cgi 下載 apr 和 apr-util, 而後把它們解壓到 httpd 的 srclib 目錄:
/png/src/httpd-2.4.10/srclib/apr
/png/src/httpd-2.4.10/srclib/apr-util
注意 srclib 下的 apr 和 apr-util 不要保留版本號.
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
# 設置Prefork MPM進程數
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失敗.
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/
sudo ln -s /usr/lib/x86_64-linux-gnu/libXpm.so /usr/lib/
sudo ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
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/
sudo ln -s /usr/lib/i386-linux-gnu/libXpm.so /usr/lib/
sudo ln -s /usr/include/i386-linux-gnu/gmp.h /usr/include/gmp.h
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-gd-jis-conv \
--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-gmp \
--with-pspell \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-mcrypt \
--with-xsl \
--with-curl \
--with-pcre-regex \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-xpm-dir=/usr \
--with-freetype-dir=/usr \
--with-gettext=/usr \
--with-zlib=/usr \
--with-bz2=/usr \
--with-recode=/usr \
--with-ldap \
--with-pear \
--with-readline \
--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/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,Redis這樣的數據存儲服務我通常使用apt-get安裝,方便快捷: sudo apt-get install mysql-server memcached redis-server