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

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

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配置文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 10068;
        multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log off;
        error_log /dev/stderr;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

我相当确定我需要按照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:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 10068;
        multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log off;
        error_log /dev/stderr;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

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

server {
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot/public;
    index index.php index.html index.htm;
    port_in_redirect off;

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

    # 禁用 .git 目录
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_param HTTP_PROXY "";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_intercept_errors on;
            fastcgi_connect_timeout         300;
            fastcgi_send_timeout           3600;
            fastcgi_read_timeout           3600;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_index index.php;
        }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

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

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:

server {
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot/public;
    index index.php index.html index.htm;
    port_in_redirect off;

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

    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_param HTTP_PROXY "";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_intercept_errors on;
            fastcgi_connect_timeout         300;
            fastcgi_send_timeout           3600;
            fastcgi_read_timeout           3600;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_index index.php;
        }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

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

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

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

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.

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:

确定