0514第二十次課:LNMP 1

1、LNMP架構介紹

  • LNMP和LAMP相識,只不過web服務由apache換成了nginx。javascript

  • 而且php是做爲一個獨立服務存在的,這個服務叫作php-fpmphp

  • Nginx直接處理靜態請求,動態請求會轉發給php-fpmcss

    在lamp中php做爲一個模塊,在lnmp中php是做爲一個服務。當用戶請求時nginx會把它交給php而後與mysql進行交互。靜態文件,如htmp或者圖片nginx會直接處理從而加快速度。html

2、mysql安裝

因爲以前已安裝過mysql,這裏就略過。java

3、PHP安裝

  1. 下載安裝包node

    cd /usr/local/src/php-5.6.30mysql

  2. 清理以前編譯的可執行文件及配置文件nginx

    make clear web

  3. 編譯安裝sql

    執行如下語句:

    ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl

    開始編譯:

    make

    make install

    檢查是否報錯:

  4. 查看安裝路徑

    ll /usr/local/php-fpm/

    比以前安裝多了sbin和var兩個目錄:

    sbin:啓動php-fpm服務目錄

    var: 存放php日誌的目錄

  5. 配置php.ini

    在解壓安裝包的目錄中複製php.ini-production文件到php-fpm/etc文件中,重命名爲php.ini

    cp php.ini-production /usr/local/php-fpm/etc/php.ini

  6. 配置php-fpm.conf

    vi /usr/local/php-fpm/etc/php-fpm.conf

    寫入如下內容:

    [global]
     pid = /usr/local/php-fpm/var/run/php-fpm.pid
     error_log = /usr/local/php-fpm/var/log/php-fpm.log
     [www]
     listen = /tmp/php-fcgi.sock
     listen.mode = 666
     user = php-fpm
     group = php-fpm
     pm = dynamic
     pm.max_children = 50
     pm.start_servers = 20
     pm.min_spare_servers = 5
     pm.max_spare_servers = 35
     pm.max_requests = 500
     rlimit_files = 1024
  7. 設置啓動腳本

    把安裝解壓包中的init.d.php-fpm文件複製到/etc/init.d/並更名爲php-fpm

    cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

    賦予775權限

    chmod 755 /etc/init.d/php-fpm

  8. 啓動php

    service php-fpm start

    查看php進程

    ps -ef|grep php-fpm

4、Nginx

  • Nginx簡介

    Nginx官網:nginx.org

    Nginx應用場景:web服務、反向代理、負載均衡

    Nginx分支:淘寶基於nginx開發的Tengine,使用與nginx一致。區別在於Tengine增長了一些定製化模塊,在安全限速方面表現突出,另外它支持對接js/css合併。Nginx+lua相關組件和模塊組成支持lua的高性能openresty,參考: http://jinnianshilongnian.iteye.com/blog/2280928

  • Nginx特色

    • 體積小

    • 處理能力強

    • 併發高

    • 可擴展性高


  • 安裝Nginx
  1. 下載nginx安裝包

    cd /usr/local/src

    wget http://nginx.org/download/nginx-1.17.0.tar.gz

  2. 編譯安裝

    cd /usr/local/src/nginx-1.17.0

    ./configure --prefix=/usr/local/nginx

    make && make install

  3. 查看nginx安裝目錄

    conf:nginx配置文件

    html:主頁樣例文件

    logs:日誌

    sbin:核心進程文件


  • Nginx啓動配置文件

    vim /etc/init.d/nginx

    寫入如下內容:

    #!/bin/bash
    # chkconfig: - 30 21
    # description: http service.
    # Source Function Library
    . /etc/init.d/functions
    # Nginx Settings
    
    NGINX_SBIN="/usr/local/nginx/sbin/nginx"
    NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
    NGINX_PID="/usr/local/nginx/logs/nginx.pid"
    RETVAL=0
    prog="Nginx"
    
    start() 
    {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    stop() 
    {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    reload()
    {
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    restart()
    {
        stop
        start
    }
    
    configtest()
    {
        $NGINX_SBIN -c $NGINX_CONF -t
        return 0
    }
    
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      reload)
            reload
            ;;
      restart)
            restart
            ;;
      configtest)
            configtest
            ;;
      *)
            echo $"Usage: $0    {start|stop|reload|restart|configtest}"
          RETVAL=1
    esac
    
    exit $RETVAL

  • 啓動nginx

    修改權限爲755

    chmod 755 /etc/init.d/nginx

    設置開機啓動

    chkconfig --add nginx

    chkconfig nginx on

    啓動/關閉/檢查

    啓動:

    /usr/local/nginx/sbin/nginx service nginx start

    關閉:

    pkill nginx //殺死nginx進程,中止nginx服務

    檢查:

    /usr/local/nginx/sbin/nginx -t //檢測配置文件語法錯誤

    啓動並查看進程

    service nginx start


  • 修改nginx配置文件

    nginx配置文件:

    vim /usr/local/nginx/conf/nginx.conf

    添加如下內容:

    user nobody nobody;
    worker_processes 2;
    error_log /usr/local/nginx/logs/nginx_error.log crit;
    pid /usr/local/nginx/logs/nginx.pid;
    worker_rlimit_nofile 51200;
    events
    {
        use epoll;
        worker_connections 6000;
    }
    http
    {
        include mime.types;
        default_type application/octet-stream;
        server_names_hash_bucket_size 3526;
        server_names_hash_max_size 4096;
        log_format combined_realip '$remote_addr    $http_x_forwarded_for [$time_local]'
     ' $host "$request_uri" $status'
     ' "$http_referer" "$http_user_agent"';
     sendfile on;
     tcp_nopush on;
     keepalive_timeout 30;
     client_header_timeout 3m;
     client_body_timeout 3m;
     send_timeout 3m;
     connection_pool_size 256;
     client_header_buffer_size 1k;
     large_client_header_buffers 8 4k;
     request_pool_size 4k;
     output_buffers 4 32k;
     postpone_output 1460;
     client_max_body_size 10m;
     client_body_buffer_size 256k;
     client_body_temp_path /usr/local/nginx/client_body_temp;
     proxy_temp_path /usr/local/nginx/proxy_temp;
     fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
     fastcgi_intercept_errors on;
     tcp_nodelay on;
     gzip on;
     gzip_min_length 1k;
     gzip_buffers 4 8k;
     gzip_comp_level 5;
     gzip_http_version 1.1;
     gzip_types text/plain application/x-javascript text/css text/htm 
     application/xml;
     server
     {
         listen 80;
         server_name localhost;
         index index.html index.htm index.php;
         root /usr/local/nginx/html;
         location ~ \.php$ 
         {
             include fastcgi_params;
             fastcgi_pass unix:/tmp/php-fcgi.sock;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
            }    
        }	
    }

  • 測試解析PHP

    vim /usr/local/nginx/html/1.php

    添加如下內容:

    <?phpecho "hello";

    測試:

    curl localhost/1.php

相關文章
相關標籤/搜索