在阿里雲上搭建nginx + ThinkPHP 的實踐


作為一個程序猿,理應用linux系統來作為平時的工作機環境,哎,之前倒是用過一段時間的linux,可惜后來換了本本,后來竟然沒有保持,嗷嗷后悔中。。。

廢話不多說,大家用windows的理由都一樣,但終究是要找補回來的,當你搭建一台linux服務器,遇到問題的時候,你發現之前偷的懶都找上來了,誠然,在windows上部署一個php+apache的環境簡直是傻瓜至極,so easy..phpstudy分分鍾幫你搞定, apache+php+mysql一步到位。

盡管網上都說nginx嗷嗷好,終究由於不熟悉而放棄使用了,用的是著名的 LAMP,也就是 Linux + Apache + Mysql + PHP.

但是由於我們網站是國內某個知名度很高的大網站。。。必須要吹一波,線程動不動飆到400是很容易得事。。。所以感覺很苦惱。 一看進程 httpd線程嗷嗷多,也想用apache 的 mpm worker模式,但是告訴我自己的php是線程安全的,感覺與php版本不搭,於是乎,開始了nginx的坑爹之旅。

首先想到的是,在LAMP上,直接裝個nginx不就完了么。那么就只需要nginx就可以了呀,后來發現,完全不行,啟動的時候完全不行,首先就是nginx 的conf文件對於解析php的設置,網上版本嗷嗷多,試了好多,終於找到一個可以用的,最后竟然說數據庫不支持。。。

 

后面想着自己從頭搭建一台LNMP服務器,發現ali雲上有這樣的鏡像,不用從頭來了,於是搞了一個,最后conf的配置如下:

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

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

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;

    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;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  yourserver1 yourserver2;
        root         /usr/share/nginx/xxxx;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

location / {  
    index index.php;  
    if (!-e $request_filename) {  
        rewrite ^/(.*)$ /index.php?s=$1 last;
        break;  
    }  
}  
location ~ \.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
        set $fastcgi_script_name2 $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
            set $fastcgi_script_name2 $1;
            set $path_info $2;
        }
        fastcgi_param   PATH_INFO $path_info;
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;
        fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;
    }
        
error_page 404 /404.html;
            location = /40x.html {
        }

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

最后記得給你的folder都加上權限。 chmod 777 -R /usr/share/nginx/xxx

 

畢。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM