安装nginx

1
sudo apt install nginx

添加配置文件

  1. /etc/nginx/sites-available/配置文件.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    server {
    listen 80;
    listen [::]:80;
    root 网站目录;
    index index.php index.html index.htm;
    server_name 网站域名;

    client_max_body_size 100M;

    location / {
    try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    }
  2. 创建配置文件.conf的链接至/etc/nginx/sites-enabled/

    1
    sudo ln -s /etc/nginx/sites-available/配置文件.conf  /etc/nginx/sites-enabled/

重启nginx服务

1
sudo nginx -s reload

安装PHP

1
sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-ldap php-zip php-curl

启动PHP服务

1
2
sudo systemctl start php7.2-fpm
sudo systemctl enable php7.2-fpm

创建网站目录,并为nginx赋予权限

nginx的账户为www-data,需要为其赋予权限(一般为755)

1
sudo chown www-data: -R 网站目录

测试

在网站目录内新建简单PHP文件index.php,并浏览器访问

1
2
3
<?PHP
echo "HELLO"
?>