[英]How to change root directory for PHP azure website?
I have created a PHP website on azure using app services.我使用应用程序服务在 azure 上创建了一个 PHP 网站。 I use continuous deployment through bitbucket. I need to point the website to public folder in my code to run the app as it is built with zend framework.我通过 bitbucket 使用持续部署。我需要将网站指向代码中的公用文件夹以运行应用程序,因为它是使用 zend 框架构建的。
After some search, was not able to find how to change the folder where the server points for default directory.经过一些搜索,无法找到如何更改服务器指向默认目录的文件夹。
Go to Azure Web apps settings -> Application Settings -> Virtual Applications and directories and setup the physical path of the new folder. 转到Azure Web应用程序设置->应用程序设置->虚拟应用程序和目录,然后设置新文件夹的物理路径。 Also check the Application checkbox. 还要选中“应用程序”复选框。
Restart the web app once. 重新启动Web应用一次。
There are a few scenarios possible:有几种可能的情况:
In the first scenario (Windows App Service) you can go to the App Service > Settings > Configuration blade you need to select the tab "Path Mappings" where you can set the Virtual Applications paths as follows: "/" maps to "site\wwwroot\public".在第一个场景(Windows 应用服务)中,您可以 go 到应用服务 > 设置 > 配置刀片,您需要 select 选项卡“路径映射”,您可以在其中设置虚拟应用程序路径,如下所示:“/”映射到“站点\ wwwroot\公共”。
In the second scenario you can use the .htaccess solution described by @Ed Greenberg, even though for Zend Framework I suggest to use the following settings:在第二种情况下,您可以使用@Ed Greenberg 描述的 .htaccess 解决方案,即使对于 Zend Framework,我建议使用以下设置:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
For the third scenario you have a bit more of a challenge since Apache was replaced by Nginx and the rewrite rules no longer apply.对于第三种情况,您将面临更多挑战,因为 Apache 被 Nginx 取代,并且重写规则不再适用。 Please see my detailed blog article " PHP 8 on Azure App Service " on how to solve this and other challenges with the new Azure App Service for PHP 8. Please see my detailed blog article " PHP 8 on Azure App Service " on how to solve this and other challenges with the new Azure App Service for PHP 8.
Good luck and let me know if it solved your problem.祝你好运,让我知道它是否解决了你的问题。
The correct answer in 2021 (for Laravel, and probably other frameworks with a /public directory) is to put an extra .htaccess in the webroot directory. 2021 年的正确答案(对于 Laravel,可能还有其他带有 /public 目录的框架)是在 webroot 目录中放置一个额外的 .htaccess。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Credit to Azure Web App - Linux/Laravel: Point domain to folder 归功于 Azure Web 应用程序 - Linux/Laravel:将域指向文件夹
For PHP 8.0 with nginx I use startup.sh script placed in the root directory of the project.对于 PHP 8.0 和 nginx,我使用位于项目根目录中的 startup.sh 脚本。 startup.sh contains the following line: startup.sh 包含以下行:
sed -i 's/\/home\/site\/wwwroot/\/home\/site\/wwwroot\/public/g' /etc/nginx/sites-available/default && service nginx reload
You need to add "startup.sh" as Startup Command in General Settings.您需要在常规设置中添加“startup.sh”作为启动命令。 Now "public" dir is your root directory.现在“公共”目录是您的根目录。
Finally I've found Laravel documentation how to make it work with Azure. To be more precise - PHP8 + NGINX. Here is the article link - https://azureossd.github.io/2022/04/22/PHP-Laravel-deploy-on-App-Service-Linux-copy/index.html最后我找到了 Laravel 文档如何使它与 Azure 一起工作。更准确地说 - PHP8 + NGINX。这是文章链接 - https://azureossd.github.io/2022/04/22/PHP-Laravel部署应用程序服务-Linux-copy/index.html
Hope it will be useful:-)希望它会有用:-)
PHP 8 (NGINX) PHP 8 (NGINX)
PHP 8 on Azure App Service Linux use NGINX as the Web Server. PHP 8 on Azure App Service Linux 使用 NGINX 作为 Web 服务器。 To have NGINX route requests to /public we'll have to configure a custom startup script.要让 NGINX 将请求路由到 /public,我们必须配置自定义启动脚本。 We can grab the existing default.conf under /etc/nginx/sites-available/default.conf and run cp /etc/nginx/sites-available/default.conf /home.我们可以在 /etc/nginx/sites-available/default.conf 下获取现有的 default.conf 并运行 cp /etc/nginx/sites-available/default.conf /home。 This will copy the default.conf we need into /home so we can download it with an FTP client or any other tool that allows this.这会将我们需要的 default.conf 复制到 /home 中,以便我们可以使用 FTP 客户端或任何其他允许此操作的工具下载它。
This default.conf has the following line:此 default.conf 具有以下行:
root /home/site/wwwroot;
We need to change it to the following:我们需要将其更改为以下内容:
root /home/site/wwwroot/public;
Next, under the location block we need to change it from:接下来,在 location 块下,我们需要将其更改为:
location / {
index index.php index.html index.htm hostingstart.html;
}
to the following:到以下内容:
location / {
index index.php index.html index.htm hostingstart.html;
try_files $uri $uri/ /index.php?$args;
}
Now configure your actual startup.sh bash script.现在配置您实际的 startup.sh bash 脚本。 Note, the file name is arbitrary as long as it is a Bash (.sh) script.注意,文件名是任意的,只要它是 Bash (.sh) 脚本即可。 Configure the file along the lines of the below:按照以下行配置文件:
#!/bin/bash
echo "Copying custom default.conf over to /etc/nginx/sites-available/default.conf"
NGINX_CONF=/home/default.conf
if [ -f "$NGINX_CONF" ]; then
cp /home/default.conf /etc/nginx/sites-available/default
service nginx reload
else
echo "File does not exist, skipping cp."
fi
NOTE: $query_string can be used as well.注意:也可以使用 $query_string。 See the official documentation here.请在此处查看官方文档。
Our custom default.conf should look like the below:我们的自定义 default.conf 应如下所示:
server {
#proxy_cache cache;
#proxy_cache_valid 200 1s;
listen 8080;
listen [::]:8080;
root /home/site/wwwroot/public;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
index index.php index.html index.htm hostingstart.html;
try_files $uri $uri/ /index.php?$args;
}
........
.....
...all the other default directives that were in this file originally...
}
Use an FTP client to upload both your startup.sh script and your custom default.sh to the /home directory for your PHP App Service.使用 FTP 客户端将 startup.sh 脚本和自定义 default.sh 上传到 PHP 应用服务的 /home 目录。
Next, under 'Configuration' in the portal target /home/startup.sh (or whatever the startup script file name is).接下来,在门户目标 /home/startup.sh(或任何启动脚本文件名)中的“配置”下。
Laravel App Laravel 应用程序
Lastly, restart the App Service.最后,重新启动应用服务。 This should now be using our custom startup script.现在应该使用我们的自定义启动脚本。 Use LogStream or the Diagnose and Solve -> Application Logs detector, or other methods, to see the stdout from the script.使用 LogStream 或 Diagnose and Solve -> Application Logs detector 或其他方法,从脚本中查看标准输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.