Process record-VMWare.CentOS7+ApacheWeb service-installation test

The following content is about virtual host based on domain name, port, and virtual IP.

Commonly used commands:
temporarily disable firewall and SELinux functions in the test environment
#systemctl stop firewalld
#setenforce 0
Check whether the web server software is installed
Check the installation information
#yum info httpd
#yum info lynx (this is a text browsing tool)
Service installation command:
#yum install httpd -y
Start Service
#systemctl start httpd
Service restart command:
#systemctl restart httpd
View status
#systemctl status httpd
View error information
#journalctl -xe

Apache default web site file directory
/var/www/html/

Test the server : (the double quotes below need to use English characters)
#echo “hello”>/var/www/html/index.html
#yum install lynx (if you have it, you don’t need to install it)
#lynx 127.0.0.1
see "Hello" is displayed on the web page to indicate normal operation.

Default website basic settings

The main configuration file of the apache web server is "/etc/httpd/conf/httpd.conf". This file contains three parts:
global environment settings, main server configuration (also provides default settings for virtual hosts), virtual host settings

Back up the original file of the main configuration file:
#cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak

Description of some parameters of the main configuration file: (parameter values ​​are case-sensitive, and parameter names are not case-sensitive)
DocumentRoot-defines the home directory of the site, the default value is /var/www/html/
DirectoryIndex-sets the default document of the site, which can be followed by multiple File name .html or .htm
Listen-Set the IP address and port number that apache listens to. If you don’t set a virtual host based on the IP address, the IP address is often omitted. Just listen to 80. The default port number is 80. You can add a new listening port. Write directly in the next line, there can be multiple listening ports at the same time.
AddDefaultCharset-used to set the default character set, such as AddDefaultCharset GB2312 (or UTF-8)
Directory-used to define the directory access restrictions, such as:
<Directory "site file directory">
AllowOverride none #Do not allow .htaccess in this directory to change the configuration here, which can improve the efficiency of the website
Require all granted #Allow all users to access this directory
</Directory>

Create the configuration file of the virtual host separately Create a file
separately in the directory you specify to configure the virtual host:
#mkdir /etc/httpd/vhost-conf.d/
#echo "include vhost-conf.d/*.conf>> / The
above command in etc/httpd/conf/httpd.conf appends the include content in quotation marks to the end of the main configuration file, which means to include the file whose name in the vhost-conf.d directory ends with .conf to the main configuration file Note here, use >> to indicate append, don't use> to overwrite.

Directory storage:
Insert picture description here

The configuration file test1.conf of the following virtual host is placed in the "/etc/httpd/vhost-conf.d/" directory. Multiple .conf configuration files can exist in this directory at the same time. If the configuration content does not conflict, they will all take effect, and the website can be accessed at the same time.
The website site files ending in .html should be placed in the directory specified in the configuration file. Here, the test chooses to create new directories for different sites under the default site directory, and use the virtual host name of the site as the directory name. The site files are stored in the directory.
For example: (Website directories mk, hr, main on the virtual host should be created by yourself)
/var/www/html/ #html files in this directory constitute the home page of the default site
/var/www/html/mk/ #in this directory html files constitute the homepage of the marketing department website
/var/www/html/hr/ #html files in this directory constitute the homepage of the personnel department website
/var/www/html/main/ #html files in this directory constitute the homepage of the company portal

Directory creation: #mkdir /var/www/html/mk

Different websites can be configured with virtual hosts to implement server construction, or you have multiple network cards, which are bound to real network cards respectively. The following content only involves virtual host configuration.

1) Configure virtual host based on domain name

When it comes to domain names, DNS services are required for resolution. For DNS content, you can read my other blog.
Or directly modify the host file. The host file of Linux is "/etc/hosts", and the file address may be different for different versions of windows, which is "c:\windows\system32\drives\etc\hosts".
Add the record corresponding to the IP and domain name in the host file:
For example under Linux:
#vi /etc/hosts
10.1.1.200 www.xxx.com
Edit the configuration file of the virtual host (in "/etc/httpd/vhost-conf.d /” directory):
#vi /etc/httpd/vhost-conf.d/test1.conf
content such as:

<VirtualHost *:80>
 DocumentRoot /var/www/html/hr
 ServerName hr.test1.com
<Directory /var/www/html/hr>
 Require ip 10.1.1.0/24
</Directory>
</VirtualHost>

<VirtualHost *:80>
 DocumentRoot /var/www/html/mk
 ServerName mk.test1.com
<Directory /var/www/html/mk>
 Require ip 10.1.1.0/24
</Directory>
</VirtualHost>

<VirtualHost *:80>
 DocumentRoot /var/www/html/main
 ServerName www.test1.com
<Directory /var/www/html/main>
 Require all granted
</Directory>
</VirtualHost>

Write the html file in the site directory: (The main directory has been created)
#echo “welcome to www.test1.com!”> /var/www/html/main/index.html After the
configuration is complete, restart the service. If no error is reported, Perform local testing and client testing.
If there is an error, you can check the configuration file for errors, network connectivity, whether the firewall is closed, and the file name and path are correct and consistent according to the error message.

Test:
Use lynx to test under the command line on Linux:
#lynx www.test1.com
or directly enter the domain name on the browser to access.

2) Configure port-based virtual host

Add a new port listener in the Listen section of the main configuration file:
#vi /etc/httpd/conf/httpd.conf
Listen 10.1.1.200:8080
You can also omit the IP and listen directly to 8080, the effect will be different.
The configuration file of the virtual host (under the "/etc/httpd/vhost-conf.d/" directory):
#vi /etc/httpd/vhost-conf.d/test2.conf The
content is for example:

<VirtualHost 10.1.1.200:8081>
 ServerName www.test1.com
 DocumentRoot /var/www/html/main
<Directory /var/www/html/main>
 Require all granted
</Directory>
</VirtualHost>

... 其它基于端口的虚拟主机配置 ...

Restart the service and test.

Modify the homepage file:
Add the following fields in the tag </VirtualHost>:

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

3) Configure virtual host based on virtual IP

Add multiple virtual IP addresses for the virtual host. The virtual addresses cannot conflict with existing addresses. Generally, addresses 1-9 reserved for the server during network planning are used. The added addresses are invalid after restarting.
#ifconfig ens33:1 10.1.1.2 up
#ifconfig ens33:2 10.1.1.3 up
#ifconfig ens33:2 10.1.1.4 up

After adding, use ifconfig to view the network card information and you will see the newly added virtual network card IP information.

Edit the virtual host configuration file:
#vi /etc/httpd/vhost-conf.d/test3.conf,
for example:

<virtualhost 10.1.2.1:80>
documentroot /var/www/html/hr
<directory /var/www/html/hr>
require ip 10.1.2.0/24
</directory>
</virtualhost>

Restart the service and test.

Modify the homepage, for the default site: <IfModule dir_module> DirectoryIndex info.php index.html </IfModule> in the
main configuration file /etc/httpd/conf/httpd.conf
, the first file after DirectoryIndex is the homepage, which is written like this It uses info.php as the homepage.



Remarks:
Modify the httpd.conf default document, the first in the order is the home page :
#vim /etc/httpd/conf/httpd.conf
Modification part: (in the figure, set info.php as the home page)
Insert picture description here

Other references:
1
Detailed explanation of Apache under Linux
2
Introduction and construction of Linux Apache service
3
Detailed installation steps of Apache web server installation under Linux

Guess you like

Origin blog.csdn.net/qq_43750882/article/details/109617075