LNMP源碼安裝配置

nginx-1.6.2+php-5.6.8+mariadb-10.0.14

mariadb10.0.14
請參看:源碼安裝MariaDB10

php5.6.8

.編譯安裝php
A.安裝編譯工具和依賴庫
[root@test1 ~]#yum -y install gcc gcc-c++ libxml2-devel openssl-devel pcre-devel libcurl-devel gd-devel bzip2-devel freetype-devel giflib-devel openjpeg-devel readline-devel libedit-devel

B.編譯php擴展庫
libmcrypt
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt
[root@test1 ~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@test1 ~] # tar -zxvf libmcrypt-2.5.7.tar.gz -C /usr/local/src
[root@test1 ~] # cd /usr/local/src/libmcrypt-2.5.7
[root@test1 libmcrypt-2.5.7] # ./configure && make && make install
[root@test1 ~] #ldconfig

mhash
http://sourceforge.net/projects/mhash/files/
[root@test1 ~] # tar -jxvf mhash-0.9.9.9.tar.bz2 -C /usr/local/src
[root@test1 ~] # cd /usr/local/src/mhash-0.9.9.9/
[root@test1 mhash-0.9.9.9] # ./configure && make && make install
[root@test1 ~] #ldconfig

mcrypt
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic
[root@test1 ~] # wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/mcrypt-2.6.4.tar.gz
[root@test1 ~] # tar -zxvf mcrypt-2.6.4.tar.gz -C /usr/local/src
[root@test1 ~] # cd /usr/local/src/mcrypt-2.6.4
[root@test1 mcrypt-2.6.4] # LD_LIBRARY_PATH=/usr/local/lib ./configure && make && make install
[root@test1 ~] #ldconfig
注意:上面標紅的部分必定不能省,否則會報以下錯誤
checking for libmcrypt - version >= 2.5.0... no
*** Could not run libmcrypt test program, checking why...
*** The test program compiled, but did not run. This usually means
*** that the run-time linker is not finding LIBMCRYPT or finding the wrong
*** version of LIBMCRYPT. If it is not finding LIBMCRYPT, you'll need to set your
*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point
*** to the installed location  Also, make sure you have run ldconfig if that
*** is required on your system
***
*** If you have an old version installed, it is best to remove it, although
*** you may also be able to get things to work by modifying LD_LIBRARY_PATH
***
configure: error: *** libmcrypt was not found

說明:若是提示GD庫找不到,能夠再從新源碼安裝下GD庫 (可選)
libgd
http://libgd.bitbucket.org/
http://autosetup1.googlecode.com/files
[root@test1 ~] # wget http://autosetup1.googlecode.com/files/gd-2.0.35.tar.gz
[root@test1 ~] # tar -zxvf gd-2.0.35.tar.gz -C /usr/local/src/
[root@test1 ~] # cd /usr/local/src/gd-2.0.35/
[root@test1 gd-2.0.35]# ./configure && make && make install
[root@test1 ~] #ldconfig
gd庫很重要,諸如dedeCMS, DiscuzX!,PHPwind等建站框架都須要gd庫支持,而且官方下載的gd庫一直安裝不成功,因此這裏才用了第三方的地址。

C.編譯php-fpm
新建用戶和用戶組
groupadd -r php-fpm
useradd -r php-fpm -g php-fpm

php
http://cn2.php.net/get/php-5.6.8.tar.xz/from/a/mirror
[root@test1 ~] # tar -xvf php-5.6.8.tar.xz -C /usr/local/src
[root@test1 ~] # cd /usr/local/src/php-5.6.8
[root@test1 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/php.d \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--enable-inline-optimization \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-shared \
--enable-opcache \
--enable-bcmath \
--enable-soap \
--enable-zip \
--enable-gd-native-ttf  \
--enable-ftp \
--enable-fpm \
--enable-mbstring \

--enable-calendar \
--enable-dom \
--enable-xml \
--with-pear \
--with-pcre-regex \
--with-curl \
--with-bz2 \
--with-zlib \
--with-gd \
--with-gettext  \
--with-jpeg-dir=/usr/local  \
--with-png-dir=/usr/local  \
--with-iconv-dir=/usr/local  \
--with-freetype-dir=/usr/local  \
--with-libxml-dir=/usr/local  \
--with-readline  \
--with-iconv  \
--with-mcrypt  \
--with-mhash  \
--with-openssl  \
--with-mysql=mysqlnd  \
--with-mysqli=mysqlnd  \
--with-pdo-mysql=mysqlnd  \

--disable-debug \
--disable-fileinfo
[root@test1 php-5.6.8]# make && make install
注意:
編譯出現錯誤
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
這是因爲內存小於1G所致使,解決辦法在./configure加上選項:
--disable-fileinfo     #禁用 fileinfo
--enable-mbstring  #
phpMyadmin4須要mbstring支持,編譯時能夠加上
--enable-gd-jis-conv   #該參數如無特殊需求,請勿添加,可能會致使圖片中文亂碼,請參看 zabbix-2.2.x LTS源碼安裝配置

添加環境變量
 
[root@test1 php-5.6.8]#vim /etc/profile
PATH=$PATH:/usr/local/php/sbin:/usr/local/php/bin
  [root@test1 php-5.6.8]# source /etc/profile


二.配置php-fpm
[root@test1 ~]#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@test1 ~]# cp /usr/local/src/php-5.6.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@test1 ~]# chmod +x /etc/init.d/php-fpm

[root@test1 php]# /etc/init.d/php-fpm start
Starting php-fpm  done
[root@test1 php]#netstat -tunlp|grep php
tcp            0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      32468/php-fpm
[root@test1 ~]# chkconfig php-fpm on
[root@test1 ~]# chkconfig --list php-fpm
php-fpm            0:off    1:off    2:on    3:on    4:on    5:on    6:off


補充:額外的php.ini文件也能夠在源碼裏找到,有須要能夠加上
[root@test1 ~]#  cp /usr/local/src/php-5.6.8/php.ini-production /usr/local/php/etc/php.ini

LNMP源碼安裝配置


nginx1.6.2
官方網站:
http://nginx.org/
http://nginx.org/download/nginx-1.6.2.tar.gz
https://openresty.org/en/benchmark.html
https://github.com/leancloud/resty-marathon-lb/blob/master/openresty/build-and-install.sh
ab -k -c10 -n10000 -t1 -r 'http://127.0.0.1:8080/
另參見: Nginx-1.x.x源碼自動安裝配置(CentOS6)

一.安裝編譯工具和依賴庫
[root@test1 ~]# yum -y install gcc gcc-c++ autoconf automake make zlib zlib-devel openssl openssl-devel pcre-devel  libxml2-devel libxslt-devel perl-ExtUtils-Embed

二.下載,編譯安裝
[root@test1 ~]#wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@test1 ~]#tar -zxvf nginx-1.6.2.tar.gz  -C /usr/local/src/
[root@test1 ~]#cd /usr/local/src/nginx-1.6.2
[root@test1 nginx-1.6.2]#./configure      \
 
--prefix=/usr/local/nginx                                          \
 
--sbin-path=/usr/sbin/nginx \
 
--conf-path=/etc/nginx/nginx.conf \
 
--error-log-path=/var/log/nginx/error_log      \
 
--pid-path=/var/run/nginx.pid                \
 
--lock-path=/var/lock/subsys/nginx \
 
--user=nginx                                            \
 
--group=nginx                                          \
 
--with-http_ssl_module                        \
 
--with-http_realip_module                  \
 
--with-http_addition_module              \
 
--with-http_xslt_module                      \
 
--with-http_sub_module                        \
 
--with-http_dav_module                        \
 
--with-http_flv_module                        \
 
--with-http_mp4_module                        \
 
--with-http_gzip_static_module        \
 
--with-http_random_index_module      \
 
--with-http_secure_link_module        \
 
--with-http_degradation_module        \
 
--with-http_stub_status_module        \
 
--http-log-path=/var/log/nginx/access_log                  \
 
--http-client-body-temp-path=/var/tmp/nginx/client \
 
--http-proxy-temp-path=/var/tmp/nginx/proxy              \
 
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi          \
 
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi          \
 
--http-scgi-temp-path=/var/tmp/nginx/scgi           
[root@test1 nginx-1.6.2]#make && make install

三.修改配置文件/etc/nginx/nginx.conf
[root@test1 ~]# cat /etc/nginx/nginx.conf

#user  nobody;
worker_processes  8; #工做進程數

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  30000; #最大併發鏈接數
}


http {
    include       mime.types;
    default_type  application/octet-stream;
   
    open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m; #meta數據緩存
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;


#壓縮功能
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types *;
    gzip_vary on;
   

#反向代理
    client_max_body_size 300m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 60s;
    proxy_read_timeout 60s;
    proxy_send_timeout 60s;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;

    proxy_temp_path /var/tmp/nginx/proxy_temp 1 2;
    proxy_cache_path /var/tmp/nginx/proxy_cache levels=1:2 keys_zone=one:200m inactive=1d max_size=1g;


#負載均衡
   upstream my_server_pool_1 {
   server server103.jlive.com:8080 weight=1 max_fails=2 fail_timeout=30s;
   server www.jlive.com weight=1 max_fails=2 fail_timeout=30s;
   }
   upstream my_server_pool_2 {
   server 192.168.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
   server 192.168.0.2:8080 weight=2 max_fails=2 fail_timeout=30s;
   }



    server {
        listen       80;
        server_name  www.jlive.com;

        #charset koi8-r;

        access_log  /var/log/nginx/www.jlive.com_access main buffer=32k;
        error_log  /var/log/nginx/www.jlive.com_error warn;

    root /usr/local/nginx/html;
        index  index.php index.html index.htm;


#啓用反向代理緩存
       location /sms {
       proxy_pass http://server103.jlive.com:8080;
       proxy_cache one;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_cache_valid 200 10m;
       proxy_cache_valid 304 1m;
       proxy_cache_valid 301 302 1h;
       proxy_cache_valid any 1m;
       }
       location /ftp {
       proxy_pass http://www.jlive.com;
       proxy_cache one;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_cache_valid 200 10m;
       proxy_cache_valid 304 1m;
       proxy_cache_valid 301 302 1h;
       proxy_cache_valid any 1m;
       }
       location ~* .*\.(gif|jpg|jpeg|png|bmp|swf|flv|js|css|html) {
       proxy_cache one;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_cache_key $host$uri$is_args$args;
       proxy_cache_valid 200 10m;
       proxy_cache_valid 304 1m;
       proxy_cache_valid 301 302 1h;
       proxy_cache_valid any 1m;
       }



#密碼認證,下載限速,簡單的訪問控制
   location /data {
       autoindex on;
       auth_basic "請輸入用戶名&密碼"; #基本密碼認證(htpasswd -cm ...)
       auth_basic_user_file /etc/nginx/.htpasswd;
       limit_rate_after 20m; #前20m不限速
       limit_rate 300k;
       allow 192.168.0.1
       deny 192.168.0.2
   }


#別名
      location /iso {
           alias /var/www/pub/iso;
       }


#nginx狀態
      location /nginx_status {
       stub_status on;
       access_log off;
       auth_basic "請輸入用戶名&密碼"; #基本密碼認證(htpasswd -cm ...)
       auth_basic_user_file /etc/nginx/.htpasswd;
       }


#地址重寫
   if ($http_user_agent ~ MSIE) {
       rewrite ^(.*)$ /msie/$1 last;
   }
   if (! -f $request_filename) {
       rewrite ^/file/(.*)$ /site/$host/img/$1 last;
   }
   if ($host="www.abc.com") {
       rewrite ^/(.*)$ https://web.example.com permanent;
   }
   rewrite ^/data/$ /bbs/ permanent;


#防盜鏈
   location ~* .*\.(gif|jpg|jpeg|png|bmp|swf|flv)$ {
   valid_referers none blocked www.example.com *.example.com;
   if ($invalid_referer) {
       rewrite ^/(.*)$ http://www.example.net/block.html;
   }


#return指令
   location ~* .*\.(sh|bash)$ {
       return 403;
   }


#瀏覽器緩存
    location ~* .*\.(gif|jpg|jpeg|png|bmp|swf|flv)$ {
    expires 30d;
    }
    location ~ .*\.(js|css)$ {
    expires 1h;
    }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/nginx/error;
        }
        error_page   403  /403.html;
        location = /403.html {
            root   /usr/local/nginx/error;
        }
        error_page   404  /404.html;
        location = /404.html {
            root   /usr/local/nginx/error;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
          proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


        # pass the PHP scripts to Unix Socket /dev/shm/php-fpm.sock
    #location ~ \.php$ {
    #fastcgi_pass unix:/tmp/php-fpm.sock;
    #fastcgi_index index.php;
    #include fastcgi.conf;
    #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
          deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
      listen       8000;
      listen       somename:8080;
      server_name  somename  alias  another.alias;
      root   /usr/local/nginx/www;
      index  index.php index.html index.htm;
    #}


    # HTTPS server
    #
   server {
       listen       443;
       server_name  web101.jlive.com;
#
       ssl                  on;
       ssl_certificate      /etc/pki/tls/certs/localhost.crt;
       ssl_certificate_key  /etc/pki/tls/private/localhost.key;
#
       ssl_session_timeout  5m;
#
       ssl_protocols  SSLv2 SSLv3 TLSv1;
       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers   on;
#
    root /var/www/server/ssl;
       index  index.php index.html index.htm;
   }

}
上面標紅的部分,就是把php動態解釋語言轉交給127.0.0.1:9000,也就是php-fpm來解釋。


四.啓動nginx
[root@test1 ~]# /etc/init.d/nginx start
Starting nginx:                                                                                      OK  ]
[root@test1 ~]# netstat -tlnp|grep nginx
tcp                      0 0.0.0.0:80                                  0.0.0.0:*                                    LISTEN          20857/nginx               
[root@test1 ~]# chkconfig nginx on
[root@test1 ~]# chkconfig --list nginx
nginx                        0:off      1:off      2:on      3:on      4:on      5:on      6:off
提示:/etc/init.d/nginx是經過rpm包安裝好nginx之後提取出來加以修改而成。

五.測試
經過phpinfo()測試函數來測試nginx+php-fpm是否正常運行 localhost/index.php
[root@test1 html]# pwd
/usr/local/nginx/html
[root@test1 html]# ls
403.html  404.html  50x.html  index.html  index.php




php-fpm socket優化加速
在服務器壓力不大的狀況下,tcp和socket差異不大,但在壓力比較滿的時候,用套接字方式,效率更高速度更快
 Unix domain socket 或者 IPCsocket 是一種終端,能夠使同一臺操做系統上的兩個或多個進程進行數據通訊。與管道相比,Unix domain sockets 既能夠使用字節流數和數據隊列,而管道通訊則只能經過字節流。Unix domain sockets的接口和Internet socket很像,但它不使用網絡底層協議來通訊。Unix domain socket 的功能是POSIX操做系統裏的一種組件。 Unix domain sockets 使用系統文件的地址來做爲本身的身份。它能夠被系統進程引用。因此兩個進程能夠同時打開一個Unix domain sockets來進行通訊。不過這種通訊方式是發生在系統內核裏而不會在網絡裏傳播。

1.修改php-fpm.conf
[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
;listen = 127.0.0.1:9000
listen = /tmp/php-fpm.sock
#註銷原來監聽9000的tcp方式改用socket方式
[root@localhost ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost ~]# ll /tmp/php-fpm.sock
srw-rw----. 1 root root 0 Apr 27 22:38 /tmp/php-fpm.sock
[root@localhost ~]# chmod 666 /dev/shm/php-fpm.sock
提示:能夠將php-fcgi.sock放在/dev/shm目錄,/dev/shm是tmpfs,速度比磁盤快默認狀況下php-fpm每次重啓都會從新生成一個新的socket,因此必定要給足權限,否則會報錯,固然能夠經過修改/etc/init.d/php-fpm來自動修改權限,如,能夠在init腳本的start段末尾加上權限修改的命令從而避免每次重啓都手動修改權限的麻煩。
           start)
                echo -n "Starting php-fpm "
...
               chmod 666 /dev/shm/php-fpm.sock &>/dev/null
               ;;

實際上,php-fpm.conf配置文件中能夠經過指定listen.mode來直接設置php-fpm.sock的權限php

listen.owner = php-fpmcss

listen.group = php-fpmhtml

listen.mode = 0666mysql

2.修改nginx.conf
[root@localhost ~]# vim /etc/nginx/nginx.conf
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; }
在server{ }段將http的方式改成socket方式 [root@localhost ~]# /etc/init.d/nginx restart Stopping nginx:                                           [  OK  ] Starting nginx:                                           [  OK  ]
相關文章
相關標籤/搜索