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

一般我們都是採用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教學有興趣的朋友有幫助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何在PHP中創建和使用接口?您如何在PHP中創建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解釋瞭如何創建,實施和使用PHP中的接口,重點關注其對代碼組織和可維護性的好處。

crypt()和password_hash()有什麼區別?crypt()和password_hash()有什麼區別?Apr 30, 2025 pm 03:39 PM

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

如何防止PHP中的跨站點腳本(XSS)?如何防止PHP中的跨站點腳本(XSS)?Apr 30, 2025 pm 03:38 PM

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

PHP中的自動加載是什麼?PHP中的自動加載是什麼?Apr 30, 2025 pm 03:37 PM

自動加載PHP會在需要時自動加載類文件,從而通過減少內存使用和增強代碼組織來提高性能。最佳實踐包括使用PSR-4和有效組織代碼。

什麼是PHP流?什麼是PHP流?Apr 30, 2025 pm 03:36 PM

PHP流通過一致的API來統一資源諸如文件,網絡插座和壓縮格式之類的處理,從而使復雜性抽象並增強代碼靈活性和效率。

可以使用PHP上傳的文件的最大大小是多少?可以使用PHP上傳的文件的最大大小是多少?Apr 30, 2025 pm 03:35 PM

本文討論了在PHP中管理文件上傳大小的管理,重點是2MB的默認限制以及如何通過修改PHP.INI設置來增加它。

PHP中的無效類型是什麼?PHP中的無效類型是什麼?Apr 30, 2025 pm 03:34 PM

本文討論了PHP 7.1中引入的PHP中的無效類型,允許變量或參數為指定類型或NULL。它突出顯示了諸如提高可讀性,類型安全性和明確意圖的好處,並解釋瞭如何聲明

unset()和unlink()函數之間有什麼區別?unset()和unlink()函數之間有什麼區別?Apr 30, 2025 pm 03:33 PM

本文討論了unset()和unlink()功能在編程中的差異,重點關注其目的和用例。 unset()從內存中刪除變量,而unlink()從文件系統中刪除文件。兩者都對效率至關重要

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能