ubunte 安装php,配置nginx

295 阅读1分钟

安装依赖

sudo apt-get update
sudo apte-get install php-fpm php-cgi php php-mysql

修改nginx配置

cd /etc/nginx/site-availabe/default
# Add index.php to the list if you are using PHP
 index index.html index.htm index.nginx-debian.html index.php;
 
 [此处省略...]
  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;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          include fastcgi_params;
     }

修改php配置

sudo vim /etc/php/7.2/fpm/php-fpm.conf
[此处省略...]
include=/etc/php/7.2/fpm/pool.d/*.conf

#在此文件末尾加上这句
listen = /var/run/php/php7.2-fpm.sock

重启

#重启nginx服务器
 sudo /etc/init.d/nginx restart
#重启php
 sudo /etc/init.d/php7.2-fpm restart

测试

新建php文件,置于服务器根目录

sudo vim /var/www/html/index.php
#index.php
 <!DOCTYPE html>
  2 <html>
  3 <body>
  4
  5 <?php
  6 echo "Hello World!";
  7 ?>
  8
  9 </body>
 10 </html>

浏览器上访问index.php 文件,能正常显示Hello World!则说明配置成功!