!!!郑重说明!!!:虚拟主机通常无法绑定运行目录到子目录,因此部署时会出现太多的安全隐患,配置比较繁琐,我们强烈不推荐!此方法只给技术爱好者作为测试参考,不再提供额外技术支持。

!!!重要的事!!!:在虚拟主机配置环境下,由于网站的所有数据都被暴露在浏览器访问路径下,因此要控制系统的系统文件不被访问到,当您配置好之后,至少要测试一下路径不能直接被用户下载,否则系统将会受到严重威胁。

​http://www.example.com/.env​http://www.example.com/storag...
!!!其他说明!!!:使用中遇到问题可直接百度/Google搜索 虚拟主机部署Laravel 关键词自行解决。

使用虚拟主机安装前,请先下载环境的检测程序进行空间自荐,不满足要求的虚拟主机将不能安装。

环境检测脚本:​​https://modstart.com/env_chec...​​(opens new window)
如果您的虚拟主机根目录可以绑定到 <网站目录>/public 目录,请绑定到 <网站目录>/public 目录,随后执行 /install.php 安装向导。

如果您的虚拟主机根目录不支持绑定到 <网站目录>/public,需要按照如下配置:

配置 Nginx 或 Apache 服务器,请将网站的根目录配置到 <网站目录>,其中 Nginx 和 Apache 配置需要参考如下所示;
Nginx参考配置

server {
listen 80;
server_name www.example.com;
charset utf-8;
index index.php index.html;
root /var/www/html/www.example.com;
autoindex off;

location ^~ /.git {
deny all;
}

location ^~ /.env {
deny all;
}

location ^~ /storage/ {
deny all;
}

location / {
try_files /public$uri /public$uri/ /public/index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param PHP_VALUE "open_basedir=/var/www/html/www.example.com/:/tmp/:/var/tmp/";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

Apache参考配置

httpd配置

<VirtualHost *:80>
  ServerName www.example.com
  DocumentRoot d:/wwwroot/example.com
</VirtualHost>

在网站系统根目录增加 .htaccess 文件

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

IIS参考配置

web.config 配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<rewrite>
<rules>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}public/{R:1}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/public/index.php" />
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="/public/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

一些已知特殊环境配置说明

阿里云虚拟主机
需要开启PHP5.5版本;
由于阿里云禁用了putenv函数,这个函数尤为重要,所以需要打一个函数补丁,在程序最开始(public/index.php)的位置嵌入PHP代码,putenv 函数被禁用补丁代码如下


function env($key, $defaultValue = null)
{
static $envFileConfig = null;
if (null === $envFileConfig) {
$envFileConfig = [];
$envFile = file_get_contents(__DIR__ . '/.env');
foreach (explode("\n", $envFile) as $line) {
$line = trim($line);
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
list($k, $v) = explode('=', $line);
$envFileConfig[trim($key)] = trim($value);
}
}
if (array_key_exists($key, $envFileConfig)) {
return $envFileConfig[$key];
}
return $defaultValue;
}