说实话,这太令人沮丧了。我不知道我做错了什么。
我想设置虚拟主机/服务块。当我浏览时www.example.com
,它应该会显示This is a test page www.example.com
!
更新:我在 nginx 错误日志文件中收到此错误:
11:40:52 [crit] 12959#12959: *9 connect() to unix:/run/php/php7.2-fpm.sock failed
我在以下位置创建了一个文件/var/www/www.example.com/index.php
:
<?php
echo "<h1> This is a test page www.example.com ! </h1>";
然后在/etc/nginx/sites-available/www.example.com
:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/www.example.com;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
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;
#}
}
然后在/etc/sites-enabled/www.example.com
(与 相同的代码/etc/nginx/sites-available/www.example.com
):
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/www.example.com;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
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;
#}
}
我也运行nginx -t
并重新启动了我的 nginx。
当我浏览时www.example.com
,它会转到其他网站。基本上index.php
没有反映。
答案1
我找到了这个问题的解决方案:
基本上我们应该编辑该www.conf
文件。
sudo nano /etc/php/7.2/fpm/pool.d/www.conf
;
您应该通过在行首添加内容来注释掉此行。
;listen = 127.0.0.1:9000;
并取消注释此行:
listen =/var/run/php/php7.2-fpm.sock
现在,当您运行时sudo tail -30 /var/log/nginx/error.log
,错误应该不再存在。
答案2
当我浏览 www.example.com 时”
您拥有 www.example.com?
当我浏览 www.example.com 时,它应该向我显示这是一个测试页面 www.example.com!
不,不是。除非 www.example.com 的所有者是你。我对此表示严重怀疑,因为它是“IANA 管理的保留域”。你应该将其更改为你的域名,如果你不拥有域名,则将其更改为 IP 地址(或本地主机)。