简体   繁体   English

apache 通过脚本启动临时 VirtualHost

[英]apache temporarily VirtualHost by script start

Let's say I have a local apache2 and I need a script that runs the apache2 with a special injected VirtualHost.假设我有一个本地 apache2,我需要一个脚本来运行带有特殊注入 VirtualHost 的 apache2。

More presice:更多的代价:
I have a xampp Installation (PHP 7.3.10, MariaDB 10.4.8, mysqlnd 5.0.12-dev, Apache 2.4.41) on Windows 10.我在 Windows 10 上安装了 xampp(PHP 7.3.10、MariaDB 10.4.8、mysqlnd 5.0.12-dev、Apache 2.4.41)。
Several PHP projects are located all over the hard drive.几个 PHP 项目遍布整个硬盘驱动器。 Until now I modify the ./apache/conf/extra/httpd-vhosts.conf file before starting the apache2 service on every project switch.到现在为止,我在每次项目切换上启动 apache2 服务之前都会修改./apache/conf/extra/httpd-vhosts.conf文件。 With that I can leave the project folders where there are without copying them in the xampp installation folder.这样我就可以将项目文件夹留在那里,而无需将它们复制到 xampp 安装文件夹中。

Now I am curious if I can write a script for each project (bat or sh, GitShell is installed) that will run mariadb normally and apache2 with a temporarily VirtualHost for that specific project folder as DocumentRoot.现在我很好奇我是否可以为每个项目(bat 或 sh,安装了 GitShell)编写一个脚本,该脚本将正常运行 mariadb 和 apache2,该特定项目文件夹的临时 VirtualHost 为 DocumentRoot。

What I currently can do:我目前可以做的:
Lay down a httpd-vhosts.conf file and a startProject.sh script in a project folder.在项目文件夹中放置一个httpd-vhosts.conf文件和一个startProject.sh脚本。 Running the script will copy the configuration file in the xampp installation and then start the apache2 service.运行脚本会复制xampp安装中的配置文件,然后启动apache2服务。
That is working, but only one apache2 process can start and not multiple projects at once.这是有效的,但只能启动一个 apache2 进程,而不能同时启动多个项目。

What I want:我想要的是:
I want to use a parameter that specifies a VirtualHost by parameter when starting apache2.我想在启动 apache2 时使用一个通过参数指定 VirtualHost 的参数。
Maybe by saying apache2 to use a special config file rather then the file in the own project structure.也许通过说 apache2 使用特殊的配置文件而不是自己项目结构中的文件。
Or I want to use a parameter by starting the apache2 process that specifies a alternatie DocumentRoot Folder for a already specified VirtualHost.或者我想通过启动 apache2 进程来使用一个参数,该进程为已经指定的 VirtualHost 指定一个替代 DocumentRoot 文件夹。

Currently this is my approach:目前这是我的方法:

#!\bin\bash
BASEDIR=$(dirname "$0")
xamppPath="C:\xampp\v7.3.10"
cd $xamppPath
"apache\bin\httpd.exe" -S "$BASEDIR\httpd-vhosts.conf" &

But -S only prints out the VirtualHosts, not setting the configuration file.但是-S只打印出 VirtualHosts,而不是设置配置文件。

How can I achieve running a project with a script somewhere on my harddrive without overriding the existing configuration file in the xampp installation folder?如何在不覆盖 xampp 安装文件夹中的现有配置文件的情况下,在硬盘驱动器上的某处使用脚本运行项目?

Easier : build a couple of congratulation files, use option -f to specify an effective configuration file, not -S .更简单:构建几个祝贺文件,使用选项-f指定有效的配置文件,而不是-S

Can be more complicated : user -D name and use <IfDefine name> in the configuration file to "select" sections of it to apply to a particular start.可能更复杂: user -D name并在配置文件中使用<IfDefine name>来“选择”它的部分以应用于特定的启动。

Note: I use apachectl , not directly httpd to start/stop Apache.注意:我使用apachectl ,而不是直接httpd来启动/停止 Apache。

In the xampp package there is no direct apachectl file, so a use of -D <name> is not possible for me.在 xampp 包中没有直接的apachectl文件,所以对我来说使用-D <name>是不可能的。 It would be the perfect solution, but unfortunately not possible.这将是完美的解决方案,但不幸的是不可能。

Now I have to override the httpd-vhosts.conf file from every project I want to start it.现在我必须覆盖每个我想要启动它的项目中的httpd-vhosts.conf文件。 Therefore, I am using a modified script solution:因此,我正在使用修改后的脚本解决方案:

#!\bin\bash
BASEDIR=$(dirname "$0")
xamppPath="C:\xampp\v7.3.10"
cp -f "$BASEDIR\httpd-vhosts.conf" "$xamppPath\apache\conf\extra\httpd-vhosts.conf"
sleep 1
cd $xamppPath
"apache\bin\httpd.exe" &

It is not the perfect solution, but it works.这不是完美的解决方案,但它有效。
Two apache services at the same time with two different projects is not possible, but until now not necessary.两个不同的项目同时使用两个 apache 服务是不可能的,但直到现在还没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM