LNMP構築

21896 ワード

1、Mysqlを取り付ける(LAMP参照)2、PHPをインストールしてPHP公式サイトをダウンロードします。http://www.php.net/downloads.php cd/usr/local/src wgethttp://am1.php.net/distributions/php-5.3.27.tar.gz 解圧PHP tar zxf php-5.7.tar.gz作成関連アカウントuseradd-s/sbin/nologin php-fpm構成コンパイルパラメータ./configre\prefix=/usr/local/php-fm\–with-file-fith=/usr/local/pcenable-fpm\–with-fpm-user=php-fpm\–with-fpm-group=php-fpm\–with-msql=/usr/local/msql\––with- msql-sock=/tmp/msql.sock/mmmysql.sock–––withdidididiggggggggggggggggggggggggggggggggggtttttttttttttttttttttttttttttttttttttttttttttttttv-dir\–with-zlib-dir\–with-mcrypt\–enabl-soap\–enabl-gd-native-ttf\enabele- ftp\–enabele- mbstring\–enabee-exif–disable-mipv 6–with-pear\–with- curl\–with- opensslエラー情報:configure:Please reinstall the libcurl distristristriristristristristristristststration-eaease-sususususususususususususususususususususyyyyyyysssssssssyyyclclclclclclclclclclclclclclclclcleeeeeeeeeeeyyyymmmmmmmmcollect 2:ld returned 1 exit status make:*[sapi/fpm/php-fpm]エラー1解決方法yum install-y libtool-ltdl-devel PHP makeinstall修正プロファイルコピーphpのグローバルプロファイルcp.ini-productctio/usr/local/php-fpm/etc/php/php.ini修正PHP-FPMのプロファイルvim/usm/usr/local/local/phphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphphph起動PHP-fpm cp/usr/local/src/php-53.27/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm chmod 755/etc/init/d/php-fpm service php-fm startを起動させたいなら、pgrap-fp-fm firtを実行します。ダウンロードhttp://nginx.org cd/usr/local/src/wgethttp://nginx.org/download/nginx-1.4.4.tar.gz 解凍tar zxvf nginx-1.4.tar.gz構成コンパイルパラメータcd nginx-1.44.realip_module\–with-http_sub_module\–with-http_gzip_static.module\–with-http_stubstatusmodule\–with-pcre make&make installがnginxを書いてシナリオをスタートさせて、そしてシステムサービスvim/etc/init.d/inininxに参加します。
#!/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/inx chkconfig–add nginx
起動したいなら、実行してください。chkconfig nginx on
nginxのプロファイルを変更するには、まず元のプロファイルをクリアします。
/usr/local/nginx/conf/nginx.co nf vim/usr/local/nginx/conf/nginx.co nf
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;
    }

}

}
修正後に保存し、問題があるかどうかを検出する/usr/local/nginx/sbin/nginx-t内容が以下のように表示されたら、構成は正しいです。そうでないと、エラーメッセージに基づいてプロファイルを修正する必要があります。
起動nginx:service nginx startが起動できない場合は、「/usr/local/nginx/logs/error.log」ファイルを確認して、nginxが起動されているかどうかを確認してください。ps aux grep nginx
PHPファイルを解析してテストファイルを作成するかどうか:vim/usr/local/nginx/html/2.php内容は以下の通りです。

    echo "  php    ";
?>
テスト:phpが解析されているかをテストします。root@localhostnginx](铉はこのように表示して、やっとphp解析が正しいと説明します。
Nginx構成1、デフォルトの仮想ホストは、主構成ファイルnginx.com nfを修正し、終了記号にinclude vhost/*.confを加えて、という意味ですusr.local/nginx/conf/vhostのすべては.confで終わるものがロードされます。このように、すべての仮想ホストの構成を/usr.local/nginx/conf/vhostの下でmkdir/usr.local/nginx/conf/vhost cd/usr.local/nginx/conf/vhost vim default.co nfにします。
server
{
    listen 80 default_server;  //              
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/nginx/default;
}
検出/usr/local/nginx/sbin/nginx-t/usr/local/nginx/sbin/nginx-s reload echo'''123123''/data/nginx/default/index.htmlテストcurl-x 127..0.0:80 aa.com/新規ユーザ認証作成/新規ホスト
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;

location  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
httpasswd-c/local/nginx/conf/httpasswd ming検出/usr/local/nginx/sbin/nginx-t/local/nginx/sbin/inx-s reload mdir/data/nginx/test.com
テストcurl-x 127..0.0:80 aa.com 3、ドメインリダイレクトは仮想ホストに設定されます。
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;

    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }

}
server_nameの後は複数のドメイン名との接続ができます。permantは永久で、検出/usr/local/nginx/sbin/nginx-t/usl/local/nginx/sbin/inx-s reload curl-x 127..1:80 test 1.com/123.txt-I 4.アクセスログフォーマットはnginx.comの中で定義されます。フォーマットから仮想ホストプロファイルにアクセスログのパスを指定します。
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;

    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    access_log /tmp/1.log combined_realip;
}
access_ロゴ(指定パス)/tmp/1.log cobined_realip;(ログフォーマットの名前)ログカットツールは持っていません。
システムを自動的に切断するログを設定します。
#! /bin/bash
##   nginx        /data/logs/
d=`date -d "-1 day" +%Y%m%d` 
logdir="/data/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`


##add cron
#0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
5.静的ファイルはログを記録しないように設定し、有効期限を追加します。
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;

    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
    access_log /tmp/1.log combined_realip;
}
locationを使用すると、対応する静的ファイルを指定できます。expires設定の有効期限が切れます。access_ログオフでアクセスログを記録しないことができます。
6、Nginx防犯チェーン
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
    expires 7d;
    valid_referers none blocked server_names  *.test.com ;
    if ($invalid_referer) {
        return 403;
    }
    access_log off;
}
7、アクセス制御
location /admin/
{
    allow 192.168.188.1;
    allow 127.0.0.1;
    deny all;
}

location /admin/
{
    deny 192.168.188.1;
    deny 127.0.0.1;
}

location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}
マッチングがあれば8.Ngix解析PHP test.com nfの中に以下のように配置されています。
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/nginx/test.com;

    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }

    location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
    }
    access_log /tmp/1.log combined_realip;
}
fastcgi_php-fpmのアドレスをパスして指定します。tcp:portとしてもいいです。例えば127.1.0.1:9000 fastcgi_パスはphp-fpmのアドレスと一致します。fastcgi_param SCRIPT_FILENAMEの後ろにあるのはこのサイトのルートディレクトリです。前に定義されたrootパスと一致するはずです。そうでないと、phpアクセスは404 9、Ngixエージェントcd/usr/local/nginx/conf/vhost vim proxy.co nf、`server{listen 80;serverame ask.apple.comが現れます。
location /
{
    proxy_pass      http://121.201.9.155/;
    proxy_set_header Host   $host;
    proxy_set_header X-Real-IP      $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}検出/usr/local/nginx/sbin/nginx-t/usr/local/nginx/sbin/nginx-s reload
curl-x 127..0.1:80 ask.apple.com-I
nginx構成負荷等化cd/usr/local/nginx/conf/vhost vim load.com nf
upstream qq_com
{
    ip_hash;
    server 61.135.157.156:80;
    server 125.39.240.113:80;
}

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

    location /
    {
        proxy_pass      http://qq_com;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
php-fpm構成1 php-fpmのpool php-fpmは複数のpoolをサポートしています。各poolは一つのポートを傍受してもいいし、一つのsocketを傍受してもいいです。php-fpmは一つの変更をして、
[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
user = php-fpm
group = php-fpm
listen.owner=nobody
listen.group=nobody
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

include=etc/php-fp.d/d/*。
[aming]
listen = /tmp/aming.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
php_admin_value[open_basedir]=/data/www/:/tmp/
各poolは一つのポートを傍受し、Nginxの異なるポートで異なるpoolを呼び出して、相互隔離の目的に達するため、二つのpoolは互いに影響しない。
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
3、php-fpmのopenubradiirは、poolに[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
3、php-fpmプロセス管理を追加します。