首页 > 代码库 > apache服务

apache服务





apache服务


server端:

yum search http        

yum install httpd.x86_64 -y        ##安装http服务##

systemctl start httpd        ##开启http服务##

systemctl  enable httpd        ##开机启动http服务##

firewall-cmd --list-all        ##查看火墙的服务开启状态##

firewall-cmd --permanent --add-service=http         ##永久开启http服务##

firewall-cmd --permanent --add-service=https        ##永久开启https服务##

firewall-cmd --reload        ##刷新火墙状态##

firewall-cmd --list-all        

cd /var/www/html        ##apache服务共享文件的默认目录##

vim westos.html

            hello world       

测试:firefox输入server端ip(如:172.25.254.44),则显示westos.html中的内容
注:输入后一般默认是http://172.25.254.44,若不是修改则需要修改为http://


apache端口:

apache服务的默认端口为80

vim /etc/httpd/conf/httpd.conf            ##apache服务的主配置文件## 

 

40 #

 41 #Listen 12.34.56.78:80

 42 Listen 8080               ##修改默认端口80为8080##

 43


systemctl restart httpd            ##重启httpd服务##

firewall-cmd --permanent --add-port=8080/tcp            ##在火墙中永久开启8080端口##

firewall-cmd --reload

测试:firefox输入server端ip端口8080(如:172.25.254.44:8080),则显示westos.html中的内容


apache的主配置文件:/etc/httpd/conf/httpd.conf 

119 #DocumentRoot "/var/www/html"

120 #

121 DocumentRoot "/www/html"

122 # Relax access to content within /var/www.

123 #

124 <Directory "/www/html">             ##默认访问目录为/www/html##

125     AllowOverride None

126     # Allow open access:

127     Require all granted


162 #

163 <IfModule dir_module>

164     DirectoryIndex westos.html linux.html           ##默认先访问westos.html文件##

165 </IfModule>

 

 

cd /var/www/html

vim linux.html

                    welcome to the page

cd /etc/httpd/conf.d/

vim default.conf             ##修改虚拟主机配置文件##

        <Virtualhost *:80>           ##虚拟主机的块,默认端口为80##

                  Documentroot /var/www/html          ##提供内容的目录

                  customlog "logs/default.log" combined

        </Virtualhost>

 

        <Directory /var/www/html>

                  require all granted

        </Directory>

mkdir /var/www/news

mkdir /var/www/music

cd /var/www/news/

vim  westos

            welcome to the page of news

cd /etc/httpd/conf.d

 vim news.conf         ##修改虚拟主机news的配置文件##

        <Virtualhost *:80>

                 Servername news.westos.com                 ##访问虚拟主机news时的域名##

                 Documentroot /var/www/news  

                  customlog "logs/news.log" combined

        </Virtualhost>

 

        <Directory /var/www/news>

                  require all granted

        </Directory>

cd /var/www/music/

vim linux

            welcome to the page of music

cd /etc/httpd/conf.d/

 vim music.conf        ##修改虚拟主机music的配置文件##

        <Virtualhost *:80>

                 Servername music.westos.com                ##访问虚拟主机music时的域名##

                   Documentroot /var/www/music

                  customlog "logs/music.log" combined

        </Virtualhost>

 

        <Directory /var/www/music>

                  require all granted

        </Directory>


selinux标签:

semanage fcontext -l

semanage fcontext -a -t httpd_sys_content_t "/directory(/.*)?"

restorecon -vvFR /directory

 

systemctl restart httpd

server端域名解析:

vim /etc/hosts

    172.25.254.44 www.westos.com news.westos.com music.westos.com

测试:

fireworx访问

www.westos.com

news.westos.com

music.wetos.com

 

基于用户的身份认证:

 htpasswd -cm apacheusr admin            ##创建admin密码文件##

htpasswd -m apacheusr tom         ##创建tom密码文件##

cat apacheusr            ##查看密码文件##


vim news.conf                              

        <Virtualhost *:80>

                 Servername news.westos.com

                 Documentroot /var/www/news

                 customlog "logs/news.log" combined

        </Virtualhost>

 

        <Directory /var/www/news>

                require all granted

        </Directory>

        <Directory /var/www/news/admin>

                  Authuserfile /etc/http/conf/apacheusr               ##用户密码文件目录##

                  Authname "Please input your name and password"             ##登陆提示##

                  Authtype basic

                  Require valid-user               ##所有可用用户##

        </Directory>


测试:

firefox访问news.westos.com/admin

 

 

 

自定义签名证书:

yum install mod_ssl -y        

cd /etc/httpd/conf.d

yum install crypto-utils -y

genkey  apache.example.com

vim ssl.conf

        <Virtualhost *:443>

        Servername login.westos.com

        Documentroot /var/www/login

        Customlog logs/login.log combined

        SSLEngine on

        SSLCertificateFile /etc/pki/tls/certs/apache.example.com.crt

        SSLCertificateFile /etc/pki/tls/private/apache.example.com.key

        </Virtualhost>

        <Directory /var/www/login>

             Require all granted

        </Directory>

        <Virtualhost *:80>

             Servername login.westos.com

             RewriteEngine on

             RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]  

        </Virtualhost>


systemctl restart httpd            ##重启服务##

mkdir -p  /var/www/login            

vim /var/www/login/index.com

systemctl restart httpd

 

 

 

 

 


apache服务