linux如何建立虚拟主机


在Linux中,您可以使用Apache、Nginx等Web服务器软件来建立虚拟主机

    安装Apache或Nginx:

对于Ubuntu/Debian系统:

sudo apt-get updatesudo apt-get install apache2

或者

sudo apt-get updatesudo apt-get install nginx

对于CentOS/RHEL系统:

sudo yum install httpd

或者

sudo yum install epel-releasesudo yum install nginx
    创建虚拟主机配置文件:

在Apache中,虚拟主机配置文件通常位于/etc/apache2/sites-available/目录下。首先创建一个新的配置文件,例如my_virtual_host.conf

sudo nano /etc/apache2/sites-available/my_virtual_host.conf

在Nginx中,虚拟主机配置文件通常位于/etc/nginx/sites-available/目录下。首先创建一个新的配置文件,例如my_virtual_host

sudo nano /etc/nginx/sites-available/my_virtual_host
    编辑虚拟主机配置文件:

在Apache的配置文件中,添加以下内容并根据需要修改:

<VirtualHost *:80>ServerAdmin webmaster@localhostServerName my_virtual_host.localServerAlias www.my_virtual_host.localDocumentRoot /var/www/my_virtual_host<Directory /var/www/my_virtual_host>Options Indexes FollowSymLinksAllowOverride AllRequire all granted</Directory>ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

在Nginx的配置文件中,添加以下内容并根据需要修改:

server {listen 80;server_name my_virtual_host.local www.my_virtual_host.local;root /var/www/my_virtual_host;index index.html index.htm;location / {try_files $uri $uri/ =404;}error_log /var/log/nginx/my_virtual_host.error.log;access_log /var/log/nginx/my_virtual_host.access.log;}
    创建网站根目录:
sudo mkdir -p /var/www/my_virtual_host
    在网站根目录中创建一个简单的index.html文件以进行测试:
echo "<h1>Welcome to My Virtual Host!</h1>" | sudo tee /var/www/my_virtual_host/index.html
    启用虚拟主机:

对于Apache:

sudo a2ensite my_virtual_host.confsudo systemctl reload apache2

对于Nginx:

sudo ln -s /etc/nginx/sites-available/my_virtual_host /etc/nginx/sites-enabled/sudo systemctl reload nginx
    更新本地hosts文件:

编辑/etc/hosts文件(在Ubuntu/Debian系统中)或/etc/sysconfig/network-scripts/ifcfg-eth0文件(在CentOS/RHEL系统中),并添加以下内容:

127.0.0.1 my_virtual_host.local

保存更改后,您应该能够通过访问http://my_virtual_host.local来查看虚拟主机。


上一篇:postgresql如何创建数据库

下一篇:centos nginxssl怎样优化性能


Linux
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器