LNMP環境搭建以及調優

  LNMP是linux、nginx、mysql、php組成的一個運行生產環境,現將本身在學習的過程當中搭載lnmp環境的過程整理出來。
javascript

  服務器版本是CentOS6.5.Mysql版本是5.1.73;nginx版本是nginx-1.6.3;php的版本是php-5.4.37。lnmp的搭建其實和lamp的搭建很相似,在mysql的安裝上,兩者幾乎一致,因此這裏再也不贅述。php

1、PHP的安裝css

 這裏要先聲明一下,針對Nginx的php安裝和針對apache的php安裝是有區別的,由於Nginx中的php是以fastcgi的方式結合nginx的,能夠理解爲nginx代理了php的fastcgi,而apache是把php做爲本身的模塊來調用的。html

  1. 在/usr/local/src下,解壓進入源碼包目錄下,進行編譯:  java

./configure \

--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-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 \
--enable-zend-multibyte \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl

紅色部分要注意,因爲以前在服務器端搭建lamp時,php安裝的目錄是/usr/local/php/,這裏再安裝時須要和lamp的php路徑有所區別,不然會覆蓋掉lamp的php安裝文件。node

2.make&&make installmysql

3.拷貝配置文件linux

 cp /usr/local/src/php-5.4.37/php.ini-production   /usr/local/php-fpm/etc/php.ini   nginx

4.拷貝啓動腳本sql

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

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

 chkconfig --add php-fpm

 chkconfig php-fpm on

將php-fpm加入到開機啓動項裏,此時運行service php-fpm start會報錯,因此咱們還要進行以下配置.

5.添加php用戶

 cd /usr/local/php-fpm/etc/

 mv php-fpm.conf.default php-fpm.conf

 useradd -s /sbin/nologin php-fpm

運行service php-fpm start,顯示以下:

Gracefully shutting down php-fpm . done

Starting php-fpm  done

至此,php安裝配置過程完成。

2、安裝nginx

  1. 編譯安裝

    在/usr/local/src下,解壓,進入源碼包目錄。

    編譯以前,先安裝一個pcre-devel的包:yum install -y pcre-devel

    ./configure --prefix=/usr/local/nginx --with-pcre  編譯安裝

    make 

    make install

    直接輸入/usr/local/nginx/sbin/nginx,nginx啓動。

  2. 測試php解析

    此時的nginx還不解析php,因此咱們要修改一下nginx的配置文件

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

    刪除下列的註釋,而且更改紅字部分

 location ~ \.php$ {

   root           html;

   fastcgi_pass   127.0.0.1:9000;

   fastcgi_index  index.php;

   fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

   include        fastcgi_params;

    }

  顯示頁面在/usr/local/nginx/html下

  /usr/local/nginx/sbin/nginx -t  #檢查配置文件有無錯誤

  /usr/local/nginx/sbin/nginx -s reload  #從新裝載配置文件

 此時,在/usr/local/nginx/html下寫一個php文件,在瀏覽器或是用curl能夠看到nginx已經解析php

3.編寫nginx啓動文件

nginx和apache不太同樣,須要本身去寫啓動腳本。

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

編輯完成後,保存退出

chmod 755  /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

將nginx加入到開機啓動項裏去,運行nginx方式能夠採用service nginx start

service nginx restart 重啓nginx

service nginx reload 從新加載nginx配置文件

service nginx stop  中止nginx

service nginx configtest 檢查nginx配置文件有無錯誤

3、nginx配置調優

1.配置主配置文件

nginx的主配置文件路徑是/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;
    
}

紅色部分是將vhosts下的配置文件都包括進去,和apache相似,主配置文件和虛擬主機文件不寫在一個文檔裏面。因此咱們還要再去配置一下虛擬主機。

2.配置虛擬主機

根據主配置文件

cd /usr/local/nginx/conf

mkdir vhosts

cd vhosts

vim default.conf

default.conf文件是默認虛擬主機的配置文件,爲了安全性的考慮,咱們應該將默認的虛擬主機配置成爲訪問403.

寫入:

server

{

    listen 80 default_server;

    server_name localhost;

    index index.html index.htm index.php;

    root /tmp/123            #這個路徑能夠下能夠不寫入任何內容,因此默認的虛擬主機                            解析均是403,保證安全性

    deny all;

}

mkdir /tmp/1233

此時輸入本身的網站域名,返回403.

下面要爲本身的網站進行配置,首先新建一個配置文件vim /usr/local/nginx/conf/111.conf   #111是自定義的

寫入:

server

{

    listen 80;

    server_name 111.com;      #網站的域名

    index index.html index.htm index.php;

    root /data/www;            #網站的根目錄

    location ~ \.php$ {

        include fastcgi_params;

        #fastcgi_pass unix:/tmp/php-fcgi.sock;   #不適用socket的形式

        #fastcgi_pass 127.0.0.1:9000;         #使用ip加端口的形式

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; #改爲網站的根目錄

    }

這裏用socket仍是ip加端口的方式,應該和php-fpm裏配置文件相一致。從新加載nginx配置,此時在瀏覽器裏輸入網站域名,成功顯示界面。

4、php-fpm配置調優

們要注意一下兩個配置文件的不一樣

/usr/local/php-fpm/etc/php-fpm.conf     #php服務所使用的的配置文件

/usr/local/php-fpm/etc/php.ini        #php的全局配置文件

將 /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/www.sock   能夠自定義

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

slowlog=/tmp/www_slow.log

request_slowlog_timeout=1     #將腳本執行超過1秒的記錄在路徑下,以備調優

php_admin_value[open_basedir]=/data/www/:/tmp/  #針對php的解析,和apache同樣設置openbasedir


此處定義的listen = /tmp/www.sock 要和nginx裏的虛擬主機配置文件相一致。

 #fastcgi_pass unix:/tmp/www.sock;


此外還能夠再加入一個pool的模塊

[www1]

listen = /tmp/www1.sock

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


listen段的sock要有區別,這樣的話不一樣的網站可使用不一樣的sock。

相關文章
相關標籤/搜索