如何在 Microsoft Azure Web App 上设置 Symfony 网站?

huangapple go评论108阅读模式
英文:

How to setup a Symfony website on a Microsoft Azure Web App?

问题

我使用Symfony框架版本6.2构建了一个PHP网站,运行在PHP 8.2上。通过Github actions,我成功将其部署到Microsoft Azure Web App。

当我访问由Microsoft Azure Web App创建的默认域时,我看到一个"403 Forbidden, nginx/1.22.1"错误消息。

通过SSH,我可以进入创建的Azure Web App以检查一些设置。

我的Symfony网站部署在文件夹/home/site/wwwroot中。

这是默认的/etc/nginx/nginx.conf配置文件:

  1. user www-data;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;
  5. events {
  6. worker_connections 10068;
  7. multi_accept on;
  8. }
  9. http {
  10. ##
  11. # Basic Settings
  12. ##
  13. sendfile on;
  14. tcp_nopush on;
  15. types_hash_max_size 2048;
  16. # server_tokens off;
  17. # server_names_hash_bucket_size 64;
  18. # server_name_in_redirect off;
  19. include /etc/nginx/mime.types;
  20. default_type application/octet-stream;
  21. ##
  22. # SSL Settings
  23. ##
  24. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  25. ssl_prefer_server_ciphers on;
  26. ##
  27. # Logging Settings
  28. ##
  29. access_log off;
  30. error_log /dev/stderr;
  31. ##
  32. # Gzip Settings
  33. ##
  34. gzip on;
  35. # gzip_vary on;
  36. # gzip_proxied any;
  37. # gzip_comp_level 6;
  38. # gzip_buffers 16 8k;
  39. # gzip_http_version 1.1;
  40. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  41. ##
  42. # Virtual Host Configs
  43. ##
  44. include /etc/nginx/conf.d/*.conf;
  45. include /etc/nginx/sites-enabled/*;
  46. }

我相当确定我需要按照Symfony文档中的说明更改nginx.conf配置:
https://symfony.com/doc/current/setup/web_server_configuration.html#nginx

但我不知道在哪里添加这个额外的配置,因为nginx.conf文件将在每次部署时被覆盖。
即使我手动更改配置文件,也无法正常工作。

有人能指导我正确的方向吗?

英文:

I build a PHP website with Symfony framework version 6.2 and running on PHP 8.2.
Via Github actions, I managed to deploy it to a Microsoft Azure Web App.

When I visit the default domain, created by the Microsoft Azure Web App, I see a "403 Forbidden, nginx/1.22.1" error message.

Via SSH, I can go to the created Azure Web App to check some settings.

My Symfony website is deployed to the folder /home/site/wwwroot.

This is the default /etc/nginx/nginx.conf:

  1. user www-data;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;
  5. events {
  6. worker_connections 10068;
  7. multi_accept on;
  8. }
  9. http {
  10. ##
  11. # Basic Settings
  12. ##
  13. sendfile on;
  14. tcp_nopush on;
  15. types_hash_max_size 2048;
  16. # server_tokens off;
  17. # server_names_hash_bucket_size 64;
  18. # server_name_in_redirect off;
  19. include /etc/nginx/mime.types;
  20. default_type application/octet-stream;
  21. ##
  22. # SSL Settings
  23. ##
  24. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  25. ssl_prefer_server_ciphers on;
  26. ##
  27. # Logging Settings
  28. ##
  29. access_log off;
  30. error_log /dev/stderr;
  31. ##
  32. # Gzip Settings
  33. ##
  34. gzip on;
  35. # gzip_vary on;
  36. # gzip_proxied any;
  37. # gzip_comp_level 6;
  38. # gzip_buffers 16 8k;
  39. # gzip_http_version 1.1;
  40. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  41. ##
  42. # Virtual Host Configs
  43. ##
  44. include /etc/nginx/conf.d/*.conf;
  45. include /etc/nginx/sites-enabled/*;
  46. }
  47. #mail {
  48. # # See sample authentication script at:
  49. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  50. #
  51. # # auth_http localhost/auth.php;
  52. # # pop3_capabilities "TOP" "USER";
  53. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  54. #
  55. # server {
  56. # listen localhost:110;
  57. # protocol pop3;
  58. # proxy on;
  59. # }
  60. #
  61. # server {
  62. # listen localhost:143;
  63. # protocol imap;
  64. # proxy on;
  65. # }
  66. #}

I'm pretty sure I need to change the nginx.conf configuration like explained in the Symfony documentation:
https://symfony.com/doc/current/setup/web_server_configuration.html#nginx

But I have no clue where I can add this extra configuration, because the nginx.conf file will be overwritten by every deploy.
Even when I manually change the configuration file, it doesn't work.

Can someone point me into the right direction?

答案1

得分: 1

Thanks to @DragonBe,我发现我需要在文件/etc/nginx/sites-available/default中更改我的nginx配置。

我在Symfony项目的根目录创建了一个文件/home/site/wwwroot/nginx.default

  1. server {
  2. listen 8080;
  3. listen [::]:8080;
  4. root /home/site/wwwroot/public;
  5. index index.php index.html index.htm;
  6. port_in_redirect off;
  7. location / {
  8. try_files $uri $uri/ /index.php$is_args$args;
  9. }
  10. # 禁用 .git 目录
  11. location ~ /\.git {
  12. deny all;
  13. access_log off;
  14. log_not_found off;
  15. }
  16. location ~ [^/]\.php(/|$) {
  17. fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
  18. fastcgi_pass 127.0.0.1:9000;
  19. include fastcgi_params;
  20. fastcgi_param HTTP_PROXY "";
  21. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  22. fastcgi_param PATH_INFO $fastcgi_path_info;
  23. fastcgi_param QUERY_STRING $query_string;
  24. fastcgi_intercept_errors on;
  25. fastcgi_connect_timeout 300;
  26. fastcgi_send_timeout 3600;
  27. fastcgi_read_timeout 3600;
  28. fastcgi_buffer_size 128k;
  29. fastcgi_buffers 4 256k;
  30. fastcgi_busy_buffers_size 256k;
  31. fastcgi_temp_file_write_size 256k;
  32. fastcgi_index index.php;
  33. }
  34. location ~ \.php$ {
  35. return 404;
  36. }
  37. error_log /var/log/nginx/project_error.log;
  38. access_log /var/log/nginx/project_access.log;
  39. }

在使用Github Actions部署后,我运行以下命令:

  1. cp /home/site/wwwroot/nginx.default /etc/nginx/sites-available/default && service nginx restart

我希望这对其他Symfony网站在Azure Web App上部署时有所帮助。

英文:

Thanks to @DragonBe, I found out that I had to change my nginx configuration in the file /etc/nginx/sites-available/default.

I created a file /home/site/wwwroot/nginx.default in the root of my Symfony project:

  1. server {
  2. listen 8080;
  3. listen [::]:8080;
  4. root /home/site/wwwroot/public;
  5. index index.php index.html index.htm;
  6. port_in_redirect off;
  7. location / {
  8. try_files $uri $uri/ /index.php$is_args$args;
  9. }
  10. # Disable .git directory
  11. location ~ /\.git {
  12. deny all;
  13. access_log off;
  14. log_not_found off;
  15. }
  16. location ~ [^/]\.php(/|$) {
  17. fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
  18. fastcgi_pass 127.0.0.1:9000;
  19. include fastcgi_params;
  20. fastcgi_param HTTP_PROXY "";
  21. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  22. fastcgi_param PATH_INFO $fastcgi_path_info;
  23. fastcgi_param QUERY_STRING $query_string;
  24. fastcgi_intercept_errors on;
  25. fastcgi_connect_timeout 300;
  26. fastcgi_send_timeout 3600;
  27. fastcgi_read_timeout 3600;
  28. fastcgi_buffer_size 128k;
  29. fastcgi_buffers 4 256k;
  30. fastcgi_busy_buffers_size 256k;
  31. fastcgi_temp_file_write_size 256k;
  32. fastcgi_index index.php;
  33. }
  34. location ~ \.php$ {
  35. return 404;
  36. }
  37. error_log /var/log/nginx/project_error.log;
  38. access_log /var/log/nginx/project_access.log;
  39. }

After the deploy with Github Actions, I run this command:

  1. cp /home/site/wwwroot/nginx.default /etc/nginx/sites-available/default && service nginx restart

I hope this will help other Symfony websites with their deployment to an Azure Web App.

答案2

得分: 0

你的思路是正确的,不过你正在查看错误的配置文件。应该修改的文件是 /etc/nginx/sites-available/default

对于你的第二个问题,其中设置被覆盖的解决方案是使用命令行指令。

  1. cp nginx.default /etc/nginx/sites-available/default && service nginx restart

如果你想了解更多信息,我在 https://www.azurephp.dev/2021/09/php-8-on-azure-app-service/ 上有一篇完整的文章。

祝你好运! 🍀

英文:

You’re thought process is correct, though you’re looking at the wrong configuration file. The file that should be modified is /etc/nginx/sites-available/default.

For your second issue, where the settings are being overwritten, the solution is to use the command line instruction.

  1. cp nginx.default /etc/nginx/sites-available/default && service nginx restart

I have a full article about it on https://www.azurephp.dev/2021/09/php-8-on-azure-app-service/ if you want to learn more.

Good luck 🍀

huangapple
  • 本文由 发表于 2023年5月20日 23:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定