Ubuntu Kylin/ubuntu20.04 installation and configuration apache2.4

1. Installation


root@hollowman-F117:~# apt install apache2    #安装
root@hollowman-F117:~# apache2 -v             #查看版本
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2020-08-12T19:46:17

Second, the configuration file

1. Introduction to configuration files

The configuration directory after the installation of apache2 is /etc/apache2, and the configuration of the apache2 service is basically carried out in this directory.

root@hollowman-F117:~#  cd /etc/apache2
root@hollowman-F117:/etc/apache2# ls -l
总用量 64
-rw-r--r-- 1 root root  7224 8月  13  2020 apache2.conf
drwxr-xr-x 2 root root   173 3月  19 21:17 conf-available
drwxr-xr-x 2 root root   143 3月  19 21:17 conf-enabled
-rw-r--r-- 1 root root  1782 4月  14  2020 envvars
-rw-r--r-- 1 root root 31063 4月  14  2020 magic
drwxr-xr-x 2 root root  8192 3月  19 21:17 mods-available
drwxr-xr-x 2 root root  4096 3月  19 21:17 mods-enabled
-rw-r--r-- 1 root root   320 4月  14  2020 ports.conf
drwxr-xr-x 2 root root    54 3月  19 21:17 sites-available
drwxr-xr-x 2 root root    30 3月  19 21:17 sites-enabled

apache2.conf global configuration file

ports.conf configuration file for listening IP address and port

Magic configuration for mod_mime_magic, that is, configuration for different file types envvars (environment variables) environment variables

conf-available/
other additional files configuration directory
The configuration files in this directory must end with .conf. The configuration file in this directory
can be enabled or disabled by the a2enconfand a2disconfcommand

conf-enabled/
If a configuration file of onf-available/ is enabled with the a2enconf command, the configuration file will be linked to this directory

mods-available/
This directory contains the available modules that have been installed.
It can be found that some modules have only one .load file, and some have both .load files and .conf files. The .load file contains the instructions needed to load the module, and the .conf file contains the instructions needed to use the module. The modules in this directory
can be enabled or disabled by the a2enmodand a2dismodcommand

mods-enabled/ is
similar to the conf-enabled/ directory, when the modules in the mods-available/ directory are enabled, the enabled module files will also be linked to this directory

sites-available/
This directory is the configuration directory of the virtual host (website), and the configuration file also needs to be enabled and disabled through the a2ensiteand a2dissitecommand.

After the sites-enabled/
virtual host configuration file is enabled, a link file will be created in this directory.

2. Fast way to open and close the module

a2enmodThe command can directly list the modules that have not been enabled, and the modules that need to be enabled can be enabled
by entering the
a2dismodcommands as needed. The commands can directly list the modules that have been enabled, and the modules that need to be disabled can be disabled by entering them according to the situation. For
example:

root@hollowman-F117:/etc/apache2/mods-available# a2dismod
Your choices are: access_compat alias auth_basic authn_core authn_file authz_core authz_host authz_user autoindex deflate dir env filter mime mpm_event negotiation reqtimeout setenvif status
Which module(s) do you want to disable (wildcards ok)?

Three, virtual host configuration

Each virtual host can correspond to a configuration file, and it is stored in a /etc/apache2/sites-availabledirectory.

root@hollowman-F117:/etc/apache2# ls -l sites-available/
总用量 12
-rw-r--r-- 1 root root 1332 4月  14  2020 000-default.conf
-rw-r--r-- 1 root root 6338 4月  14  2020 default-ssl.conf

/etc/apache2/sites-availableThere is 1 virtual host configuration file by default in the directory 000-default.conf, we check the default valid code just a few lines of code:

root@hollowman-F117:/etc/apache2# cat sites-available/000-default.conf | grep -v "#" 
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The configuration content of the code is: the virtual host ip address is this machine, the port is 80, the root directory of the website is /var/www/html, and the storage path of the error log and the access log is specified.

If the server has only one website, you can directly modify this file. If there are multiple websites, you need to create a new virtual host configuration file to achieve it. The following is a configuration analysis based on the virtual host of the hollowman.cn domain name:

1. Create a website directory and web page files

root@hollowman-F117:~#  mkdir /var/www/hollowman
root@hollowman-F117:~#  echo 'hello,world!' > /var/www/hollowman/index.html

2. Create a new virtual host configuration file

root@hollowman-F117:~# vim /etc/apache2/sites-available/hollowman.com.conf

 <VirtualHost *:80>    #定义1个针对ip地址为本机、端口为80的虚拟主机,*表示本机,也可以使用ip地址。注意配置文件中的<>...</>是成对出现的。
	ServerAdmin [email protected]   #定义站长邮箱
	ServerName hollowman.cn    #定义网站域名
	
	DocumentRoot /var/www/hollowman    #定义网站根目录,应当为绝对路径
	
	<Directory /var/www/hollowman>     #配置一些只针对该目录有效的指令,此配置信息在全局配置文件/etc/apache2/apache2.conf中也会出现,具体解析见下文。
	        Options Indexes FollowSymLinks  
	        AllowOverride None 
	        Require all granted	        
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log    #定义错误日志存放位置,${APACHE_LOG_DIR}在envvars配置文件中有设置
	CustomLog ${APACHE_LOG_DIR}/access.log combined        #定义访问日志的路径以及格式
</VirtualHost>

About <Directory ></ Directory> configuration instructions:

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 home page 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

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.

Require: The access control commands for directories in apache2.4 version, the following are common ways of writing

Require all granted    #允许所有来源访问
Require all denied    #拒绝所有来源访问
Require expr expression   #只有表达式为true时允许访问
Require ip 10.1 192.168.10 12.12.0.0/16   #允许指定的IP地址
Require host hollowman.cn   #只允许来自域名hollowman.cn的主机访问 

Note: In the apache2.2 version, the access control to the directory is implemented through the Order command

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. Common writing:

Order deny,allow       #先拒绝后允许
Deny from all          #拒绝所有
Order allow,deny       #先允许后拒绝
Allow from all         #允许所有
Order deny,allow        #先拒绝后允许
Deny from all           #拒绝所有
Allow from hollowman.cn #允许hollowman.cn

3. Turn on the virtual host

 root@hollowman-F117:~# cd /etc/apache2/sites-available/
root@hollowman-F117:/etc/apache2/sites-available# a2ensite hollowman.com
Enabling site hollowman.com
To activate the new configuration, you need to run:
systemctl reload apache2
root@hollowman-F117:/etc/apache2/sites-available#  systemctl restart apache2

At this time, there will be a link file that only wants hollowman.com.conf in the /etc/apache2/sites-enable directory. In fact, the role of a2ensite is to create a link.

4. Open the browser and enter hollowman.com to access, and you will find the web page information: hello, world!

Guess you like

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