Linux之web服务的搭建推荐

今天我来给大家分享一下在Linux下搭建web服务器

1、安装包

yuminstallhttpd

安装完成后我们会在/etc目录下看到有一个httpd的目录

我们的默认文件存放位置在/var/www/html

然后修改我们的注配置文件

vim/etc/httpd/conf/httpd.confServerName自己的主机名:80

表示服务名称为自己的主机名,监听在80端口

然后就可以建文件了

vim/var/www/html/index.htmlhello

这就是我们的主页文件了

然后我们开启一下我的HTTP服务

servicehttpdstart

然后我们就可以在另外一台虚拟机上测试了

这就简单的搭建好一台web服务器了。

注:如果显示无法连接,可能是你的防火墙给挡在墙外了,需要关闭防火墙才行。

2、创建基于文件的访问控制

首先我们需要创建一个目录

cd/var/www/htmlmkdiradminmkdirbbsvimbbs/index.htmlhelloavim/etc/httpd/conf/httpd.conf Directory"/var/www/html/admin" OptionsnoneAllowoverrideAuthConfigAuthTypeBasicAuthName"AdminArea."AuthUserFile/etc/httpd/conf/.httpasswdRequirevalid-user /Directory 

然后做用户名和密码存放文件

htpasswd-c-m/etc/httpd/conf/.httpasswdtomhtpasswd-m/etc/httpd/conf/.httpasswdjryjry

然后重读一下配置文件

servicehttpdreload

当我们再次访问时就会弹出输入用户名和密码了

3、基于组的访问控制配置

在2的基础上,我们创建一个文件

vim/etc/httpd/conf/.httpgrouptest:tom

添加test组,组成员为tom,然后再编辑配置文件,把上个实例中的文件内容修改如下

vim/etc/httpd/conf/httpd.conf Directory"/var/www/html/admin" OptionsnoneAllowoverrideAuthConfigAuthTypeBasicAuthName"AdminArea."AuthUserFile/etc/httpd/conf/.httpasswdAuthGroupFile/etc/httpd/conf/.httpgroupRequiregrouptest /Directory 

然后重读配置文件

servicehttpdreload

然后我们分别登陆tom和jry查看

4、基于端口做虚拟主机

编辑主配置文件

vim/etc/httpd/conf/httpd.confListen8080开启监听8080端口#DocumentRoot"/var/www/html"注释掉中心主机配置,这样虚拟主机配置才能生效 VirtualHost172.16.249.29:80 ServerNamewww.jsh.comDocumentRoot"/web/hosta" /VirtualHost  VirtualHost172.16.249.29:8080 ServerNamewww.jsh.comDocumentRoot"/web/hostb" /VirtualHost 

配置文件语法检查

httpd-t

创建对应的目录,并提供页面文件

mkdir-pv/web/host{a,b}vim/web/hosta/index.htmlhellohostavim/web/hostb/index.htmlhellohostb

然后重读配置文件

servicehttpdrestart

然后就可以测试了

5、基于IP的虚拟主机

步骤跟4实例中的一样,只用把第二个IP地址改为其他IP即可,并把8080端口改为80。

6、基于ssl的配置

首先我们需要自建CA[root@jsh~]#cd/etc/pki/CA/生成密钥文件[root@jshCA]#(umask077;opensslgenrsa-outprivate/cakey.pem2048)创建自签证书[root@jshCA]#opensslreq-new-x509-keyprivate/cakey.pem-outcacert.pem-days1000Youareabouttobeaskedtoenterinformationthatwillbeincorporatedintoyourcertificaterequest.WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN.TherearequiteafewfieldsbutyoucanleavesomeblankForsomefieldstherewillbeadefaultvalue,Ifyouenter'.',thefieldwillbeleftblank.-----CountryName(2lettercode)[XX]:CNStateorProvinceName(fullname)[]:HenanLocalityName(eg,city)[DefaultCity]:ZZOrganizationName(eg,company)[DefaultCompanyLtd]:jshOrganizationalUnitName(eg,section)[]:opsCommonName(eg,yournameoryourserver'shostname)[]:caserver.jsh.comEmailAddress[]:[root@jshCA]#touchserialindex.txt[root@jshCA]#echo00 serial在另外一台http服务器上创建密钥文件[root@jsh~]#cd/etc/httpd/[root@jshhttpd]#mkdirssl[root@jshhttpd]#cdssl[root@jshssl]#(umask077;opensslgenrsa-outhttpd.key1024)生成请求证书[root@jshssl]#opensslreq-new-keyhttpd.key-outhttpd.csrYouareabouttobeaskedtoenterinformationthatwillbeincorporatedintoyourcertificaterequest.WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN.TherearequiteafewfieldsbutyoucanleavesomeblankForsomefieldstherewillbeadefaultvalue,Ifyouenter'.',thefieldwillbeleftblank.-----CountryName(2lettercode)[XX]:CNStateorProvinceName(fullname)[]:HenanLocalityName(eg,city)[DefaultCity]:ZZOrganizationName(eg,company)[DefaultCompanyLtd]:jshOrganizationalUnitName(eg,section)[]:opsCommonName(eg,yournameoryourserver'shostname)[]:jshEmailAddress[]:Pleaseenterthefollowing'extra'attributestobesentwithyourcertificaterequestAchallengepassword[]:Anoptionalcompanyname[]:[root@jshssl]#opensslca-inhttpd.csr-outhttpd.crt-days1000编辑配置文件[root@jshssl]#cd/etc/httpd/conf.d/[root@jshconf.d]#vimssl.conf修改监听端口为443SSLCertificateFile/etc/httpd/ssl/httpd.crtSSLCertificateKeyFile/etc/httpd/ssl/httpd.keyDocumentRoot"/web/vhosta"[root@jshconf.d]#好啦,这样就完成了!!!

我只愿,在你的理想和希望里能为你增加一点鼓励,

Linux之web服务的搭建推荐

相关文章:

你感兴趣的文章:

标签云: