[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf //ctrtl+z退出下 [1]+ 已中止 vim /usr/local/apache2/conf/httpd.conf [root@yong-02 ~]# /usr/local/apache2/bin/apachectl restart //會看到警告信息 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::c44:f02d:4192:8d42. Set the 'ServerName' directive globally to suppress this message [root@yong0-02 ~]# fg //回到剛暫停的任務中——>vim /usr/local/apache2/conf/httpd.conf 在文件中搜索 /ServerName 而後將 # ServerName www.example.com:80 前的# 去除掉保存退出便可
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl restart httpd not running, trying to start /usr/local/apache2/bin/apachectl: 行 79: 1990 段錯誤 $HTTPD -k $ARGV
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl start
[root@yong-02 ~]# ps aux |grep httpd root 1995 0.1 0.6 253536 12576 ? Ss 21:02 0:00 /usr/local/apache2/bin/httpd -k restart daemon 2086 0.0 0.5 540364 9452 ? Sl 21:02 0:00 /usr/local/apache2/bin/httpd -k restart daemon 2087 0.0 0.5 540364 9452 ? Sl 21:02 0:00 /usr/local/apache2/bin/httpd -k restart daemon 2088 0.0 0.5 540364 9452 ? Sl 21:02 0:00 /usr/local/apache2/bin/httpd -k restart root 2171 0.0 0.0 112676 984 pts/0 R+ 21:02 0:00 grep --color=auto httpd
如果能夠運行 telnet 命令,那出來的則是一個結果,顯示80並不通php
[root@yong-02 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 347 32935 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 266 26673 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 266 26673 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 266 26673 INPUT_ZONES 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 262 26469 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 等等,只截取了一部分,會發現80端口並無打開
[root@yong-02 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@yong-02 ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 而後搜索 /denied ,會看到 <Directory /> AllowOverride none Require all denied </Directory> 把 denied 改爲 granted
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 是由於DocumentRoot指定了Directory,即以下: DocumentRoot "/usr/local/apache2/htdocs" <Directory "/usr/local/apache2/htdocs"> 還有後面的 Require all granted 如果將這裏的Require all granted改爲Require all denied ,那確定沒法瀏覽
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl -t Syntax OK
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl graceful
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 搜索 /AddType,而後 AddType application/x-compress .Z AddType application/x-gzip .gz .tgz 在這兩行下面,增長php一行 AddType application/x-httpd-php .php
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 搜索 /Index ,找到 <IfModule dir_module> DirectoryIndex index.html </IfModule> 在index.html後加 index.php 顯示爲 <IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl -t Syntax OK
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl graceful
[root@yong-02 ~]# vim /usr/local/apache2/htdocs/1.php 在文件中寫入 <?php phpinfo(); ?> 而後保存退出
http://192.168.180.135/1.php
當看到php正常顯示,如上圖,那說明php支持支持解析html
若是php不支持解析(以上四步驟錯誤一處),那麼刷新頁面顯示出來的則是源代碼(就是 1.php 文件中的代碼)mysql
若是遇到php沒法解析,則去檢查Apache的配置文件linux
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl -M Loaded 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@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 在文件中搜索/libphp5,看是否加載了這一行配置 LoadModule php5_module modules/libphp5.so
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 在文件搜索/AddType中關於php這一行配置是否存在 AddType application/x-httpd-php .php
[root@yong-02 ~]# ls /usr/local/apache2/htdocs/ 1.php index.html
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 在文件中搜索 /libphp7,而後註釋掉php5打開php7 # LoadModule php5_module modules/libphp5.so LoadModule php7_module modules/libphp7.so
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@yong-02 ~]# /usr/local/apache2/bin/apachectl graceful
[root@yong-02 ~]# /usr/local/php7/bin/php -i |less phpinfo() PHP Version => 7.1.6 System => Linux yong-02 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 Build Date => May 26 2018 00:00:30 Configure Command => './configure' '--prefix=/usr/local/php7' '--with-apxs2=/usr/local/apache2/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 => 20160303 PHP Extension => 20160303 Zend Extension => 320160303 Zend Extension Build => API320160303,TS 等等等,只截取了一部分
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 在文件中搜索/htdocs,其中DocumentRoot 定義了文件根目錄在哪裏 DocumentRoot "/usr/local/apache2/htdocs" <Directory "/usr/local/apache2/htdocs"> 搜索/ServerName,域名就是定義的ServerName ,只不過這個ServerName,,可使用任何一個域名去訪問它,好比可使用IP能夠訪問,使用example.com能夠訪問,用www也能夠去訪問它 ServerName www.example.com:80
<VirtualHost *:80>web
ServerAdmin admin@aminglinux.comsql
DocumentRoot "/data/wwwroot/aming.com"apache
ServerName aming.comvim
ServerAlias www.aming.comwindows
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>
## 默認虛擬主機 - 默認虛擬主機,能夠理解成Apache(也就是httpd),一個服務下面跑多了多個網站,跑了多個域名 - 例子,假如在服務器上,它既能訪問百度,又能訪問谷歌,這是兩個不一樣的網站,但同時都在一臺服務器運行着,就用了一個httpd的服務,這個就是一個網站多個域名,每個域名對着一個虛擬主機 ## 更改hosts - 在windows下去寫hosts 1. hostsl路徑地址,這個和linux下的hosts相似
1. 打開物理機C盤
2. 而後選擇Windows
3. 再選擇System32
4. 選擇dirvers
5. 選擇etc
6.選擇hosts
192.168.180.135 www.abc.com www.123.com
#192.168.180.135 www.abc.com www.123.com
192.168.180.135 www.abc.com www.123.com
[root@yong-02 ~]# vim /usr/local/apache2/conf/httpd.conf 搜索/extra 關鍵詞,這一行就作虛擬主機 # Virtual hosts #Include conf/extra/httpd-vhosts.conf 把#Include conf/extra/httpd-vhosts.conf前面的 #號 去掉 Include conf/extra/httpd-vhosts.conf # Virtual hosts Include conf/extra/httpd-vhosts.conf
[root@yong-02 ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //虛擬主機配置文件 在打開配置文件,會發現有兩個<VirtualHost *:80> ,每個<VirtualHost >都是一對出現的,每個<VirtualHost >都表明着一個主機,一個主機就是一個網站 <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com //定義管理員的郵箱——>可刪除 DocumentRoot "/usr/local/apache2/docs/dummy-host.example.com" //定義網站的根目錄在哪裏 ServerName dummy-host.example.com //服務器名字 ServerAlias www.dummy-host.example.com //定義別名,別名就是一個網站能夠有多個域名訪問,好比能夠是abc.com訪問,也能夠是www.abc.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/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log" CustomLog "logs/dummy-host2.example.com-access_log" common </VirtualHost>
<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> 而後保存退出
[root@yong-02 ~]# mkdir /data/wwwroot/ [root@yong-02 ~]# mkdir /data/wwwroot/abc.com [root@yong-02 ~]# mkdir /data/wwwroot/111.com
[root@yong-02 ~]# vim /data/wwwroot/abc.com/index.php 在文件中寫入 <?php echo "abc.com"; 並保存退出
[root@yong-02 ~]# vim /data/wwwroot/111.com/index.php 在文件中寫入 <?php echo "111.com"; 並保存退出
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl -t Syntax OK
[root@yong-02 ~]# /usr/local/apache2/bin/apachectl graceful
[root@yong-02 ~]# curl -x192.168.180.135:80 abc.com abc.com[root@yong-02 ~]# curl -x192.168.180.135:80 abcd.com abc.com[root@yong-02 ~]#
[root@yong-02 ~]# curl -x192.168.180.135:80 www.example.com 111.com[root@yong-02 ~]#