Nginx安裝與配置(包括解決403錯誤,站點配置,_STORAGE_WRITE_ERROR_,No input file)

1.yum 安裝nginx
nginx -v   #安裝前檢查
yum install nginx -y  #yum安裝nginx
2.啓動nginx並設置開機自啓動
 
systemctl start nginx.service  #啓動nginx
 systemctl stop  nginx.service  #關閉nginx
 systemctl enable nginx.service #設置nginx開機自啓
 systemctl status nginx.service #查看nginx運行狀態
3.安裝完成後即可輸入ip進行訪問,若出現403,檢查是否關閉防火牆
 
systemctl stop firewalld  #關閉防火牆
4.修改nginx配置文件 使其支持php解析,並配置相關參數

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;  #運行用戶
worker_processes auto;  ##啓動進程,通常設置成和cpu的數量相等
error_log /var/log/nginx/error.log; #錯誤日誌位置
pid /run/nginx.pid; #全局錯誤日誌及PID文件

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
worker_rlimit_nofile 51200; #單個後臺worker process進程的最大併發鏈接數

events {
    use epoll;  #僅用於linux2.6以上內核,可以大大提高nginx的性能
    worker_connections 51200;
    multi_accept on;
}

http {
    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  /var/log/nginx/access.log  main;
    charset             utf-8;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        index index.html index.htm index.php;
       # root         /usr/share/nginx/html;    #默認網站根目錄
        root          /home/www;    #自定義網站根目錄 需要給足讀寫權限
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        index index.html index.htm index.php;
        autoindex on;
        }
        location ~ \.php$ {
         #root /usr/share/nginx/html; #指定php的根目錄
         root /home/www;    #自定義網站根目錄 需要給足讀寫權限
         fastcgi_pass 127.0.0.1:9000;#php-fpm的默認端口是9000
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
  include vhosts/*.conf;
}

5.啓動php-fpm(有兩種啓動方式,1.手動切換到目錄下啓動,2.設置開機自啓動)
  a.手動啓動:切換到php-fpm目錄 執行./php-fpm     </usr/sbin/php-fpm>
  b.開機自啓:systemctl enable php-fpm.service 

6.關閉 selinux(selinux開啓可能會導致訪問時出現403)  
  a. 臨時關閉  
setenforce 0

  b. 永久關閉

vi /etc/selinux/config(find / -name selinux)
  將SELINUX=enforcing改爲SELINUX=disabled 
  設置後需要重啓才能生效

7.配置虛擬站點
a.find / -name nginx.conf #找到nginx.conf目錄路徑 </etc/nginx/nginx.conf>
b.在nginx目錄下新建文件夾vhosts
c.切換到vhosts目錄,新建web1.conf,web1.conf配置如下
server {
    listen       80;
    server_name  web1.com;
    root        /home/www/web/;
    index  index.php index.html;


    location / {
        index  index.php index.html;
    }
    location ~ .*\.(php|php5)?$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV development;
        include fastcgi_params;
    }
}
d.並在nginx.conf的server最後添加  include vhosts/*.conf;
e.配置解析,find / -name hosts </etc/hosts> 添加域名及ip,還需在本機hosts中也添加ip地址

8.如果nginx安裝或運行過程中出現 403 錯誤  首先檢查是否具有讀寫權限,如果出現其他錯誤,可使用cat命令查看錯誤日誌進行問題排查 </var/log/nginx/error.log>

9.部署tp框架在Nginx中(或者出現 No input file)

使Nginx支持tp框架支持:common中加入以下配置

return array(
	'URL_MODEL' => '2', //URL模式 或者 'URL_MODEL' =>3(URL兼容模式)
	'URL_PATHINFO_FETCH' => ':get_path_info', //加入此項配置
);
Nginx.conf:
location / { // …..省略部分代碼
   if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=$1  last;
   break;
    }
 }

如出現下圖情況(_STORAGE_WRITE_ERROR_),請檢查Runtime目錄是否具有讀寫權限


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章