一:apache虛擬主機簡介php
linux主機上安裝了apache這個web服務器,它只服務bbs.zijian.com這個論壇,因此只有一個網站根目錄,這時候就不必用到虛擬主機,這個主機都時bbs.zijian.com的,它的配置直接在apache主配置文件裏面修改html
shell> vim /usr/local/apache/conf/httpd.conf ----------------------------------------------- #DocumentRoot "/var/www/bbs" #網站根目錄 <Directory "/var/www/bbs"> #針對根目錄設置相應權限 Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> -----------------------------------------------
另外一種狀況,apache搭建的web服務器可能同時服務多個網站(bbs.xxx.com,www.xxx.com,news.xxx.com),這時apache就得爲每一個網站分配一個網站根目錄,並分別對每一個網站作相應設置,每一個網站就好像運行在單獨的一臺虛擬主機上,書寫格式:
mysql
------------------------------------------------- <VirtualHost 10.10.54.152:80> #基於IP的虛擬主機,監聽10.10.54.152:80端口 #<VirtualHost *:80> #基於域名的虛擬主機,監聽全部傳入到本機上的80端口 ServerAdmin root@jian.com DirectoryIndex index.php index.html DocumentRoot "/var/www/news" ServerName news.zijian.com ErrorLog "logs/news-error_log" CustomLog "logs/news-access_log" common </VirtualHost> --------------------------------------------------
二:具體配置步驟
linux
//環境介紹
nginx
1.apache編譯安裝版本2.4.7 2.系統:centos6.4 IP: eth0 10.10.54.157 www.zijian.com eth0:0 10.10.54.151 bbs.zijian.com eth0:1 10.10.54.152 news.zijian.com 3.實現 www和bbs網站須要用ssl加密 news網站不準要ssl加密 #在配置以前,須要先用系統提供的openssl工具製做網站證書,參考個人另外一篇文章 http://my.oschina.net/zijian1315/blog/205839 4.php編譯參數 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock
//主配置文件修改
web
shell> vim /usr/local/apache2/conf/httpd.conf ------------------------------------------------ #爲了支持ssl,確保下面兩個模塊開啓 LoadModule ssl_module modules/mod_ssl.so LoadModule socache_shmcb_module modules/mod_socache_shmcb.so #news網站須要監聽80端口 Listen 80 ServerName www.localhost.com:80 #下面兩個文件必定要開啓 #下面使用的是相對路徑,相對於 ServerRoot "/usr/local/apache2" Include conf/extra/httpd-vhosts.conf #完整的是/usr/local/apache2/conf/extra/httpd-vhosts.conf Include conf/extra/httpd-ssl.conf ------------------------------------------------
//配置httpd-ssl.conf文件加密www和bbs網站sql
httpd-ssl.conf --------------------------------------------------- Listen 443 #www網站https協議 <VirtualHost 10.10.54.157:443> DocumentRoot "/var/www/html" ServerName www.zijian.com ServerAdmin you@example.com ErrorLog "/usr/local/apache2/logs/www_error_log" TransferLog "/usr/local/apache2/logs/www_access_log" #Enable/Disable SSL for this virtual host. SSLEngine on SSLCertificateFile "/usr/local/nginx/conf/ssl/client.pem" #製做的網站證書路徑 SSLCertificateKeyFile "/usr/local/nginx/conf/ssl/client.key" #網站key文件 </VirtualHost> #bbs網站https協議 <VirtualHost 10.10.54.151:443> DocumentRoot "/var/www/bbs" ServerName bbs.zijian.com ServerAdmin you@example.com ErrorLog "/usr/local/apache2/logs/bbs_error_log" TransferLog "/usr/local/apache2/logs/bbs_access_log" SSLEngine on SSLCertificateFile "/usr/local/nginx/conf/ssl/client.pem" #製做的網站證書路徑 SSLCertificateKeyFile "/usr/local/nginx/conf/ssl/client.key" #網站key文件 </VirtualHost> #news網站https協議 <VirtualHost 10.10.54.152:80> #https不能訪問 DocumentRoot "/var/www/news" ServerName news.zijian.com ServerAdmin you@example.com ErrorLog "/usr/local/apache2/logs/news_error_log" TransferLog "/usr/local/apache2/logs/news_access_log" </VirtualHost> -------------------------------------------------- #這臺主機在httpd.conf主配置文件中開啓了80端口監聽,在http-ssl.conf中開啓了443端口監聽,每一個網站根據它能夠接受的端口處理用戶請求