配置web服务器-搭建LAMP环境
修改web网站根目录-配置别名-虚拟目录
实现apache打开软链接功能-禁止显示目录列表-用户认证
apache虚拟主机基于-IP-域名-端口三种搭建方式
LAMP=linux apache Mysql PHP中的Apache (httpd)
1.LAMP工作原理
image.png2.web服务器的工作模式和端口
工作模式是:B/S 模式
工作端口是:
正常端口:80/http
SSL 端口 443/https
3.安装:
yum 搭建LAMP环境
LAMP=linux + apache + mysql + php
yum -y install httpd mariadb mariadb-server php php-mysql
httpd配置文件
/etc/httpd/conf/httpd.conf
重启服务
systemctl start httpd
systemctl enable httpd
systemctl start mariadb
systemctl enable mariadb
查看端口
netstat -antp | grep httpd
netstat -antp| grep mysql
[root@localhost html]# netstat -antp | grep httpd
tcp6 0 0 :::80 :::* LISTEN 2709/httpd
[root@localhost html]# netstat -antp| grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2558/mysqld
初始化数据库
mysql_secure_installation
登录测试
mysql -u root -p
mysql> show databases;
mysql>exit;
测试php
vim /var/www/htmlindex.php
<?php
phpinfo();
?>
配置
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
重启httpd
systemctl restart httpd
4.httpd配置文件详解
vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" #httpd服务的根目录
Listen 80 #监听端口,默认本地 IP,如果指定 ip 写上 IP:80
Include conf.modules.d/*.conf #当前目录下conf.modules.d 这个目录下所有conf文件都生效
Options FollowSymLinks #Options Indexes 目录浏览FollowSymLinks 用连接
浏览 #注释该行可避免index.php insex.html文件不存在时,客户端无法访问网站根目录而保护网站数据的安全
AllowOverride None #设置为 none,忽略.htaccess
Include conf.d/*.conf #conf.d 里面的 conf 文件也属有效配置文件
User apache #运行以哪个身份运行
Group apache #运行以哪个组的身份运行
ServerAdmin root@localhost #管理员邮箱
DocumentRoot "/var/www/html" #默认的主目录,如果改动要改动两处,Directory
<Directory "/var/www/html">
Options Indexes FollowSymLinks AllowOverride None
</Directory>
LogLevel warn #日志等级
AddDefaultCharset UTF-8 #支持的语言,默认编码
<IfModule dir_module>
DirectoryIndex index.html index.php #配置网站默认首页
</IfModule>
5.指定网站默认根目录、配置网站别名及虚拟目录
指定网站默认根目录
DocumentRoot "/var/www/html/bbs/" #指定目录为/var/www/html/bbs
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html/bbs">
使用别名,引用网站根目录以外的路径
/usr/local/phpdata/ 即访问ip/phpdata/指向/usr/local/phpdata/目录
[root@localhost bbs]# mkdir /usr/local/phpdata
[root@localhost bbs]# cd !$
cd /usr/local/phpdata
[root@localhost phpdata]# vim index.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Alias /phpdata/ "/usr/local/phpdata/"
<Directory "/usr/local/phpdata">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
重启httpd服务测试
6.设置网站需登录才可访问
设置/usr/local/phpdata/目录,只能通过用户名密码方式访问。
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
</Directory>
Alias /phpdata/ "/usr/local/phpdata/"
<Directory "/usr/local/phpdata">
Options Indexes FollowSymLinks
AllowOverride None
authtype basic
authname "my web site"
authuserfile /etc/httpd/conf/passwd.secret
require valid-user
</Directory>
[root@localhost ~]# htpasswd -cm /etc/httpd/conf/passwd.secret yundd
New password:
Re-type new password:
Adding password for user yundd
[root@localhost ~]# htpasswd -m /etc/httpd/conf/passwd.secret
spider #创建第二个用户时去掉-c参数,可通过htpasswd -L命令查看
New password:
Re-type new password:
Adding password for user spider'
添加允许登录的用户及密码
[root@localhost ~]# cat /etc/httpd/conf/passwd.secret
yundd:$apr1$fW6kv/Id$d.O6Mxa9tTR.4aMhaBs6t.
spider:$apr1$VbSzGLOm$ToOz.hRqIEEK16erGsteW1
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
[root@localhost ~]# systemctl restart httpd
测试
7.基于域名-ip-端口访问网站
基于域名访问
windows客户端添加解析记录
C:\Windows\System32\drivers\etc
[root@localhost conf]# vim ip.conf
<ViretualHost *:80>
ServerAdmin webmaster@dummy-host-example.com
DocumentRoot /var/www/html/
ServerName www.yunko1.cn
ErrorLog logs/www.yunko1.cn-error_log
</ViretualHost>
<ViretualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/yundd/
ServerName bbs.yunko1.cn
ErrorLog logs/bbs.yunko1.cn-error_log
CustomLog logs/bbs.yunko1.cn-access_log common
</ViretualHost>
测试
基于ip访问
[root@localhost ~]# ifconfig ens33:1 10.0.0.12 netmask 255.255.255.0
[root@localhost ~]# mkdir /var/www/html/yundd
[root@localhost ~]# cd !$
cd /var/www/html/yundd
[root@localhost yundd]# vim index.html
[root@localhost yundd]# vim /etc/httpd/conf/ip.conf
配置ip.conf
<ViretualHost 10.0.0.6:80>
ServerAdmin webmaster@dummy-host-example.com
DocumentRoot /var/www/html/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
</ViretualHost>
<ViretualHost 10.0.0.11:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/yundd/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</ViretualHost>
测试
基于端口访问
[root@localhost conf]# vim ip.conf
<ViretualHost 10.0.0.6:8080>
ServerAdmin webmaster@dummy-host-example.com
DocumentRoot /var/www/html/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
</ViretualHost>
<ViretualHost 10.0.0.13:8082>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/yundd/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</ViretualHost>
[root@localhost conf]# vim httpd.conf
#
#Listen 12.34.56.78:80
Listen 10.0.0.6:8080
Listen 10.0.0.13:8082
#
测试
网友评论