Apache
Apache是世界上使用排名第一的Web服務器軟件,它可以運行在所有廣泛使用的計算機平臺上,由于其跨平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一。
一、Apache的前期準備
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -antlupe | grep 80
tcp6 0 0 :::80 :::* LISTEN 0 36254 2133/httpd
[root@localhost ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::80 :::* LISTEN 0 36254 2133/httpd
[root@localhost ~]# cd /var/www/html/ ? ?
##目錄/var/www/html是默認訪問目錄
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# vim index.html ? ?
##index.html文件是默認訪問文件
[root@localhost html]# cat index.html
<h1>/var/www/html/index's page<h1> ? ? ? ?
##<h1>內容<h1> 這是html的寫法,表示的格式是標題一
[root@localhost html]# vim tutu.html ? ?
?##tutu.html不是默認訪問文件,所以在訪問時,需訪問http://ip/tutu.html
[root@localhost html]# cat tutu.html
<h1>/var/www/html/tutu's page<h1>
[root@localhost html]#
瀏覽器查看——http://172.25.254.127/
瀏覽器查看——http://172.25.254.127/tutu.html
二、Apache的基本配置
1、協議端口的修改
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ? ?
##httpd的配置文件
Listen 80 ——> Listen 8080
##修改第42行
[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -antlupe | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 0 110156 9396/httpd
瀏覽器訪問——http://172.25.254.127
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
Listen 8080 ——> Listen 80 ? ?
##修改第42行
[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -antlupe | grep httpd
tcp6 0 0 :::80 :::* LISTEN 0 111224 9449/httpd
瀏覽器訪問——http://172.25.254.127
2、默認訪問目錄、文件的設置
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# mkdir /westos/html -p
[root@localhost html]# cd /westos/html/
[root@localhost html]# pwd
/westos/html
[root@localhost html]# vim index.html
[root@localhost html]# cat index.html
<h1>/westos/html/index's page</h1>
瀏覽器訪問——http://172.25.254.127
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ? ? ?
##注釋掉第119行,添加120——123行內容如下
119 #DocumentRoot "/var/www/html" ? ? ? ? ?
120 DocumentRoot "/westos/html"
121 <Directory "/westos">
122 require all granted
123 </Directory>
[root@localhost html]# systemctl restart httpd
瀏覽器訪問——http://172.25.254.127(默認目錄)
[root@localhost html]# ls
index.html
[root@localhost html]# vim test.html
[root@localhost html]# cat test.html
<h1>/westos/html/test's page</h1>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ?
?##添加第123行,設置默認訪問文件為test.html
120 DocumentRoot "/westos/html"
121 <Directory "/westos">
122 require all granted
123
DirectoryIndex test.html
124 </Directory>
[root@localhost html]# systemctl restart httpd
瀏覽器訪問——http://172.25.254.127(默認文件)
[root@localhost html]# pwd
/westos/html
[root@localhost html]# ls
index.html test.html
[root@localhost html]# mkdir linux
[root@localhost html]# cd linux/
[root@localhost linux]# vim index.html
[root@localhost linux]# cat index.html
<h1>/westos/html/linux/index's page<h1>
[root@localhost linux]# vim test.html
[root@localhost linux]# cat test.html
<h1>/westos/html/linux/test's page<h1>
瀏覽器訪問——http://172.25.254.127/linux
[root@localhost linux]# vim /etc/httpd/conf/httpd.conf ?
?##添加121——123行,使得訪問linux目錄時,默認文件是index.html
120 DocumentRoot "/westos/html"
121
<Directory "/westos/html/linux">
122 DirectoryIndex index.html
123 </Directory>
124 <Directory "/westos">
125 require all granted
126 DirectoryIndex test.html
127 </Directory>
[root@localhost linux]# systemctl restart httpd
3、基于ip的身份認證
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
##恢復原配置,即刪除120——127行或者注釋掉,這里是刪除了
[root@localhost html]# systemctl restart httpd
[root@localhost html]# pwd
/westos/html
[root@localhost html]# cd /var/www/html/
[root@localhost html]# ls
index.html tutu.html
[root@localhost html]# mkdir westos
[root@localhost html]# ls
index.html tutu.html westos
[root@localhost html]# cd westos/
[root@localhost westos]# ls
[root@localhost westos]# vim index.html
[root@localhost westos]# cat index.html
<h1>/var/www/html/westos/index's page</h1>
[root@localhost westos]# vim /etc/httpd/conf/httpd.conf ? ?
##添加第120——124行,相當于黑名單
##效果:除了ip為172.25.254.50的不能訪問外,其他ip都可以訪問
119 DocumentRoot "/var/www/html"
120 <Directory "/var/www/html/westos">
121 Order Allow,Deny ? ? ?
?##先讀Allow,再讀Deny
122 Allow from All ? ? ?
??##允許所有ip訪問
123 Deny from 172.25.254.50
? ##禁止172.25.254.50訪問
124 </Directory>
[root@localhost westos]# systemctl restart httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
##修改第120——125行,相當于白名單
##效果:除了ip為172.25.254.50的不能訪問外,其他ip都可以訪問
119 DocumentRoot "/var/www/html"
120 #DocumentRoot "/westos/html"
121 <Directory "/var/www/html/westos">
122 Order Deny,Allow ? ?
?##先讀Deny,再讀Allow
123 Allow from 172.25.254.27
##允許172.25.254.50訪問
124 Deny from All ? ? ? ? ??
##禁止所有ip訪問
125 </Directory>
[root@localhost ~]# systemctl restart httpd
[root@localhost westos]# cd /etc/httpd/
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# htpasswd -cm apacheuser admin
##htpasswd [-cimBdpsDv] [-C cost] passwordfile username
##-c? Create a new file.
##-m? Force MD5 encryption of the password (default).
##添加第二個用戶時,不要加上“-c ”
New password:
Re-type new password:
Adding password for user admin
[root@localhost httpd]# cat apacheuser
admin:$apr1$.g7wvziV$0TpPETAiCBx5Gzfh5n50G/
[root@localhost httpd]# htpasswd -cm apacheuser tom
New password:
Re-type new password:
Adding password for user tom
[root@localhost httpd]# cat apacheuser
tom:$apr1$2faTciFK$iHsm6EAb1.SHdkHFL5ur6.
[root@localhost httpd]# htpasswd -m apacheuser admin
New password:
Re-type new password:
Adding password for user admin
[root@localhost httpd]# cat apacheuser
tom:$apr1$2faTciFK$iHsm6EAb1.SHdkHFL5ur6.
admin:$apr1$AbU8gYqU$KluSOrkvCLjvSj3QwfRIq/
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
##效果:允許/etc/httpd/apacheuser中的用戶admin輸入密碼訪問,不允許/etc/httpd/apacheuser中的其他用戶訪問
119 DocumentRoot "/var/www/html"
120 <Directory "/var/www/html/westos">
121 AuthUserFile /etc/httpd/apacheuser
122 AuthName "Please input user and password !!"
123 AuthType basic
124 Require user admin
125 </Directory>
[root@localhost httpd]# systemctl restart httpd
因為只允許用戶admin登陸,所以用戶tom登陸時,不能成功,會再次回到登陸頁面
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
##效果:允許/etc/httpd/apacheuser中的所有用戶輸入密碼訪問(此處就不再進行瀏覽器訪問測試了,這就留給你吧,^_^)
119 DocumentRoot "/var/www/html"
120 <Directory "/var/www/html/westos">
121 AuthUserFile /etc/httpd/apacheuser
122 AuthName "Please input user and password !!"
123 AuthType basic
124 # Require user admin
##注釋掉
125 Require valid-user
126 </Directory>
[root@localhost httpd]# systemctl restart httpd
三、關于節點設置以及HTTPS加密的設置
1、Apache——一臺主機設置多個節點
[root@foundation50 Desktop]# vim /etc/hosts
172.25.254.127 www.westos.com news.westos.com music.westos.com login.westos.com
[root@foundation50 Desktop]#
[root@localhost httpd]# pwd
/etc/httpd
[root@localhost httpd]# ls
apacheuser conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# ls
autoindex.conf README userdir.conf welcome.conf
[root@localhost conf.d]# vim default.conf
[root@localhost conf.d]# cat default.conf
<VirtualHost _default_:80>DocumentRoot /var/www/htmlCustomLog "logs/default.log" combined
</VirtualHost>
[root@localhost conf.d]# mkdir /var/www/virtual/westos.com/news -p
[root@localhost conf.d]# mkdir /var/www/virtual/westos.com/music -p
[root@localhost conf.d]# vim /var/www/virtual/westos.com/news/index.html
[root@localhost conf.d]# cat /var/www/virtual/westos.com/news/index.html
<h1>
/var/www/virtual/westos.com/news /index's page <h1>
[root@localhost conf.d]# vim /var/www/virtual/westos.com/music/index.html
[root@localhost conf.d]# cat /var/www/virtual/westos.com/music/index.html
<h1>
/var/www/virtual/westos.com/music /index's page <h1>
[root@localhost conf.d]# vim news.conf
[root@localhost conf.d]# cat news.conf
<VirtualHost *:80>ServerName news.westos.comDocumentRoot "/var/www/virtual/westos.com/news/"CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news/">Require all granted
</Directory>
[root@localhost conf.d]# cp news.conf music.conf
[root@localhost conf.d]# vim music.conf
[root@localhost conf.d]# cat music.conf
<VirtualHost *:80>ServerName music.westos.comDocumentRoot "/var/www/virtual/westos.com/music/"CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/music/">Require all granted
</Directory>
[root@localhost conf.d]# systemctl restart httpd
2、HTTPS配置
[root@localhost ~]# yum install mod_ssl -y
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# ls /etc/httpd/conf.d/
autoindex.conf music.conf README userdir.conf
default.conf news.conf ssl.conf welcome.conf
[root@localhost conf.d]# systemctl restart httpd
##訪問https://www.westos.com,下載證書,但是證書不是自己的信息,如下圖
[root@localhost conf.d]# yum install crypto-utils -y
[root@localhost conf.d]# genkey www.westos.com
##操作見下圖
output will be written to /etc/pki/tls/certs/www.westos.com.crt
output key written to /etc/pki/tls/private/www.westos.com.key
##上面這張圖加載時,需要在輸入字符(隨意輸入),不然無法完成加載
[root@localhost conf.d]# vim ssl.conf
101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
108 SSLCertificateFile /etc/pki/tls/private/www.westos.com.key
[root@localhost conf.d]# systemctl restart httpd
##訪問https://www.westos.com,下載證書,現在證書才是自己的信息
##每次訪問HTTPS時,都必須訪問https://域名
3、輸入域名跳轉,自動成為HTTPS
[root@localhost conf.d]# ls
autoindex.conf music.conf README tmprequest welcome.conf
default.conf news.conf ssl.conf userdir.conf
[root@localhost conf.d]# vim login.conf
[root@localhost conf.d]# cat login.conf
<VirtualHost *:443>ServerName login.westos.comDocumentRoot "/var/www/virtual/westos.com/login/"CustomLog "logs/login.log" combinedSSLEngine onSSLCertificateFile /etc/pki/tls/certs/www.westos.com.crtSSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/virtual/westos.com/login/">Require all granted
</Directory>
<VirtualHost *:80>ServerName login.westos.comRewriteEngine onRewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
[root@localhost conf.d]# mkdir -p /var/www/virtual/westos.com/login/
[root@localhost conf.d]# vim /var/www/virtual/westos.com/login/index.html
[root@localhost conf.d]# cat /var/www/virtual/westos.com/login/index.html
<h1>/var/www/virtual/westos.com/login/index's page<h1>
[root@localhost conf.d]# systemctl restart httpd
##輸入配置過的域名時,會自動跳轉成為HTTPS,這里是配置的是www.westos.com,此處就不附測試的圖了,第一次訪問,需要下載證書
四、集成PHP 和 CGI
[root@localhost conf.d]# yum install httpd-manual -y ? ?##安裝此軟件,可以訪問ip/manual——php等的說明
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# ls
index.html tutu.html westos
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
<?phpphpinfo();
?>
[root@localhost html]# yum install php -y
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
169 <IfModule dir_module>
170 DirectoryIndex index.php index.html
171 </IfModule>
[root@localhost html]# systemctl restart httpd
##瀏覽器訪問172.25.254.127
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# mkdir cgi
[root@localhost html]# ls
cgi index.html index.php tutu.html westos
[root@localhost html]# vim cgi/index.cgi
[root@localhost html]# cat cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@localhost html]# chmod +x cgi/index.cgi
[root@localhost html]# ./cgi/index.cgi
Content-type: text/htmlWed May 30 08:14:48 EDT 2018
[root@localhost html]# ./cgi/index.cgi
Content-type: text/htmlWed May 30 08:14:57 EDT 2018
##瀏覽器訪問效果,看見/var/www/html/cgi/index.cgi中的文本內容如下:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@localhost html]# ls
cgi index.html index.php tutu.html westos
[root@localhost html]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# ls
autoindex.conf login.conf music.conf php.conf ssl.conf userdir.conf
default.conf manual.conf news.conf README tmprequest welcome.conf
[root@localhost conf.d]# vim default.conf
[root@localhost conf.d]# cat default.conf | tail -n 5
<Directory "/var/www/html/cgi">Options +ExecCGIAddHandler cgi-script .cgiDirectoryIndex index.cgi
</Directory>
[root@localhost conf.d]# systemctl restart httpd
##瀏覽器訪問效果,效果如下:
Wed May 30 08:19:21 EDT 2018
五、論壇的搭建
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
cgi Discuz_X3.2_SC_UTF8.zip index.html index.php tutu.html westos
##Discuz_X3.2_SC_UTF8.zip——從網上下載,或者從其他處獲取
[root@localhost html]# unzip Discuz_X3.2_SC_UTF8.zip
[root@localhost html]# ls
cgi index.html readme upload westos
Discuz_X3.2_SC_UTF8.zip index.php tutu.html utility
[root@localhost html]# chmod 777 /var/www/html/upload/ -R
[root@localhost html]# php -m
##參數: -m Show compiled in modules
【root@localhost html]# yum install php-mysql.x86_64 -y
[root@localhost html]# systemctl restart httpd
六、代理上網(翻墻)
[root@localhost ~]# yum install squid -y
[root@localhost ~]# vim /etc/squid/squid.conf
56 http_access allow all
62 cache_dir ufs /var/spool/squid 100 16 256
[root@localhost ~]# systemctl start squid
七、squid+apache實現緩存加速
IP為172.25.254.50的設置——Apache(距離較遠)
[root@shenzhen squid]# yum install squid -y
[root@shenzhen squid]# systemctl start squid
[root@shenzhen squid]# cd /usr/share/doc/squid-3.3.8/
[root@shenzhen squid-3.3.8]# ls
ChangeLog COPYRIGHT README rredir.pl url-normalizer.pl
COPYING QUICKSTART rredir.c squid.conf.documented user-agents.pl
[root@shenzhen squid]# systemctl stop firewalld
[root@shenzhen squid]# cd /var/www/html/
[root@shenzhen html]# vim index.html
[root@shenzhen html]# cat index.html
<h1>172.25.254.50<h1>
[root@shenzhen squid-3.3.8]# systemctl start httpd
IP為172.25.254.227的設置——Squid(距離較近)
[root@xian ~]# yum install squid -y
[root@xian ~]# vim /etc/squid/squid.conf56 http_access allow all59 http_port 80 vhost vport60 cache_peer 172.25.254.50 parent 80 0 proxy-only62 cache_dir ufs /var/spool/squid 100 16 256
[root@xian ~]# systemctl start squid
[root@xian ~]# systemctl stop firewalld
總結
以上是生活随笔 為你收集整理的linux——apache 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。