Nginx+php 搭建Thinkphp服務支持URL_MODEL全模式

  1. 以下部署中nginx配置支持thinkphp的URL_MODEL全模式

    ‘URL_MODEL’ => 1, // URL訪問模式,可選參數0、1、2、3,代表以下四種模式:// 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默認爲PATHINFO 模式

  2. 安裝Nginx(1.16.1)

    • 下載:wget http://nginx.org/download/nginx-1.16.1.tar.gz

    • 安裝依賴

      • apt-get install gcc g++ autoconf automake
      • apt-get install zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev
    • 編譯安裝
      tar xvf nginx-1.16.1.tar.gz
      cd nginx-1.16.1
      ./configure --with-stream
      make
      make install
      ldconfig

    • 配置nginx.conf
      vi /usr/local/nginx/conf/nginx.conf #直接替換所有內容

         user  root;
         worker_processes  auto;
      
         #error_log  logs/error.log;
         #error_log  logs/error.log  notice;
         #error_log  logs/error.log  info;
      
         #pid        logs/nginx.pid;
      
      
         events {
         	use epoll;
         	worker_connections  51200;
         	multi_accept on;
         }
      
      
         http {
         	include       mime.types;
         	default_type  application/octet-stream;
         	
         	server_names_hash_bucket_size 128;
         	client_header_buffer_size 32k;
         	large_client_header_buffers 4 32k;
         	client_max_body_size 50m;
         	sendfile        on;
         	tcp_nopush     on;
         	keepalive_timeout 60;
         	tcp_nodelay on;
         	fastcgi_connect_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;
         	gzip on;
         	gzip_min_length  1k;
         	gzip_buffers     4 16k;
         	gzip_http_version 1.0;
         	gzip_comp_level 2;
         	gzip_types       text/plain application/x-javascript text/css application/xml;
         	gzip_vary on;
         	gzip_proxied        expired no-cache no-store private auth;
         	gzip_disable        "MSIE [1-6]\.";
         	server_tokens off;
         	log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
         		'$status $body_bytes_sent "$http_referer" '
         		'"$http_user_agent" $http_x_forwarded_for';
      
         	server {
         		listen       12345;
         		server_name  47.116.7.26;
         		root /opt/php;
         		index  index.html index.htm index.php;
         		error_page  404              /404.html;
         		location = /404.html {
         			return 404 'Sorry, File not Found!';
         		}
         		error_page  500 502 503 504  /50x.html;
         		location = /50x.html {
         			root   /usr/share/nginx/html; # windows用戶替換這個目錄
         		}
         		location / {
         			try_files $uri @rewrite;
         		}
         		location @rewrite {
         			set $static 0;
         			if  ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
         				set $static 1;
         			}
         			if ($static = 0) {
         				rewrite ^/(.*)$ /index.php?s=/$1;
         			}
         		}
         		location ~ /Uploads/.*\.php$ {
         			deny all;
         		}
         		location ~ \.php/ {
         		   if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
         		   fastcgi_pass 127.0.0.1:9000;
         		   include fastcgi_params;
         		   fastcgi_param SCRIPT_NAME     $1;
         		   fastcgi_param PATH_INFO       $2;
         		   fastcgi_param SCRIPT_FILENAME $document_root$1;
         		}
         		location ~ \.php$ {
         			fastcgi_pass 127.0.0.1:9000;
         			fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         			include fastcgi_params;
         		}
         		location ~ /\.ht {
         			deny  all;
         		}
         	}
      
         }
      
    • 啓動
      vi /usr/lib/systemd/system/nginx.service
      [Unit]
      Description=nginx - high performance web server
      After=network.target remote-fs.target nss-lookup.target

      	[Service]
      	Type=forking
      	ExecStart=/usr/local/nginx/sbin/nginx
      	ExecReload=/usr/local/nginx/sbin/nginx -s reload
      	ExecStop=/usr/local/nginx/sbin/nginx -s stop
      
      	[Install]
      	WantedBy=multi-user.target
      systemctl daemon-reload //重新加載服務
      
      systemctl start nginx  //啓動
      systemctl stop nginx  //停止
      systemctl reload nginx  //重啓 可以不用停止nginx服務,使修改的配置生效
      systemctl restart nginx  //重啓
      systemctl enable nginx  //設置開機啓動
      systemctl disable nginx  //禁用開機啓動
      systemctl status nginx  //查看服務狀態
      
  3. 安裝PHP(5.6)

    • 安裝程序包及依賴庫
      方法一:
      apt-get install python-software-properties
      apt-get install software-properties-common
      add-apt-repository ppa:ondrej/php
      apt-get update
      apt-get upgrade
      apt-get install php5.6-fpm php5.6-mysql
      方法二:
      php5.6無法安裝就安裝php7.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章