Apache management and web optimization in Linux

The role of Apache

When the web is accessed, http:// is usually used http://
hypertext transfer protocol

http:// Hypertext Transfer Protocol provides software:
Apache
nginx
stgw
jfe
Tengine

Apache installation
dnf install httpd.x86_64 -y
Insert picture description here

Apache is enabled

systemctl enable --now httpd Start the service and set the service bit to start
firewall-cmd --permanent --add-service=http Permanently open http access in the
firewall firewall-cmd --reload Refresh the firewall to make the settings effective
firewall-cmd --list-all View firewall information
Insert picture description here

In firefox: 172.25.254.106
will appear Apache page
Insert picture description here

Basic information of Apache
Service name: httpd
configuration file: /etc/httpd/conf/httpd.conf main configuration file
/etc/httpd/conf.d/*.conf sub configuration file
Default publishing directory: /var/www/html
default Publishing file: index.html
Default port: 80 http
443 (encrypted port) https
user: apache
log: /etc/httpd/logs

The basic configuration of
Apache Apache port modification
vim /etc/httpd/conf/httpd.conf
Listen 1111
Insert picture description here

firewall-cmd --permanent --add-port=1111/tcp
firewall-cmd --reload semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 1111
systemctl restart httpd
Insert picture description here

Insert picture description here

Default publishing file

In 10:
vim /etc/hosts
Insert picture description here

In 20:
vim /var/www//html/index.html
Insert picture description here

At this time, the default file content can be accessed in 10
Insert picture description here

If you want to modify the default release file, you can modify the release file to test.html as follows:
vim test.html
Insert picture description here

vim /etc/httpd/conf/httpd.conf
167 DirectoryIndex test.html westos.html
systemctl restart httpd
Insert picture description here

At this time, visit www.westos.com in 10 to access the new release file
Insert picture description here

The default publishing directory
vim /etc/httpd/conf/httpd.conf
DocumentRoot “/westos/html”
<Directory “/westos/html”>
Require all granted
</Directory>
Insert picture description here

semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’
restorecon -RvvF /westos/
systemctl restart httpd
Insert picture description here

firefox http://www.westos.com
Insert picture description here

Apache access control
experiment material
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
<h1>westosdir's page< /h1>
firefox http://172.25.254.106/westos

Access control
ip whitelist based on client ip
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
Order Deny, Allow
Allow from 172.25.254.6
Deny from All
</Directory>
Insert picture description here

106 cannot be accessed at this time, 6 can be accessed
Insert picture description hereInsert picture description here

ip黑名单
<Directory “/var/www/html/westos”>
Order Allow,Deny
Allow from All
Deny from 172.25.254.6
< /Directory>
Insert picture description here

6 is not accessible at this time, others are accessible
Insert picture description here

User-based authentication
vim /etc/httpd/conf/httpd.conf<Directory “/var/www/html/westos”>
AuthUserfile /etc/httpd/.http_user Specify the authentication file
AuthName “Please input your name and password” Authentication prompt
AuthType basic Authentication type
Require user admin Allowed authenticated users to choose 1 from 2
Require valid-user Allows all users to pass authentication 1 from 2
</Directory>
Insert picture description here

htpasswd -cm .http_user admin Generate authentication file
Insert picture description here

Note: When /etc/httpd/htpasswdfile exists, then do not add the -c parameter when adding users, otherwise the content of the source file will be overwritten

At this time, you need to enter a password to verify the user identity to access westosdir
Insert picture description here

Apache virtual host
mkdir -p /var/www/westos.com/{news,music}
echo "music's page" >/var/www/westos.com/music/index.html
echo "news's page"> /var/ www/westos.com/news/index.html

vim /etc/httpd/conf.d/Vhost.conf
< VirtualHost default:80>
DocumentRoot “/var/www/html”
CustomLog logs/default.log combined
< /VirtualHost>

<VirtualHost *:80>
ServerName wenku.westos.com
DocumentRoot “/var/www/westos.com/wenku”
CustomLog logs/wenku.log combined

< /VirtualHost><VirtualHost *:80>
ServerName news.westos.com
DocumentRoot “/var/www/westos.com/news”
CustomLog logs/news.log combined
< /VirtualHost>
Insert picture description here

Test: vim /etc/hosts 172.25.254.106 www.westos.com music.westos.com news.westos.com
in the host where the browser is located


Insert picture description here

firefox http://www.westos.com
firefox http://music.westos.com
firefox http://news.westos.com
Insert picture description here
Insert picture description here
Insert picture description here

Apache language supports
php
vim /var/www/html/index.php

<?php phpinfo(); ?>

Insert picture description here

dnf install php -y
systemctl restart httpd
firefox http://172.25.254.106/index.php
Insert picture description here

cgi
dnf install httpd-manual
systemctl restart httpd
mkdir /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t ‘/var/www/html/cgi(/.*)?’
restorecon -RvvF /var/www//html/cgi/
vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date;
chmod +x index.cgi

vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/html/cgidir”>
Options +ExecCGI
AddHandler cgi-script .cgi
< /Directory>
Insert picture description here

firefox http://www.westos.com/cgi/index.cgi
Insert picture description here

Encrypted access of Apache
Install encryption plug-in
dnf install mod_ssl -y Generate certificate
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048 Generate private key
Insert picture description here

openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.csr Generate certificate signature file
Insert picture description here

openssl x509 -req -days 365 -in \ /etc/pki/tls/certs/www.westos.com.csr -signkey /etc/pki/tls/private/www.westos.com.key -out /etc/pki /tls/certs/www.westos.com.crt Generate certificate
Insert picture description here

x509 certificate format-
req request-in
load visa name-
signkey

vim /etc/httpd/conf.d/ssl.conf
Insert picture description here

systemctl restart httpd


After downloading the certificate from https://www.westos.com , check the certificate information and find that the certificate has been generated
Insert picture description here

Encrypted virtual host
vim /etc/httpd/conf.d/vhosts.conf
Insert picture description here

systemctl restart httpd
Insert picture description here

Web page rewriting function

vim /etc/httpd/conf.d/vhosts.conf
Insert picture description here

At this time, browsing music.westos.com will be automatically encrypted
Insert picture description here

Squid+Apache
squid forward proxy
Experimental environment:
single network card host node1172.25.254.6 set ip can not access the Internet,
dual network card host node2172.25.254.206 set ip1 to connect to a single network card host, set ip2 to access the Internet

Experimental effect
Let the host with a single network card cannot access the Internet but the browser can access the Internet page

Operation: dnf install squid -y vim /etc/squid/squid.conf http_access allow all cache_dir ufs /var/spool/squid 100 16 256
in the dual-NIC host 206




Insert picture description here

systemctl restart squid
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
Insert picture description here

Select
NetWork Proxy in single network card host 20
Insert picture description here

172.25.254.30 3128
Insert picture description here

Test: ping www.baidu.com
in a single network card host
does not work

Visit www.baidu.com in the browser to
Insert picture description here

Squid reverse proxy
Experimental environment:
172.25.254.6 Apache server
172.25.254.206 Squid, no data is responsible for caching

vim /etc/squid/squid.conf
http_port 80 vhost vport vhost supports virtual domain name vport supports virtual port
When port 80 of 172.25.254.206 is accessed, data will be cached from port 80 of
172.25.254.6 cache_peer 172.25.254.6parent 80 0 proxy-only
systemctl restart squid
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

Insert picture description here

Test:
In 106:
firefox http://172.25.254.206 to
access the data on 172.25.254.6 when you see it
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42958401/article/details/108087653