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  ]
相关文章
相关标签/搜索