実現したいこと
VirtualBoxで学習用に構築したAlmaLinux+nginx+PHP環境でホームディレクトリ内のPHPを実行したい。
- ホームディレクトリ
/home/username/public_html/ - ホームディレクトリURL
http://localhost/~username/ - ルートディレクトリ
/usr/share/nginx/html - ルートディレクトリURL
http://localhost/
発生している問題・分からないこと
ホームディレクトリ内ではhtmlとjpgファイルのアクセスはできるのに、phpinfo.phpを実行しようとすると、File not foundというHTTP 404エラーが表示され実行できない。
ルートディレクトリに配置したphpinfo.phpは正常に実行され表示することができる。
該当のソースコード
# ファイル場所: /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; # 追記箇所ここから location ~ ^/~([^/]+?)/(.+\.php)$ { alias /home/$1/public_html/$2; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; } location ~ ^/~(.+?)(/.*)?$ { alias /home/$1/public_html$2; index index.html index.htm; autoindex on; } # 追記箇所ここまで # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; } # Settings for a TLS enabled server. # # server { # listen 443 ssl; # listen [::]:443 ssl; # http2 on; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers PROFILE=SYSTEM; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # } }
# ファイル場所: /etc/nginx/conf.d/php-fpm.conf # php-fpm実行時に生成されたものをそのまま使用 upstream php-fpm { server unix:/run/php-fpm/www.sock; }
# ファイル場所: /etc/nginx/default.d/php.conf # php-fpm初回実行時に生成されたものをそのまま使用 index index.php index.html index.htm; location ~ \.(php|phar)(/.*)?$ { fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$; fastcgi_intercept_errors on; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass php-fpm; }
<?php phpinfo(); ?>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Google検索で「nginx ホームディレクトリ php」と検索出てくる情報を参考したり、生成AIに質問したりしたが、うまくいかなかった。
補足
インストール構成
AlmaLinux 10.0 Minimal ISOでインストールオプションのチェックを標準・開発ツールとシステムツールに入れてインストール。
インストール完了後、sudo dnf updateを実行した後、nginxとphpをインストール。
事前に設定したこと
etc/php-fpm.d/www.confのuser, groupをnginxに(元の名前はapache)。
listen.owner, listen.groupをnginxに(元の名前はnobody)。
listen.modeを0666にする。
sudo setsebool -P httpd_enable_homedirs onを実行し、ホームディレクトリ(/home/username/)のパーミッションを0711に変更。
バージョン情報
- OS -
AlmaLinux 10.0 (Purple Lion) - Kernel -
Linux localhost.localdomain 6.12.0-55.43.1.el10_0.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 12 06:49:12 EST 2025 x86_64 GNU/Linux - nginx -
nginx/1.26.3 - PHP -
PHP 8.3.19 (cli) (built: Mar 12 2025 13:10:27) (NTS gcc x86_64) - php-fpm -
PHP 8.3.19 (fpm-fcgi) (built: Mar 12 2025 13:10:27)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2025/11/19 01:16