我正在尝试设置带有子域名的 apache2

我正在尝试设置带有子域名的 apache2

rollparatha.shahrukhathar.info 在 hosts 文件中使用此子域名,如下所示

127.0.0.1       rollparatha.shahrukhathar.info rollparatha
127.0.1.1       rollparatha
<publicip>  www.rollparatha.shahrukhathar.info


# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

文件位于 /var/www/html/Python/ & /var/www/html/Lentrax.com/demo/

配置于

user@rollparatha:/etc/apache2/sites-available$ cat Lentrax.com.conf 
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

  
    ServerName 127.0.0.1
    ServerAlias *.rollparatha.shahrukhathar.info
    DocumentRoot /var/www/html/lentrax.com/demo/

        ServerAdmin webmaster@localhost


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

其他网站的配置文件是

    user@rollparatha:/etc/apache2/sites-available$ cat Python.conf 
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        ServerName Python.com
    ServerAlias www.Python.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/Python

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我想将这些 vhost 配置为 Python.shahrukhathar.info 和 Lentrx.shahrukhathar.info 。

答案1

如果您要从互联网访问域名,则无需编辑文件/etc/hosts。但是,您必须配置子域名的 DNS 设置以打开服务器的外部 IP。

您可以使用现有的 vhost 配置,但最好从头开始。禁用您已创建的配置文件:

sudo a2dissite Lentrax.com.conf
sudo a2dissite Python.conf
sudo service apache2 restart

python.shahrukhathar.info

创建一个新的 vhost 文件/etc/apache2/sites-available/python.shahrukhathar.info

<VirtualHost *:80>

    DocumentRoot "/var/www/html/Python"
    ServerName python.shahrukhathar.info
    ServerAlias www.python.shahrukhathar.info

    ErrorLog ${APACHE_LOG_DIR}/python.shahrukhathar.info_error.log
    CustomLog ${APACHE_LOG_DIR}/python.shahrukhathar.info_access.log combined

    <Directory /var/www/html/Python>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

当您完成配置后,您必须启用网站并重新启动 Web 服务器:

sudo a2ensite python.shahrukhathar.info
sudo service apache2 restart

lentrax.shahrukhathar.info

创建一个新的 vhost 文件/etc/apache2/sites-available/lentrax.shahrukhathar.info

<VirtualHost *:80>

    DocumentRoot "/var/www/html/Lentrax.com/demo"
    ServerName lentrax.shahrukhathar.info
    ServerAlias www.lentrax.shahrukhathar.info

    ErrorLog ${APACHE_LOG_DIR}/lentrax.shahrukhathar.info_error.log
    CustomLog ${APACHE_LOG_DIR}/lentrax.shahrukhathar.info_access.log combined

    <Directory /var/www/html/Lentrax.com/demo>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

当您完成配置后,您必须启用网站并重新启动 Web 服务器:

sudo a2ensite lentrax.shahrukhathar.info
sudo service apache2 restart

笔记:

  • 这不是必需的,但为每个虚拟主机使用不同的access.logerror.log文件是一种很好的做法。默认值为APACHE_LOG_DIR/var/log/apache2/因此您可以在那里找到新的日志文件。

  • 在配置中,我使用 添加了别名www。我不知道您是否需要它,它也不是必需的,因此如果您不打算使用www.python.shahrukhathar.infowww.lentrx.shahrukhathar.info,则可以删除ServerAlias指令。

  • <Directory>您可能需要根据项目要求更改一些指令。

  • 如果你在服务器上使用防火墙,则必须允许端口80。例如,如果你正在使用,ufw你可以这样做:

    sudo ufw allow 80
    

相关内容