首頁 > 後端開發 > php教程 > 使用Nginx搭建PHP伺服器

使用Nginx搭建PHP伺服器

WBOY
發布: 2016-08-08 09:28:41
原創
1349 人瀏覽過

一般我們都是採用Apache 作為PHP的解析伺服器,這次則是採用Nginx這個強大的反向代理伺服器來搭建PHP伺服器。下面就以Linux發行版Ubuntu為例搭建一個Nginx的PHP伺服器。

先下載安裝Nginx
<code>sudo apt-get install nginx
</code>
登入後複製
安裝完成後,啟動Nginx
<code>sudo /etc/init.d/nginx start
</code>
登入後複製
這時候打開瀏覽器裡輸入http://localhost/就可以看到Welcome to nginx!的頁面了,說明我們的Nginx伺服器安裝成功接下來安裝PHP5
<code>sudo apt-get install php5-fpm
</code>
登入後複製
安裝成功後,我們要修改Nginx的虛擬機器配置,讓瀏覽器要求的php檔案可以被php cgi解析。編輯Nginx虛擬機器設定檔/etc/nginx/sites-available/default
<code>sudo vim /etc/nginx/sites-available/default
</code>
登入後複製
然後把裡面的設定修改為如下設定內容:
<code># You may add here your
# server {
#   ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    # Only for nginx-naxsi : process denied requests
    #location /RequestDenied {
        # For example, return an error code
        #return 418;
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#   listen 8000;
#   listen somename:8080;
#   server_name somename alias another.alias;
#   root html;
#   index index.html index.htm;
#
#   location / {
#       try_files $uri $uri/ /index.html;
#   }
#}

# HTTPS server
#
#server {
#   listen 443;
#   server_name localhost;
#
#   root html;
#   index index.html index.htm;
#
#   ssl on;
#   ssl_certificate cert.pem;
#   ssl_certificate_key cert.key;
#
#   ssl_session_timeout 5m;
#
#   ssl_protocols SSLv3 TLSv1;
#   ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#   ssl_prefer_server_ciphers on;
#
#   location / {
#       try_files $uri $uri/ /index.html;
#   }
#}
</code>
登入後複製
重新載入我們剛剛變更的Nginx設定
<code>sudo /etc/init.d/nginx reload
</code>
登入後複製
share/nginx/www/目錄下新建一個phpinfo.php文件,可以查看php的設定與環境資訊
<code>sudo vim /usr/share/nginx/www/phpinfo.php
</code>
登入後複製
在phpinfo.php中錄入以下內容:
<code><?php
  phpinfo();
?>
</code>
登入後複製
我們在瀏覽器輸入http://localhost/ phpinfo.php就可以看到PHP的資訊頁了,有版本等資訊。 PHP5還有很多支援的模組,如果需要的話可以選擇安裝,一般這些模組都是php5-開頭,例如php5-mysql,在Ubuntu裡安裝他只需
<code>sudo apt-get install php5-mysql
</code>
登入後複製
PHP的模組安裝後別忘記重啟PHP5哦,執行以下命令可以重啟
<code>sudo /etc/init.d/php5-fpm restart
</code>
登入後複製

以上就介紹了使用Nginx建造PHP伺服器,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板