1. 程式人生 > >apache和nginx虛擬主機的配置

apache和nginx虛擬主機的配置

apache虛擬主機配置:

[[[email protected] conf.d]# mkdir /webroot/web1   

[email protected] conf.d]# mkdir -p /var/log/httpd/web1/      //準備網站根目錄和日誌存放目錄(日誌目錄可不寫)

[[email protected] conf.d]# echo this is web1 html> /webroot/web1/index.html

[[email protected] html]# cd /etc/httpd/conf.d

[[email protected]

conf.d]# cat kailey.conf <VirtualHost 192.168.122.224:80>     //如果監聽埠更改了,需要在/etc/httpd/conf/httpd.conf 中配置listen 埠;     ServerAdmin [email protected]     ServerName www.kailey.com     ServerAlias kailey.com     DocumentRoot /webroot/web1     後面要和這個一致     ErrorLog /var/log/httpd/web1/error_log     CustomLog /var/log/httpd/web1/access_log combined </VirtualHost> <Directory "/webroot/web1
">                       //網頁所在位置     AllowOverride none     <RequireAll>         Require not ip 192.168.122.29         Require all granted     </RequireAll> </Directory>

[[email protected] conf.d]# httpd -t Syntax OK

[[email protected] ~]# cat /etc/hosts                                 //在本地主機中做解析 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.122.224 www.kailey.com kailey.com              

[[email protected] conf.d]# systemctl restart httpd

[[email protected] ~]# elinks --dump www.kailey.com       //物理機做測試訪問域名    this is web1 html

nginx虛擬主機配置:

[[email protected] conf.d]# mkdir -p /webroot/a

[[email protected] conf.d]# mkdir -p /var/log/nginx/a

[[email protected] conf.d]# echo nginx a.org > /webroot/a/index.html

[[email protected] ~]# cd /etc/nginx/conf.d/ [[email protected] conf.d]# cat a.org.conf server{    listen 80;                                  //如果更改埠只需要再這裡跟該埠就行    server_name www.a.org a.org;

   error_log /var/log/nginx/a/err.log error;    access_log /var/log/nginx/a/access.log main;

   location / {     root /webroot/a;     index index.html index.htm;    } }   

[[email protected] conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

[[email protected] conf.d]# nginx -s reload

[[email protected] ~]# cat /etc/hosts 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.122.13 www.a.org a.org

[[email protected] ~]# elinks --dump www.a.org    nginx a.org