Linux从入门到精通——Apache
###apache###
企業(yè)中常用的web服務(wù),用來(lái)提供http:// (超文本傳輸協(xié)議)
httpd是apaceh服務(wù)器守護(hù)的進(jìn)程
1.apache安裝
?? yum install httpd -y ? ? ? ? ? ? ? ? ##apache軟件的安裝
?? yum install httpd—manual ? ? ?##apache的手冊(cè)
?? systemctl start httpd???????
?? systemctl enable httpd???????
?? systemctl start firewalld
?? firewall-cmd --list-all ? ? · ? ? ##列出火墻信息
?? firewall-cmd --permanent --add-server=http?? ##永久允許http
?? firewall-cmd --reload ? ? ? ? ? ? ##火墻的重新加載策略
?? netstat -antlupe|grep httpd ? ?##查看監(jiān)聽端口,默認(rèn)端口80
??
?? cd /var/www/html ? ? ? ? ? ? ? ? ?##apache的 / 目錄,默認(rèn)發(fā)布目錄
?? vim index.html ? ? ? ? ? ? ? ? ? ? ?##默認(rèn)發(fā)布文件? /var/www/html/index.html
??? <h1> hahahaha wangfang </h1>
?
?? 測(cè)試:
?? 在火狐瀏覽器上輸入 172.25.254.110?? ##會(huì)出現(xiàn) hahahaha wangfang
?
?? ls???
?? index.html
?? vim westos
??? <h1> wangfang westos's index.html </h1>?
?
????? 在火狐瀏覽器上輸入172.25.254.110/westos??? ##會(huì)出現(xiàn)wangfang westos's index.html??
?
?? yum install httpd—manual???? ##apache的手冊(cè)
?? 測(cè)試:
?? 172.25.254.110/manual
2.apache的基礎(chǔ)信息
?? 主配置目錄 /etc/httpd/conf
?? 主配置文件 /etc/httpd/conf/httpd.conf
?? 子配置目錄 /etc/httpd/conf.d/
?? 子配置文件 /etc/httpd/conf.d/*.conf
??
?? 默認(rèn)發(fā)布目錄 /var/www/html
?? 默認(rèn)發(fā)布文件 index.html
?? 默認(rèn)端口 80
?? 默認(rèn)安全上下文 httpd_sys_content_t
?? 程序開啟默認(rèn)用戶 apache
?? apache日志 /etc/httpd/logs/*
?? (1)修改默認(rèn)端口:
??? vim /etc/httpd/conf/httpd.conf
??? 43 Listen 8080 ? ? ? ? ? ? ? ? ?##修改默認(rèn)端口為8080
??? firewall-cmd --permanent --add-port=8080/tcp??
??? firewall-cmd --reload
??? netstat -antlupe|grep httpd? ##查看監(jiān)聽端口,默認(rèn)端口80
?
?? (2)修改默認(rèn)發(fā)布文件:
??? vim /etc/httpd/conf/httpd.conf???
??? DocumetRoot "/westos/html"??????? ##目錄
??? <Directory "/westos">????????????
??????? Require all granted
??? </Directory>
?
??? <IfModule dir_module>???????????? ##文件
??? ?? DirectoryIndex westos index.html
??? </IfModule>
?
??? 測(cè)試:
??? 172.25.254.110???? ##會(huì)出現(xiàn)westos里的內(nèi)容
3.apache的訪問(wèn)控制
?? (1)
?? vim /etc/httpd/conf/httpd.conf
?? <Directory "/var/www/html/westos">
??? Order Allow,Deny ? ? ? ? ? ? ? ? ? ?##這里的命令,先讀取allow,后讀取deny
??? Allow from All ? ? ? ? ? ? ? ? ? ? ? ? ?##先允許全部人訪問(wèn)
??? Deny from 172.25.254.110???? ##再在允許訪問(wèn)的名單中將110這個(gè)ip覆蓋掉
?? </Directory> ? ? ? ? ? ? ? ? ? ? ? ? ? ?##除了110都可以訪問(wèn)
??
? <Directory "/var/www/html/westos">
??? Order Deny,Allow ? ? ? ? ? ? ? ? ##這里的命令,先讀取deny,后讀取allow
??? Allow from 172.25.254.110???? ##先禁止全部人訪問(wèn)
??? Deny from all??? ?? ? ? ? ? ?##再在禁止訪問(wèn)的名單中將110這個(gè)ip,設(shè)為允許訪問(wèn)
?? </Directory>???????????? ?? ? ? ? ? ##除了110都不能訪問(wèn)
?
?? (2)設(shè)定用戶登陸
?? cd /etc/httpd/conf/
?? htpasswd -cm westosuser wf?????? ##建立用戶(會(huì)要求設(shè)置密碼)
?? htpasswd -m westosuser lsy?????
?? 注意:當(dāng)有一個(gè)用戶存在的時(shí)候。就不能用 -cm 來(lái)建立新的用戶,如果這樣建立會(huì)將原來(lái)的用戶覆蓋掉。重新添加用戶應(yīng)該用 -m 。如果本身沒(méi)有用戶存在,則需要用 -cm
?
?? 創(chuàng)建好用戶后可以用 cat westosuser 這個(gè)命令查看
??
?? 在設(shè)置好用戶后,就可以設(shè)置用戶輸入賬戶和密碼查看:(ps:要將上面的注釋掉)
?? vim /etc/httpd/conf/httpd.conf
?? <Directory "/var/www/html/westos">
??? # Order Allow,Deny????????????
??? # Allow from All???? ????
??? # Deny from 172.25.254.110?
??
??? AuthUserFile /etc/httpd/conf/westosuser
??? AuthTYpe basic
??? AuthName "please input your name and passwd !!"
? ? ?#Require user wf ? ? ? ? ? ##只允許wf用戶訪問(wèn)
? ? Require valid-user???? ##允許所有有效用戶訪問(wèn),必須要寫,不寫的話,就沒(méi)有密碼驗(yàn)證的對(duì)話框
?? </Directory>??????????????????
?? systemctl restart httpd
?
?? 測(cè)試:
?? 172.25.254.110/westos
?
4.apache的虛擬主機(jī)(為了訪問(wèn)不同的頁(yè)面)
?? cd /var/www/
?? mkdir -p virtual/news/html
?? mkdir -p virtual/news/html
?? vim /var/www/virtual/news/html/index.html
??? <h1> news's page </h1>
?? vim /var/www/virtual/music/html/index.html
??? <h1> music's page </h1>
?
?? cd /etc/httpd/conf.d/
?? vim a_default.conf
??? <Virtualhost _default_:80>
??????? DocumentRoot /var/www/html
??????? CustomLog logs/dedfault.log combined
??? </Virtualhost>
?? vim news.conf
??? <VirtualHost *:80>
??????? ServerName news.westos.com
??????? DocumentRoot /var/www/virtual/news/html
??????? CustomLog logs/news.log combined
??? </VirtualHost>
??? <Directory "/var/www/virtual/news/html">
??????? Require all granted
??? </Directory>
?? vim music.conf
??? <VirtualHost *:80>
??????? ServerName music.westos.com
??????? DocumentRoot /var/www/virtual/music/html
??????? CustomLog logs/music.log combined
??? </VirtualHost>
??? <Directory "/var/www/virtual/music/html">
??????? Require all granted
??? </Directory>
?
?? systemctl restart httpd
?? 測(cè)試:
?? vim /etc/hosts?? ##做域名解析
?? 172.25.254.110?? www.westos.com?? news.westos.com?? music.westos.com
?
5.php 和 cgi
?? cd /var/www/html/
?? vim index.php
??? <?php
??????? phpinfo();
??? ?>
?? yum install php -y
?
? 測(cè)試:
?? 172.25.254.110/index.php
?
?? cd /var/www/html/
?? mkdir cgi
?? cd cgi/
?? vim index.cgi
??? #!/usr/bin/perl
??? print "Content-type:text/html\n\n";
??? print "Hello,wf.";
?? chmod 755 index.cgi
?? ./index.cgi
? ? vim a_dedault.conf
??? <Virtualhost _default_:80>
??????? DocumentRoot /var/www/html
??????? CustomLog logs/dedfault.log combined
??? </Virtualhost>
??? <Directory "/var/www/html/cgi">
??????? Options +ExecCGI
??????? AddHandler cgi-script .cgi
??? </Directory>
?
?? 測(cè)試:
? ? 172.25.254.110/cgi/index.cgi
?
6.apache的簽證
?? yum install mod_ssl -y
?? cd /etc/httpd/conf
?? vim ssl.conf
??? SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ? ? ? ? ? ? ##證書
??? SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key?? ##密鑰
?? systemctl restart httpd
?? yum install crypto-utiles -y
?? genkey www.westos.com?? ????????##獲得證書
?? systemctl restart httpd
?
6.網(wǎng)頁(yè)重寫(加密訪問(wèn)) 將 hppt:// 變成 hppts://
?? cd /etc/httpd/conf.d
?? cp ?news.conf ?login.conf
?? vim login.conf
??? <VirtualHost *:80>
??????? ServerName login.westos.com
??????? DocumentRoot /var/www/virtual/login/html
??????? CustomLog logs/login.log combined
??? </VirtualHost>
??? <Directory "/var/www/virtual/login/html">
??????? Require all granted
??? </Directory>
?? vim /var/www/virtual/login/html/index.html
?? <h1> login's page </h1>
?? 測(cè)試:
?? 需要手動(dòng)加 https://
?? vim news.conf
??? <VirtualHost *:443>
??????? ServerName login.westos.com
??????? DocumentRoot /var/www/virtual/login/html
??????? CustomLog logs/login.log combined
??????? SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt??????
??????? SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
??????? SSLEngine on
??? </VirtualHost>
??? <Directory "/var/www/virtual/login/html">
??????? Require all granted
??? </Directory>
??? <VirtualHost *:80>
??????? ServerName login.westos.com
??????? RewriteEngine on
??????? RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
??? </VirtualHost>
?? 測(cè)試:
?? 不需要手動(dòng)加??? login.westos.com
?? 注意:
?? ^(/.*)$?? ##客戶在瀏覽器地址欄中輸入的所有字符
?? https://? ##強(qiáng)制客戶加密訪問(wèn)
?? %{HTTP_HOST}?? ##客戶請(qǐng)求主機(jī)
?? $1???????????? ##表示 ^(/.*)$ 的值
?? [redirect=301] ##臨時(shí)重寫? 302永久轉(zhuǎn)換
?
7.squid(FQ)
?? 在沒(méi)有權(quán)限查看某網(wǎng)站,另一個(gè)主機(jī)可以訪問(wèn)的時(shí)候,可以在可以訪問(wèn)的主機(jī)上裝squid,然后開啟服務(wù),開通端口,在訪問(wèn)不了的主機(jī)上,輸入可查看主機(jī)的ip和端口,就可以查看某網(wǎng)站
??
?? yum install squid -y
?? systemctl start squid
?? vim /etc/squid/squid.conf
??? http_access allow all
??? http_port 3128
??? cache_dir ufs /var/spool/squid 100 16 256
?
?? 測(cè)試:
?? 在真機(jī)上,preferences -> Advanced -> network -> setting -> Manual proxy configuration -> 172.25.254.110 3128
?
8.輔助squid
?? yum install squid -y
?? systemctl start squid
?? vim /etc/squid/squid.conf
??? http_access allow all
??? http_port 80 vhost vport
??? cache_peer 172.25.254.110 parent 80 0 proxy-only round-robin originserver name=web1? weight=3
??? cache_peer 172.25.254.111 parent 80 0 proxy-only round-robin originserver name=web2
??? cache_peer_domain web1 web2 www.westos.com
??? cache_dir ufs /var/spool/squid 100 16 256
?
? ??
?
轉(zhuǎn)載于:https://www.cnblogs.com/wf-aiyouwei/p/9482900.html
總結(jié)
以上是生活随笔為你收集整理的Linux从入门到精通——Apache的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [SHOI2017]组合数问题
- 下一篇: Linux 下源码编译安装 vim 8.