0%

Nginx+PHP搭建网站和文件服务器

本文记录搭建简单网站和文件服务器的过程,针对刚入门的小白,仅仅安装Nginx和PHP,不安装mysql

1. 安装Nginx

1
sudo apt update && sudo apt install nginx

1.1. 测试是否安装成功

在浏览器输入你本机IP地址或者域名,会看到如下欢迎信息表示安装成功

欢迎信息在/var/www/html中

2. 安装PHP

1
sudo apt update && sudo apt install php-fpm

如果安装了mysql,用以下命令

1
sudo apt update && sudo apt install php-fpm php-mysql

2.1 查看PHP版本

1
php --version

3. 配置Nginx和PHP

Nginxd的配置文件位于/etc/nginx/sites-available/default

3.1 在此文件中添加php支持,在index.html前添加index.php

index index.php index.html index.htm index.nginx-debian.html; #

3.2 配置域名

server_name YOUR_DOMAIN_OR_IP_HERE;

3.3 配置php与Nginx关联

1
2
3
4
5
6
7
8
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;#此处和所安装的php版本号有关,根据实际填写
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}

3.4 测试Nginx语法是否有误

sudo nginx -t

如果看到如下信息,表示无误

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

3.5 重载Nginx

sudo service nginx reload

4. 测试PHP是否成功运行

编辑info.php
sudo nano /var/www/html/info.php
填入以下内容

1
2
<?php
phpinfo();

然后在你的浏览器输入 http://your_domain_or_IP/info.php,如果出现以下界面表示PHP配置成功

5. 配置网站和文件服务器

配置文件位于/etc/nginx/sites-available/default
编辑完成后重启Nginx服务
systemctl restart nginx.service
以下是我的配置文件,请参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Default server configuration
#网站配置信息
server {
listen 80 ;

root /var/www/site_path; #网站存放目录

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html; #添加index.php支持

server_name YOUR_DOMAIN_OR_IP_HERE; #域名或IP

location / {
try_files $uri $uri/ =404;
}

# 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;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

#文件服务器配置信息
server {
listen 80 ;

charset utf-8; #设置UTF-8防止中文乱码
root /var/www/file_path; #文件存放目录

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name YOUR_DOMAIN_OR_IP_HERE; #域名或者ip

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
autoindex on; #索引
autoindex_exact_size off; #关闭文件确切大小,只显示大概大小(kb,mb,gb)
autoindex_localtime on; #显示文件时间
}

# 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;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}