如何在 ubuntu 14.04 中创建虚拟主机

how to create a virtual host in ubuntu 14.04

我正在使用 ubuntu 14.04 操作系统,我想在开始我的 php 项目之前创建一个虚拟主机。如果有人能给我终端命令,我将不胜感激。

我已经安装了必要的安装并且我正在使用 Symfony php 框架。

创建虚拟主机

sudo nano /etc/hosts

输入密码=>

创建ip和域名=>

127.0.0.1    testsite.lk

创建项目文件夹=> 转到该目录命令提示符=>

sudo chmod -R 777 testsite.lk

将项目分配给组=> :~/mayura$

> sudo chown -R www-data:www-data testsite.lk/

授予权限=> :~/mayura$

sudo chmod -R 777 testsite.lk/

为虚拟主机提供文档根文件 :~/mayura$

cd /etc/apache2/sites-available/

sudo nano testsite.lk.conf
                <VirtualHost *:80>
                    DocumentRoot /home/mayura/project/testsite/web
                    DirectoryIndex index.php
                    ServerName testsite
                    <Directory "/home/mayura/project/testsite/web">
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Order allow,deny
                        allow from all
                    Require all granted
                    </Directory>
                </VirtualHost>

在该文件夹中的 Netbeans 上创建新项目=> index.php

启用站点=>

cd /etc/apache2/sites-available

sudo a2ensite testsite.lk.conf

Apache 重新加载=>

sudo /etc/init.d/apache2 reload

启动浏览器=> 运行=>

xdg-open http://testsite.lk

打开终端或控制台并输入以下命令

转到您的 sites-available apache2 文件夹:

cd /etc/apache2/sites-available/

将您的 000-default.conf 复制到另一个文件(最好使用您的站点或 vhost 的名称)

sudo cp 000-default.conf example.conf

使用您最好的文本编辑器打开该新文件 (example.conf) 或使用:

sudo nano example.conf

将 example.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.

    ServerAdmin webmaster@mail.com
    ServerName  example.com
    ServerAlias www.example.com

    # example.com is the folder where you have to put your site's code.
    # Obviously, its name can be whatever you want
    DocumentRoot /var/www/html/example.com

    # 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>

启用站点如下:

sudo a2ensite example.com

重新启动您的 apache2 网络服务器

sudo /etc/init.d/apache2 restart

编辑您的 /etc/host 文件:

sudo nano /etc/hosts

在文件末尾添加域 example.comwww.example.com。它必须看起来像:

127.0.0.1    localhost
...
# you can replace with your IP
127.0.0.1   example.com
127.0.0.1   www.example.com
...

打开您最好的网络浏览器并在其地址栏中输入:www.example.comexample.com