1. 程式人生 > >Ubuntu 搭建php7+Nginx

Ubuntu 搭建php7+Nginx

1. 下載安裝php7.0、php7.0-fpm及其他常用模組

sudo apt-get install -y php7.0
sudo apt-get install -y php7.0-fpm
sudo apt-get install -y php-zip
sudo apt-get install -y php-dompdf
sudo apt-get install -y php-xml
sudo apt-get install -y php-mbstring
sudo apt-get install -y php-curl
sudo apt-get install -y php-mysql

2. 下載安裝Nginx(1.10.3)

sudo apt-get install -y nginx

3. 配置Nginx

  • 修改/etc/nginx/sites-available下的default檔案
# Add index.php to the list if you are using PHP
index.php index index.html index.htm index.nginx-debian.html;

server項中找到關於php的配置並啟用

location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                #With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                #With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

4. 測試伺服器

  • 重啟Nginx:
  • 重啟php7.0-fpm
sudo /etc/init.d/nginx restart
sudo /etc/init.d/php7.0-fpm restart
  • /var/www/html目錄下新建一個php檔案
<?php
phpinfo();
?>
  • 在瀏覽器中訪問

FAQ

  • 修改nginx預設埠號
    /etc/nginx/sites-available/default中修改server項中的埠監聽
listen 8080 default_server;
listen [
::]:8080 default_server;
  • 修改預設站點目錄
root /var/www/html;
  • 啟用php錯誤提示
    修改php.ini配置檔案/etc/php/7.0/fpm/php.ini
# display_errors = Off
displau_errors = On
# error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL | E_STRICT