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


how to create a virtual host in ubuntu 14.04

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

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

打开终端或控制台并编写以下命令

转到您的站点可用的 apache2 文件夹,其中包含:

cd /etc/apache2/sites-available/

将您的 000-default.conf 复制到另一个文件(最好是站点或虚拟主机的名称(

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 Web 服务器

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

创建虚拟主机

sudo nano /etc/hosts

输入密码=>

创建 IP 和域名 =>

127.0.0.1    testsite.lk

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

sudo chmod -R 777 testsite.lk

将项目分配到组=>:~/马尤拉$

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

授予权限=>:~/马尤拉$

sudo chmod -R 777 testsite.lk/

为垂直主机提供文档根文件:~/马尤拉$

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 上创建新项目=>索引.php

启用站点 =>

cd /etc/apache2/sites-available
sudo a2ensite testsite.lk.conf

Apache reload=>

sudo /etc/init.d/apache2 reload

启动浏览器=>运行=>

xdg-open http://testsite.lk