Score:0

Apache Centos mutiple instances and virtual hosts

cw flag

I installed apache on centos and added two virtual hosts (server1.com and server2.com ):

server1.conf

Listen 443 https
<VirtualHost *:443>
    DocumentRoot /opt/server1/
    ServerName  server1.com
    ServerAlias server1.com
<Directory "/opt/server1/">
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
       SetHandler "proxy:unix:/var/run/php-fpm-server1.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

server2.conf

<VirtualHost *:443>
    DocumentRoot /opt/server2/
    ServerName  server2.com
    ServerAlias server2.com
<Directory "/opt/server2/">
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
       SetHandler "proxy:unix:/var/run/php-fpm-server2.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

The configuration above works perfectly, I can access server1.com and server2.com both on port 443 on the browser and they show different contents (perfect).

Now the problem is that I want two instances of apache separate, so I can start and stop each instance when needed. I created two services in /etc/systemd/system folder.

  1. The problem is when I do systemctl start httpd-server1.service, server1.com works perfectly, but server2.com also works and shows same content as server1.com. Only server1.com should be working!!
  2. Second problem is when I do systemctl start httpd-server2.service, It fails and says "no listening sockets available, shutting down" and even if I add another "listen 443" on top of my server2.conf, I still get another error "could not bind to address [::]:443"

Here are the commands in the two systemctl service files that start the httpd processes:

/opt/httpd/sbin/apachectl -f /etc/opt/httpd/conf/httpd-server1.conf -k start

/opt/httpd/sbin/apachectl -f /etc/opt/httpd/conf/httpd-server2.conf -k start

httpd-server1.conf points to server1.conf and httpd-server2.conf to server2.conf so I don't get why when starting one service, the other cannot start as well even on the same port..

I am lost, and I don't know how to make each instance separately. Anyone has this issue before ?

Romeo Ninov avatar
in flag
Merge two config files and read apache docs about virtual hosts. You can't have two programs to listen on the same port in one machine.
el sparrow avatar
cw flag
Thanks @RomeoNinov, If I merge them, I cannot start and stop them seperately, which I would like to achieve
Romeo Ninov avatar
in flag
You can change config of web sites and and make apache daemon to reload the configs w/o restart. Is this OK for you?
el sparrow avatar
cw flag
@RomeoNinov I think I don't understance your answer. Are you talking about the graceful command option? It does not work since I cannot even start my service for server2. Do you know if it is possible to have two instances and start/stop them if needed ?
el sparrow avatar
cw flag
I see an example here https://serverfault.com/questions/31838/can-rhel-4-have-two-instances-of-apache-httpd-running-using-two-different-config , that is what I am trying to do but don't know why I am having ports issues. I can also see the notice " httpd-server1.service: Can't open PID file /var/opt/httpd-server1/run/httpd.pid" although instance of server 1 is working perfectly and the httpd.pid file can be found. I am wondering if the pid has to do with my issue
Romeo Ninov avatar
in flag
Merge the configs, virtual hosts, based on `ServerName`. And when you need to change one of the hosts use graceful reload of config (no restart)
djdomi avatar
za flag
I think he want to use 2 container to separate the configuration. if one daemon fails, the other is not affected if I understood the point of view
el sparrow avatar
cw flag
@djdomi exactly what I want!!! but it seems I can do that with what Romeo Ninov said? I don't know how if the reload would work, I will try and look into it. If you have any other suggestion, I will take it! Thanks everyone
djdomi avatar
za flag
well, you could either just use lxc/docker container to separate also the full stack
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.