linux(ubuntu)服務器安裝Nginx、PHP

使瀏覽器可以正常解析index.php,而不是下載index.php文件

一、安裝Nginx、PHP

apt-get install nginx
apt-get install php7.2 php-fpm

二、重啓一下php-fpm服務:service php7.2-fpm restart

如果報錯說找不到這個服務,那可以去/etc/init.d/中查看Service服務,找到你的php-fpm具體叫什麼名字。由於我在安裝PHP時指定了版本7.2,所以fpm的服務名就是php7.2-fpm


三、安裝好後,可以在/etc/nginx/sites-enabled/default查看Nginx服務的默認配置。

我們可以看到Nginx網站的默認路徑是/var/www/html, 在這下面可以把index.php也加上:index index.html index.htm index.nginx-debian.html index.php;

然後去/var/www/html文件夾下面創建一個index.php文件,

cd /var/www/html
vim index.php

在index.php中寫入:

<?php
	phpinfo();
?>

繼續看/etc/nginx/sites-enabled/default這個文件,
可以看到解析PHP腳本的FastCGI服務,

		# pass PHP scripts to FastCGI server
        #
        # location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

去掉註釋,把這段代碼釋放出來,否則php文件無法被瀏覽器解析,會直接以文件形式下載到本地。
去掉註釋後:

		# pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

四、然後重啓Nginx:service nginx restart

接下來打開網站後,就可以正常訪問index.php了,不再出現下載情況了。


這是我第一次購買騰訊雲服務器並進行環境配置,學生價買的,很便宜。域名是jiailing.com,目前還在備案中。如果寫的不清楚之處,可以微信聯繫我:jal517486222

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