httpd主配置文件/usr/local/apache2.4/conf/httpd.confphp
- vim /usr/local/apache2.4/conf/httpd.conf //修改如下4個地方 ServerName Require all denied AddType application/x-httpd-php .php DirectoryIndex index.html index.php - /usr/local/apache2.4/bin/apachectl -t //測試語法- /usr/local/apache2.4/bin/apachectl start //啓動服務- netstat -lntp - curl localhost- vim /usr/local/apache2.4/htodcs/test.php //增長以下內容 <?php echo 123; ?> curl localhost/test.php
先打開httpd的配置文件html
- [root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf[1]+ 已中止 vim /usr/local/apache2.4/conf/httpd.conf [root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl restartAH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using fe80::a152:bbdf:8b2b:db9b. Set the ‘ServerName‘ directive globally to suppress this message httpd not running, trying to start [root@aminglinux-001 ~]# fgvim /usr/local/apache2.4/conf/httpd.conf#ServerAdmin you@example.com## ServerName gives the name and port that the server uses to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.## If your host doesn‘t have a registered DNS name, enter its IP address here.#ServerName www.example.com:80## Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory /> AllowOverride none Require all denied </Directory>## Note that from this point forward you must specifically allow# particular features to be enabled - so if something‘s not working as# you might expect, make sure that you have specifically enabled it# below.#-- 插入 -- [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[1]+ 已中止 vim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# /usr/local/apache2.4/bin/apachectl restartAH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message httpd not running, trying to start /usr/local/apache2.4/bin/apachectl: 行 79: 2258 段錯誤 $HTTPD -k $ARGV [root@localhost ~]# fgvim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# fg-bash: fg: 當前: 無此任務 [root@localhost ~]# /usr/local/apache2.4/bin/apachectl restarthttpd not running, trying to start /usr/local/apache2.4/bin/apachectl: 行 79: 2262 段錯誤 $HTTPD -k $ARGV [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl restarthttpd not running, trying to start /usr/local/apache2.4/bin/apachectl: 行 79: 2266 段錯誤 $HTTPD -k $ARGV [root@localhost ~]# /usr/local/apache2.4/bin/apachectl stophttpd (no pid file) not running[root@localhost ~]# /usr/local/apache2.4/bin/apachectl start/usr/local/apache2.4/bin/apachectl: 行 79: 2272 段錯誤 $HTTPD -k $ARGV [root@localhost ~]# !vivim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# /usr/local/apache2.4/bin/apachectl start[root@localhost ~]#
[root@localhost ~]# ps aux |grep httpdroot 2277 0.0 0.8 253580 8788 ? Ss 14:30 0:00 /usr/local/apache2.4/bin/httpd -k start daemon 2278 0.0 1.1 540408 11008 ? Sl 14:30 0:00 /usr/local/apache2.4/bin/httpd -k start daemon 2279 0.0 1.1 540408 11008 ? Sl 14:30 0:00 /usr/local/apache2.4/bin/httpd -k start daemon 2282 0.0 1.1 540408 11008 ? Sl 14:30 0:00 /usr/local/apache2.4/bin/httpd -k start root 2364 0.0 0.0 112680 976 pts/0 S+ 14:32 0:00 grep --color=auto httpd [root@localhost ~]#
剛纔把servername打開了 也就再也不提示那個錯誤了 httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message httpd not running, trying to startmysql
這是第一處,第二處 Require all deniedlinux
用網頁訪問地址192.168.202.131(本機ip)帶不開,再用windowsping 下,是能夠ping通,試下telnet 訪問下80端口 使用telnet 192.168.202.131 80web
由於windows 並無打開tenet的客戶端,因此它會提示沒有這個命令sql
打開telnet客戶端方式-打開控制面板-程序-程序和功能-打開或關閉windows功能apache
使用telnet 192.168.202.131 80 結果是 C:\Users\Administrator.USER-20170629EN>telnet 192.168.202.131 80 正在鏈接192.168.202.131...沒法打開到主機的鏈接。 在端口 80: 鏈接失敗 80端口並不通,由於在linux服務器上並無打開80端口vim
使用iptables -nvL 看下規則windows
[root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 660 54606 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 1 80 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 25 1798 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 25 1798 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 25 1798 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 23 1686 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_direct all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_IN_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_IN_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_OUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_OUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 545 packets, 76871 bytes) pkts bytes target prot opt in out source destination 545 76871 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD_IN_ZONES (1 references) pkts bytes target prot opt in out source destination 0 0 FWDI_public all -- ens33 * 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 FWDI_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto] Chain FORWARD_IN_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination Chain FORWARD_OUT_ZONES (1 references) pkts bytes target prot opt in out source destination 0 0 FWDO_public all -- * ens33 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 FWDO_public all -- * + 0.0.0.0/0 0.0.0.0/0 [goto] Chain FORWARD_OUT_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination Chain FORWARD_direct (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public (2 references) pkts bytes target prot opt in out source destination 0 0 FWDI_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDI_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDI_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain FWDI_public_allow (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public_deny (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public_log (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public (2 references) pkts bytes target prot opt in out source destination 0 0 FWDO_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDO_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDO_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FWDO_public_allow (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public_deny (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public_log (1 references) pkts bytes target prot opt in out source destination Chain INPUT_ZONES (1 references) pkts bytes target prot opt in out source destination 25 1798 IN_public all -- ens33 * 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 IN_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto] Chain INPUT_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination Chain INPUT_direct (1 references) pkts bytes target prot opt in out source destination Chain IN_public (2 references) pkts bytes target prot opt in out source destination 25 1798 IN_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 25 1798 IN_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 25 1798 IN_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 1 60 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain IN_public_allow (1 references) pkts bytes target prot opt in out source destination 1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW Chain IN_public_deny (1 references) pkts bytes target prot opt in out source destination Chain IN_public_log (1 references) pkts bytes target prot opt in out source destination Chain OUTPUT_direct (1 references) pkts bytes target prot opt in out source destination [root@localhost ~]# ;臨時增長一條規則,容許80端口訪問 [root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@localhost ~]#
這個時候再來看網頁 ,刷新下,顯示能夠了,可以訪問了 It works !瀏覽器
再看下telenet 再試下,telnet 192.168.202.131 80 ,只要顯示成這個樣子說明80端口通了
按ctrl + ] 再輸quit 就退出來了
再把那條規則刪掉
[root@localhost ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
再來看看卡死了,再加上規則,就能夠了,瀏覽器也能夠訪問了
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@localhost ~]#
打開配置文件,把 Require all denied 改成 Require all granted
## Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory /> AllowOverride none Require all denied</Directory> #<Directory /> AllowOverride none Require all granted</Directory># 之全部能網頁上能訪問,DocumentRoot "/usr/local/apache2.4/htdocs" 指定了一個 <Directory "/usr/local/apache2.4/htdocs"> 後面有一個 Require all granted 若是把這個改爲denied 確定訪問不了 AllowOverride None # # Controls who can get stuff from this server. # Require all granted</Directory> 改成 # Controls who can get stuff from this server. # #Require all granted Require all denied</Directory>[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# 改完配置,要作一個操做 重啓,後者叫作 從新加載配置 /usr/local/apache2.4/bin/apachectl graceful, 這裏告訴一個命令,用來檢查配置文件語法是否正確/usr/local/apache2.4/bin/apachectl -t 結果是Syntax OK 就沒問題,若是錯了就會報錯 [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]#[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#
再來運行下網頁, 打不開了
因此這個地方須要改爲granted,上面那個地方也是要改爲granted
# # Controls who can get stuff from this server. # Require all granted</Directory>
還有這個地方也是
# Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory /> AllowOverride none Require all granted</Directory>
這是第二處,第三處,須要增長一行和php 相關的配置
增長一行 AddType application/x-httpd-php .php
首先在胚子文件搜AddType,
# # AddType allows you to add to or override the MIME configuration # file specified in TypesConfig for specific file types. # #AddType application/x-gzip .tgz # # AddEncoding allows you to have certain browsers uncompress # information on the fly. Note: Not all browsers support this. # #AddEncoding x-compress .Z #AddEncoding x-gzip .gz .tgz # # If the AddEncoding directives above are commented-out, then you # probably should define those extensions to indicate media types: # AddType application/x-compress .Z AddType application/x-gzip .gz .tgz # # AddHandler allows you to map certain file extensions to "handlers": 增長一行 AddType application/x-httpd-php .php,若是不增長php就沒辦法解析 # If the AddEncoding directives above are commented-out, then you # probably should define those extensions to indicate media types: # AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php #-- 插入 -- [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# 還有一個地方,增長一個索引頁 DirectoryIndex index.html index.php 打開一個網站 爲何不用輸入inde.php 就能夠訪問呢? 打開配置文件vim /usr/local/apache2.4/conf/httpd.conf 搜索Index, 在DirectoryIndex index.html後加入 index.php Require all granted</Directory>## DirectoryIndex: sets the file that Apache will serve if a directory# is requested.#<IfModule dir_module> DirectoryIndex index.html index.php</IfModule>## The following lines prevent .htaccess and .htpasswd files from being -- 插入 -- 檢查 /usr/local/apache2.4/bin/apachectl -t 從新加載 /usr/local/apache2.4/bin/apachectl graceful [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#
先來驗證一下apache 到底支不支持 php呢
[root@aminglinux-001 ~]# vi /usr/local/apache2.4/htdocs/1.php [root@aminglinux-001 ~]# vi /usr/local/apache2.4/htdocs/1.php<?php phpinfo();?>~ ~ ~ ~ ~ ~ ~ :wq
保存以後,用瀏覽器打開訪問一下
這個頁面能夠出來,說明你的php 支持解析了,若是不能出來,會是什麼樣的呢?
好比說咱們沒有加AddType 這一行,把它註釋掉 # AddType application/x-httpd-php .php
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful [root@localhost ~]#
再來看下,打不開,
若是解析不了,須要檢查一下你的apache文件,都檢查哪幾項呢?
1.第一項/usr/local/apache2.4/bin/apachectl -M 看看有沒有加載php5_module (share)php5的模塊
2.若是沒有加載要看一看有沒有這個模塊 ls /usr/local/apache2.4/modules/libphp5.so 有沒有這個文件,若是有文件並無顯示那就查看配置文件裏面有沒有加載這個libphp5.so 的這一行配置,若是沒有確定不能解析的
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -MLoaded Modules: core_module (static) so_module (static) http_module (static) mpm_event_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared) php5_module (shared) [root@localhost ~]# ls /usr/local/apache2.4/modules/libphp5.so /usr/local/apache2.4/modules/libphp5.so [root@localhost ~]#
若是沒有模塊,看下配置文件有沒有LoadModule php5_module modules/libphp5.so 這一行配置
LoadModule php5_module modules/libphp5.so#LoadModule php7_module modules/libphp7.so<IfModule unixd_module> #
3.接下來檢查你的配置文件,有沒有加這一行AddType application/x-httpd-php .php 注意這個地方 有個空格,沒加空格就說明有問題
有問題是能夠檢查出來的,使用命令/usr/local/apache2.4/bin/apachectl -t 若是顯示結果是Syntnx OK
4.還有一步 就是這個index.php,這一行加不加倒也不影響,爲何要加這個呢,由於在訪問一個網站的時候 有時候並無輸入www.aplearn.com/index.php 正常的這纔是真正的地址,可是不加/index.php 也能夠訪問,就是有這個索引頁index.php,就是定義了一個index
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful [root@localhost ~]#
再來看看
如今來試試這個php7 ,不要php5 了,進入配置文件,搜索libphp
#LoadModule php5_module modules/libphp5.soLoadModule php7_module modules/libphp7.so<IfModule unixd_module>## If you wish httpd to run as a different user or group, you must run# httpd as root initially and it will switch. ## User/Group: The name (or #number) of the user/group to run httpd as.# It is usually good practice to create a dedicated user and group for# running httpd, as with most system services.#User daemonGroup daemon</IfModule># ‘Main‘ server configuration#:wq [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful [root@localhost ~]#
快捷鍵ctrl r 再輸入命令相關詞,能夠快速幫你找到命令歷史裏相關的 命令
再打開網頁,也是能夠的
我們看到的這個網頁和/usr/local/php7/bin/php -i |less 看到的結果基本一致的,只不過一個是圖形化表格化 格式,一個是文本文件格式
[root@localhost ~]# /usr/local/php7/bin/php -i |less phpinfo() PHP Version => 7.1.6System => Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 Build Date => Oct 5 2017 23:52:17Configure Command => ‘./configure‘ ‘--prefix=/usr/local/php7‘ ‘--with-apxs2=/usr/local/apache2.4/bin/apxs‘ ‘--with-config-file-path=/usr/local/php7/etc‘ ‘--with-pdo-mysql=/usr/local/mysql‘ ‘--with-mysqli=/usr/local/mysql/bin/mysql_config‘ ‘--with-libxml-dir‘ ‘--with-gd‘ ‘--with-jpeg-dir‘ ‘--with-png-dir‘ ‘--with-freetype-dir‘ ‘--with-iconv-dir‘ ‘--with-zlib-dir‘ ‘--with-bz2‘ ‘--with-openssl‘ ‘--with-mcrypt‘ ‘--enable-soap‘ ‘--enable-gd-native-ttf‘ ‘--enable-mbstring‘ ‘--enable-sockets‘ ‘--enable-exif‘Server API => Command Line Interface Virtual Directory Support => enabled Configuration File (php.ini) Path => /usr/local/php7/etc Loaded Configuration File => (none) Scan this dir for additional .ini files => (none) Additional .ini files parsed => (none) PHP API => 20160303PHP Extension => 20160303Zend Extension => 320160303Zend Extension Build => API320160303,TS PHP Extension Build => API20160303,TS Debug Build => no Thread Safety => enabled Zend Signal Handling => enabled Zend Memory Manager => enabled Zend Multibyte Support => provided by mbstring IPv6 Support => enabled DTrace Support => disabled Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, mcrypt.*, mdecrypt.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk This program makes use of the Zend Scripting Language Engine: Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies :
概念:域名(主機名)、DNS、解析域名、hosts
任何一個域名解析到這臺機器,均可以訪問的虛擬主機就是默認虛擬主機
vim /usr/local/apache2/conf/httpd.conf //搜索httpd-vhost,去掉#
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //改成以下
<VirtualHost *:80> ServerAdmin admin@aminglinux.com DocumentRoot "/data/wwwroot/aming.com" ServerName aming.com ServerAlias www.aming.com ErrorLog "logs/aming.com-error_log" CustomLog "logs/aming.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com</VirtualHost>
/usr/local/apache2/bin/apachectl –t
/usr/local/apache2/bin/apachectl graceful
一臺服務器能夠訪問多個網站,多個域名 ,每一個網站都是一個虛擬主機
假如你的網站既能訪問百度 有能夠訪問谷歌,同時都在一臺服務器上運行,就用了一個httpd的服務,這個就是一個網站 多個域名,每一個域名對應着一個虛擬主機
/DocumentRoot "/usr/local/apache2.4/htdocs"
<Directory "/usr/local/apache2.4/htdocs"> 這個參數定義了 這個網站的根目錄在那裏,域名就是上面的ServerNmae www.exaple.com :80 我用ip 也能夠訪問它,用www.aminglinux.com 也能訪問它
先教你們在windows下怎麼去寫hosts
C:\Windows\System32\drivers\etc\hosts 這個和我們的linux下的hosts 類似,能夠在裏面定義一個ip 定義一個域名,而後讓這個域名指向到這個ip去,打開方式用記事本
# Copyright (c) 1993-2009 Microsoft Corp. ## This is a sample HOSTS file used by Microsoft TCP/IP for Windows. ## This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. ## Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a ‘#‘ symbol. ## For example: ## 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost# ::1 localhost
在這裏裏面加上 192.168.202.131 www.abc.com www.123.com
# # For example: # # 102.54.94.97 rhino.acme.com # source server# 38.25.63.10 x.acme.com # x client host192.168.202.131 www.abc.com # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost
到底能不能指向這個192.168.202.131 呢,保存下這個文件
格式 先ip 域名 空格 域名 能夠寫多個,一行裏面能夠有多個域名
正常狀況下不寫這個hosts
再來訪問下
這ip 61.132.13.130 很明顯是一個公網ip,上面那個之因此能夠訪問到192。168.202.131那裏是由於咱們寫了hosts,咱們指向了這個ip 這個就是hosts 的做用,它的目的是就讓你臨時訪問到一個服務器上去,好比上面的www.abc.com 或者 123.com
把這個域名指向到這臺機器上去,這個是在什麼狀況下用呢,是在DNS 尚未生效的狀況下
咱們這個並無在httpd 的配置文件上去定義,僅僅在ServerNamewww.example.com ,這個叫作 apache的默認主機 任何一個域名 只要是指定要這個ip,它都會訪問到這個站點,那這個站點就叫作默認虛擬主機
若是你沒有作任何的操做,僅僅是使用了httpd.conf
搜索這個extra 找到Virtual hosts #Include conf/extra/httpd-vhost.conf 把這行#去掉
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf # Virtual hostsInclude conf/extra/httpd-vhosts.conf # Local access to the Apache HTTP Server Manual #Include conf/extra/httpd-manual.conf # Distributed authoring and versioning (WebDAV) #Include conf/extra/httpd-dav.conf:wq
打開一個二級配置文件就,這個既是虛擬主機配置文件,在這個配置文件裏 能夠定義多個ServerName ,vim /usr/local/apache2.4/conf/extra/httpd-vhoust-conf 虛擬主機配置文件
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf # Virtual Hosts## Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # Virtual Hosts## Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn‘t need to worry about # IP addresses. This is indicated by the asterisks in the directives below. ## Please see the documentation at # <URL: # for further details before you try to setup virtual hosts. ## You may use the command line option ‘-S‘ to verify your virtual host# configuration. ## VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not# match a ServerName or ServerAlias in any <VirtualHost> block. #<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common</VirtualHost><VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log"
改下這個配置文件
每個這個都表明這一個主機,一個主機就是一個網站
如今要想給它區分開,就得打開虛擬主機配置文件,打開以後作一個更改
DocumentRoot 這個用來定義你網站的更目錄在哪裏
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> :wq
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [root@localhost ~]# mkdir /data/wwwroot/ [root@localhost ~]# mkdir /data/wwwroot/abc.com [root@localhost ~]# mkdir /data/wwwroot/111.com [root@localhost ~]#
[root@localhost ~]# vim /data/wwwroot/abc.com/index.php<?php echo "abc.com";~ ~ [root@localhost ~]# vim /data/wwwroot/abc.com/index.php [root@localhost ~]# vim /data/wwwroot/111.com/index.php<?php ehco "111.com";~ ~
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#
新的命令curl -x
若是不綁定hosts 去ping www.abc.com,會訪問到外網上去,
[root@localhost ~]# ping www.abc.comPING abc.com (199.181.132.250) 56(84) bytes of data. 64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=1 ttl=128 time=238 ms 64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=2 ttl=128 time=241 ms 64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=3 ttl=128 time=236 ms 64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=4 ttl=128 time=237 ms 64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=5 ttl=128 time=236 ms ^C --- abc.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 6519ms rtt min/avg/max/mdev = 236.087/238.031/241.597/1.958 ms [root@localhost ~]#
vim /etc/hosts 文件
還能夠用curl -x192。168.202.131:80 abc.com
- [root@localhost ~]# curl -x192.168.202.131:80 abc.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abc.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abcd.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abcde.comabc.com[root@localhost ~]#
不管你輸入任何一個域名都指向到192.168.202.131,它都會訪問到htdocs下面
不管你訪問什麼域名 ,只要揭曉到這個ip,都會訪問到abc.com 默認的虛擬主機就是abc.com
再來訪問一個example
1.在主配置文件當中,httpd.conf裏面打開了虛擬主機配置文件,這時候虛擬主機配置文件生效了,主配置文件裏定義的 失效
2,虛擬主機配置文件一旦單開,裏面能夠定義多個vir host,每個virtural host 都docunmentroot ,以及servername ,每個vir host 杜表明一個站點,都表明一個虛擬主機
默認虛擬主機,不管任何域名解析到這個ip 上,都會去訪問默認虛擬主機