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中写入:


	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

你可能感兴趣的:(服务器运维)