如何在 Amazon EC2 上为 phpmyadmin 设置 VirtualHost

如何在 Amazon EC2 上为 phpmyadmin 设置 VirtualHost

你好,我目前正在 Amazon EC2 上设置一个 VirtualHost 来访问 phpmyadmin,这样我就可以通过 test.example.com 访问它,而不是像默认的 example.com/phpmyadmin 那样被广泛使用。

到目前为止,我已经在 /etc/apache2/sites-available/ 中使用以下代码创建了一个文件“testfile”,并启用了它“a2ensite testfile”,但是我没有让 vhost 工作

<VirtualHost *:80>
            ServerAdmin [email protected]

            ServerName test.example.com
            ServerAlias test.example.com

            #DocumentRoot /usr/share/phpmyadmin
            DocumentRoot /home/user/public_html/folder

            RewriteEngine On
            RewriteCond %{HTTP_HOST} !test.example.com
            RewriteRule (.*) [L]


            <Directory /home/user/public_html/folder>

                    Options FollowSymLinks
                    DirectoryIndex index.php
                    AllowOverride None

                    <IfModule mod_php5.c>
                            AddType application/x-httpd-php .php

                            php_flag magic_quotes_gpc Off
                            php_flag track_vars On
                            php_flag register_globals Off
                            php_admin_flag allow_url_fopen Off
                            php_value include_path .
                            php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
                            php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/
                    </IfModule>

            </Directory>
     # Authorize for setup
            <Directory /usr/share/phpmyadmin/setup>
                <IfModule mod_authn_file.c>
                AuthType Basic
                AuthName "phpMyAdmin Setup"
                AuthUserFile /etc/phpmyadmin/htpasswd.setup
                </IfModule>
                Require valid-user
            </Directory>

            # Disallow web access to directories that don't need it
            <Directory /usr/share/phpmyadmin/libraries>
                Order Deny,Allow
                Deny from All
            </Directory>
            <Directory /usr/share/phpmyadmin/setup/lib>
                Order Deny,Allow
                Deny from All
            </Directory>

            ErrorLog /home/user/public_html/folder/logs/error.log
            LogLevel warn
            CustomLog /home/user/public_html/folder/logs/access.log combined

    </VirtualHost>

sudo ln -s /usr/share/phpmyadmin /home/user/public_html/folder

上面这行代码在公共文件夹中创建了 phpmyadmin 的链接。

任何帮助都将不胜感激。

注意:example.com 将替换为我的官方域名

答案1

经过数小时的测试和故障排除后,我们找到了解决方案。

首先确保您在 DNS 管理器中创建了 A 记录,以将您的子域“test.example.com”指向您的服务器 IP(这大约需要 24-72 小时才能传播)。

然后使用以下命令创建您的主机文件(请注意,我使用的是使用单独主机文件的 ubuntu,但您可以将其添加到您的主主机文件中):

sudo vi /etc/apache2/sites-available/test.example.com

在下面添加以下数据(我已在我的系统上启用了 ssl,即端口 443,并且在末尾取消了 ssl 部分):

<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot /usr/share/phpmyadmin
    ServerName test.example.com

    RewriteEngine On
        RewriteCond %{HTTP_HOST} !test.example.com
        RewriteRule (.*) [L]

     <Directory /usr/share/phpmyadmin>

        Options Indexes FollowSymLinks
            AllowOverride None
        deny from all
        allow from localhost
        #allow access via your IP
            allow from xxxxxx.xxxxx.xxxxxxx.xxxxxxxxx/xx

        <IfModule mod_php5.c>
            AddType application/x-httpd-php .php

            php_flag magic_quotes_gpc Off
            php_flag track_vars On
            php_flag register_globals Off
            php_admin_flag allow_url_fopen Off
            php_value include_path .
            php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
            php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/
        </IfModule>

    </Directory>

    # Authorize for setup
    <Directory /usr/share/phpmyadmin/setup>
        <IfModule mod_authn_file.c>
        AuthType Basic
        AuthName "phpMyAdmin Setup"
        AuthUserFile /etc/phpmyadmin/htpasswd.setup
        </IfModule>
        Require valid-user
    </Directory>

    # Disallow web access to directories that don't need it
    <Directory /usr/share/phpmyadmin/libraries>
        Order Deny,Allow
        Deny from All
    </Directory>
    <Directory /usr/share/phpmyadmin/setup/lib>
        Order Deny,Allow
        Deny from All
    </Directory>

    #   SSL Engine Switch:
        #   Enable/Disable SSL for this virtual host.
       # SSLEngine on

        #   A self-signed (snakeoil) certificate can be created by installing
        #   the ssl-cert package. See
        #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
        #   If both key and certificate are stored in the same file, only the
        #   SSLCertificateFile directive is needed.
       # SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
       # SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

        ErrorLog /home/user/public_html/folder/logs/error.log
        LogLevel warn
        CustomLog /home/user/public_html/folder/logs/access.log combined

</VirtualHost>

完成后保存文件并激活你的虚拟主机

sudo a2ensite test.example.com

完成后,它会提示您重新启动 Apache,这样您就可以启动并运行,并且能够通过“test.example.com”而不是“www.example.com/phpmyadmin”访问 phpmyadmin

相关内容