[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 ~]#