今天我来给大家分享一下在Linux下搭建web服务器
1、安装包
yum install httpd
安装完成后我们会在/etc目录下看到有一个httpd的目录
我们的默认文件存放位置在/var/www/html
然后修改我们的注配置文件
vim /etc/httpd/conf/httpd.confServerName 自己的主机名:80
表示服务名称为自己的主机名,监听在80端口
然后就可以建文件了
vim /var/www/html/index.htmlhello
这就是我们的主页文件了
然后我们开启一下我的HTTP服务
service httpd start
然后我们就可以在另外一台虚拟机上测试了
这就简单的搭建好一台web服务器了。
注:如果显示无法连接,可能是你的防火墙给挡在墙外了,需要关闭防火墙才行。
2、创建基于文件的访问控制
首先我们需要创建一个目录
cd /var/www/htmlmkdir adminmkdir bbsvim bbs/index.htmlhello a vim /etc/httpd/conf/httpd.confOptions none Allowoverride AuthConfig AuthType Basic AuthName "Admin Area." AuthUserFile /etc/httpd/conf/.httpasswd Require valid-user
然后做用户名和密码存放文件
htpasswd -c -m /etc/httpd/conf/.httpasswd tomtomtomhtpasswd -m /etc/httpd/conf/.httpasswd jryjryjry
然后重读一下配置文件
service httpd reload
当我们再次访问时就会弹出输入用户名和密码了
3、基于组的访问控制配置
在2的基础上,我们创建一个文件
vim /etc/httpd/conf/.httpgrouptest:tom
添加test组,组成员为tom,然后再编辑配置文件,把上个实例中的文件内容修改如下
vim /etc/httpd/conf/httpd.confOptions none Allowoverride AuthConfig AuthType Basic AuthName "Admin Area." AuthUserFile /etc/httpd/conf/.httpasswd AuthGroupFile /etc/httpd/conf/.httpgroup Require group test
然后重读配置文件
service httpd reload
然后我们分别登陆tom和jry查看
4、基于端口做虚拟主机
编辑主配置文件
vim /etc/httpd/conf/httpd.conf添加Listen 8080 开启监听8080端口#DocumentRoot "/var/www/html" 注释掉中心主机配置,这样虚拟主机配置才能生效ServerName www.jsh.com DocumentRoot "/web/hosta" ServerName www.jsh.com DocumentRoot "/web/hostb"
配置文件语法检查
httpd -t
创建对应的目录,并提供页面文件
mkdir -pv /web/host{a,b}vim /web/hosta/index.htmlhello hostavim /web/hostb/index.htmlhello hostb
然后重读配置文件
service httpd restart
然后就可以测试了
5、基于IP的虚拟主机
步骤跟4实例中的一样,只用把第二个IP地址改为其他IP即可,并把8080端口改为80。
6、基于ssl的配置
首先我们需要自建CA[root@jsh ~]# cd /etc/pki/CA/生成密钥文件[root@jsh CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)创建自签证书[root@jsh CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 1000You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:HenanLocality Name (eg, city) [Default City]:ZZOrganization Name (eg, company) [Default Company Ltd]:jshOrganizational Unit Name (eg, section) []:opsCommon Name (eg, your name or your server's hostname) []:caserver.jsh.comEmail Address []:初始化[root@jsh CA]# touch serial index.txt[root@jsh CA]# echo 00 > serial在另外一台http服务器上创建密钥文件[root@jsh ~]# cd /etc/httpd/[root@jsh httpd]# mkdir ssl[root@jsh httpd]# cd ssl[root@jsh ssl]# (umask 077; openssl genrsa -out httpd.key 1024)生成请求证书[root@jsh ssl]# openssl req -new -key httpd.key -out httpd.csrYou are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:HenanLocality Name (eg, city) [Default City]:ZZOrganization Name (eg, company) [Default Company Ltd]:jshOrganizational Unit Name (eg, section) []:opsCommon Name (eg, your name or your server's hostname) []:jshEmail Address []: Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:签署证书[root@jsh ssl]# openssl ca -in httpd.csr -out httpd.crt -days 1000编辑配置文件[root@jsh ssl]# cd /etc/httpd/conf.d/[root@jsh conf.d]# vim ssl.conf修改监听端口为443SSLCertificateFile/etc/httpd/ssl/httpd.crtSSLCertificateKeyFile/etc/httpd/ssl/httpd.keyDocumentRoot "/web/vhosta" [root@jsh conf.d]# 好啦,这样就完成了!!!