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

时间:2015-02-26 18:29:02

标签: php apache ubuntu

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

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

3 个答案:

答案 0 :(得分:1)

创建虚拟主机

sudo nano /etc/hosts

输入密码=>

创建ip和域名=>

127.0.0.1    testsite.lk

创建项目文件夹=> 转到该目录comand prompt =>

sudo chmod -R 777 testsite.lk

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

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

给予权限=> :〜/ mayura $

sudo chmod -R 777 testsite.lk/

为vertual host提供文档根文件 :〜/ 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上创建新项目=&gt; 的index.php

启用site =&gt;

cd /etc/apache2/sites-available

sudo a2ensite testsite.lk.conf

Apache reload =&gt;

sudo /etc/init.d/apache2 reload

启动浏览器=&gt; 运行=&GT;

xdg-open http://testsite.lk

答案 1 :(得分:1)

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

使用以下步骤转到您的网站可用的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.com www.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.com example.com

答案 2 :(得分:-1)

复制所有代码,并将其保存为.sh文件(例如:virtualhost.sh),然后在命令行中运行该文件

./virtualhost.sh

并按照说明进行操作。它将创建一个文件夹,即您的VirtualHost文件夹。并输入后缀为“ .localhost”的域名,例如projectname.localhost

clear

echo "============================================"
echo "Virtual Host SetUp Script"
echo "============================================"
echo "Please type in directory name for virtual host  \n\r ( the dir should be under /var/www/ )\n\r"
cd /var/www/
read _dir
if [ -d /var/www/$_dir ] ; then

    printf "\n \n The directory: $_dir already exist. would you like to delete it ? (y/n) \n\r" ;
    read answer
    if [ $answer = y ] ; then
        rm -rf $_dir ;
    else 
        return
    fi  
fi


printf "\n \n Creating new dir name: $_dir ... \n \n\r"
cd /var/www/
mkdir $_dir

cd $_dir; 

pwd ;

# virtual host

printf "\n \n \n Shall script proceed further for setting up virtual host ? (y/n) \n\r"
read answer

if [ $answer = y ] ; then

virtualhost="y";

    printf "\n Creating virtual host \n "

    printf "\n Type in user (if you don't know what to write here the it is probably root ) \n\r"
    read usr

   homedir=$_dir ;

    printf "\n Type in domain ( exp: s1.localhost ) \n\r"
    read sn


    # Create a directory for your apache errors log 
    mkdir /var/log/apache2/$sn/


    # Create  VirtualHost configuration file in /etc/apache2/site-available/
    printf "<VirtualHost *:80>
            ServerAdmin webmaster@$sn
            ServerName $sn
            ServerAlias www.$sn

            DocumentRoot /var/www/$homedir

            <Directory /var/www/$homedir>
                    Options All
                    AllowOverride All
            </Directory>    

            ErrorLog /var/log/apache2/$sn/error.log

            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn

            CustomLog /var/log/apache2/$sn/access.log combined

    </VirtualHost>" > /etc/apache2/sites-available/"$sn.conf"


    # Add the host to the hosts file
    if [ grep $sn /etc/hosts ] ; then
        printf "the vhost is allready in /etc/hosts "

    else 
        printf "Adding $sh to /etc/hosts "  
        printf "\n" >> /etc/hosts
        printf "127.0.0.1 $sn" >> /etc/hosts
    fi

    # Enable site 
    a2ensite $sn

    # Reload Apache2
    /etc/init.d/apache2 reload

fi

printf "\n \n Script written by Inamur Rahman \n -> Thanks You :) \n" 
printf "\n \n Enjoy your new Virtual Host here http://$_dir.localhost \n \n";
相关问题