简体   繁体   中英

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.

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.
Several PHP projects are located all over the hard drive. Until now I modify the ./apache/conf/extra/httpd-vhosts.conf file before starting the apache2 service on every project switch. With that I can leave the project folders where there are without copying them in the xampp installation folder.

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.

What I currently can do:
Lay down a httpd-vhosts.conf file and a startProject.sh script in a project folder. Running the script will copy the configuration file in the xampp installation and then start the apache2 service.
That is working, but only one apache2 process can start and not multiple projects at once.

What I want:
I want to use a parameter that specifies a VirtualHost by parameter when starting apache2.
Maybe by saying apache2 to use a special config file rather then the file in the own project structure.
Or I want to use a parameter by starting the apache2 process that specifies a alternatie DocumentRoot Folder for a already specified VirtualHost.

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.

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?

Easier : build a couple of congratulation files, use option -f to specify an effective configuration file, not -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.

Note: I use apachectl , not directly httpd to start/stop Apache.

In the xampp package there is no direct apachectl file, so a use of -D <name> is not possible for me. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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