A
A
Alexander2017-09-04 16:09:01
Apache HTTP Server
Alexander, 2017-09-04 16:09:01

Apache and subdomains?

Hello everyone, help me out!
There is Apache on Linux and the following directory structure:
/var/www/domain/htdocs
It is necessary that the domain itself is stored in the above directory, and all subdomains are stored in the domain directory, at the level of the htdocs folder. It also worked automatically adding a subdomain when creating a new folder. How to organize this?
At the moment, it is configured like this and you have to manually always add a new one:

NameVirtualHost *:80
 
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName site.ru
        ServerAlias www.site.ru
        DocumentRoot /var/www/site.ru/htdocs
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
 
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName test.site.ru
        ServerAlias www.test.site.ru
        DocumentRoot /var/www/site.ru/test
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Well, the hosts file is currently
127.0.0.1 localhost
192.168.1.5 WS01
192.168.1.5 site.ru
192.168.1.5 *.site.ru

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2017-09-05
@alterak

Did that and everything worked fine. Naturally, after activating mod_vhost_alias And there is no need to fence anything.

<VirtualHost *:80>
        ServerAdmin  [email protected]
        ServerName   site.ru
        ServerAlias  www.site.ru
        DocumentRoot /var/www/site.ru/htdocs

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName site.ru
        ServerAlias *.site.ru

        UseCanonicalName Off
        VirtualDocumentRoot /var/www/site.ru/%-3

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

A
Alexander Karpov, 2017-09-04
@Inkognitoss

Look, it can be implemented using the sh script. I automated a little in my case, I didn’t need to register hosts, I can share (but you should finalize if I understood you correctly at all)
I use a separate folder for such configs (httpd-add) I include all files with the conf extension inside the classic apache config
by doing something like sudo /etc/httpd/httpd-add/httpd-add.sh newsite
I immediately get a folder in the right directory and a config

#!/bin/bash
if [ -n "$1" ]
then
    if ! [ -d /var/www/$1/ ]
    then
        echo "Message: Create directory ' /var/www/$1/ '"
        mkdir /var/www/$1/
 
        if [ -d /var/www/$1/ ]
            then
                echo "Successfull: create directory ' /var/www/$1/ '"
                echo "<?php echo 'hello world. site automaticaly added';?>" > /var/www/$1/index.php
                chown -R apache:apache /var/www/$1/
            else
                echo "Error: i cat't add directory ' /var/www/$1/ '"
        fi
 
    if ! [ -f /etc/httpd/httpd-add/$1.conf ]
    then
        echo "Create config file /etc/httpd/httpd-add/$1.conf"
 
STRING="<VirtualHost *:80>
\tServerAdmin [email protected]
\tDocumentRoot /var/www/$1
\tServerName $1.site.ru
\tErrorLog logs/$1-error.log
\tLogFormat \"%h %l %u %t \\\"%r\\\" %>s %b\" common
\tCustomLog logs/$1-access.log common
\t<Directory \"/var/www/$1\">
\t\tAllowOverride all
\t\tOrder allow,deny
\t\tAllow from all
\t</Directory>
</VirtualHost>"
     
        echo -e "$STRING" > /etc/httpd/httpd-add/$1.conf
        if  [ -f /etc/httpd/httpd-add/$1.conf ]
        then
            echo -e "Successfull: add configuration:\n$STRING"
        else
            echo "Error: i can't add configuration file to '/etc/httpd/httpd-add/$1.conf ' "
        fi
    else
        echo "Error: config file '/etc/httpd/httpd-add/$1.conf ' exits"
    fi
    else
        echo "Error: directory ' /var/www/$1/ ' exist"
    fi
else
    echo 'No argument!'
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question