Redhat installation and configuration of Apache service

1. Commonly used installation and management commands

yum install  httpd   		#安装Apache,也就是httpd服务
systemctl start httpd  		#启动httpd服务
systemctl enable httpd  	#设置开机自动启动httpd服务
systemctl restart httpd  	#重启httpd服务

Two, configuration file description

/etc/httpd/conf/httpd.conf 		#主配置文件
/etc/httpd/conf.d    			#子配置文件目录
/var/log/httpd/     			#日志文件目录(access.log error.log)
/etc/httpd/modules     		#模块文件的目录
/var/www/html       			#默认网站根目录

Main configuration file ( /etc/httpd/conf/httpd.conf ) information:
The configuration information contained by <></> is all regional configuration information, and the others are global configuration information.

ServerRoot "/etc/httpd"   			#服务主目录
Listen 80    						#监听IP地址及端口号
Include conf.modules.d/*.conf    
User apache   	 				#运行httpd服务的用户
Group apache   					#运行httpd服务的用户组
ServerAdmin root@localhost    		#管理员邮箱
ServerName www.example.com:80 	#服务器域名,默认为注释

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"   #网站数据根目录

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"   #错误日志文件
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
CustomLog "logs/access_log" combined     #访问日志文件
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on
IncludeOptional conf.d/*.conf

About website directory configuration information (take the root directory as an example):

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

AllowOverride: Allows the type of instructions that exist in the .htaccess file (the name of the .htaccess file can be changed, and the file name is determined by the AccessFileName instruction). When the value is None, it means that the .htaccess file in the directory will not be searched; when the value is All, it means that all commands can be used in the .htaccess file.

Options: Configure which features are used in a specific directory. Multiple features are separated by spaces. ExecCGI means that CGI scripts are allowed to be executed in this directory; FollowSymLinks means that symbolic links are allowed in the file system in this directory; Indexes means that when the user accesses the directory, if the user cannot find the homepage file specified by DirectoryIndex (for example, index.html), Then return the list of files in the directory to the user; SymLinksIfOwnerMatch means that when symbolic links are used, only when the owner of the symbolic link is the same as the owner of the actual file can they be accessed.

Order: Control which of the two access rules of Allow and Deny takes precedence during access: Allow represents the list of hosts that are allowed to access (available domain names or subnets, for example: Allow from 192.168.0.0/16); Deny represents the list of hosts that are denied access.

DirectoryIndex index.html index.htm index.php   #设置默认的主页文件名

Three, virtual host configuration

1. Domain-based virtual hosting

1) Configure domain name information

[root@hollowman ~]# echo '192.168.100.100 bbs.hollowman.cn'  >>  /etc/hosts

2) Set up the site directory and test webpage corresponding to the domain name

[root@hollowman ~]# mkdir /var/www/bbs
[root@hollowman ~]# echo bbs.hollowman.cn > /var/www/bbs/index.html

3) Configure virtual host information. You
can add virtual host configuration information anywhere in the main configuration file (/etc/httpd/conf/httpd.conf). It is generally placed behind the website data root directory and in front of the latter for a clearer reading of the configuration file.

[root@hollowman ~]# vim /etc/httpd/conf/httpd.conf
......
DocumentRoot "/var/www/html"   #网站数据根目录
<VirtualHost 192.168.100.100>
    DocumentRoot "/var/www/bbs"
    ServerName "bbs.hollowman.cn"
    <Directory "/var/www/bbs">
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
......

4) Restart the httpd service

[root@hollowman ~]# systemctl restart httpd
[root@hollowman ~]# systemctl enable httpd

5) Enter the domain name bbs.hollowman.cn in the browser to visit
Insert picture description here

2. Port-based virtual hosting

1) Set the site directory and test webpage corresponding to the port

[root@hollowman ~]# mkdir /var/www/8080
[root@hollowman ~]# echo ‘192.168.100.100:8080’ > /var/www/8080/index.html

2) Configure port-based virtual host information.
Note that you need to configure listening port information, which can be put together with the domain-based virtual host information configured last time.

[root@hollowman ~]# vim /etc/httpd/conf/httpd.conf
......
DocumentRoot "/var/www/html"   #网站数据根目录
...基于域名的虚拟主机配置信息...
Listen 8080
<VirtualHost 192.168.100.100:8080>
    DocumentRoot "/var/www/8080"
    <Directory "/var/www/8080">
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
......

3) Restart httpd service

[root@hollowman ~]# systemctl restart httpd

4) Enter the ip address with port: 192.168.100.100:8080
Insert picture description here

Fourth, modify the website root directory

Because modifying the root directory of the website involves the SELinux security policy, you need to modify the security context. You can view the content of the "SElinux Study Notes".

Guess you like

Origin blog.csdn.net/ymz641/article/details/112001269