apache common skills

About apache

apache is a server that can be used to configure a static page server

apache configuration path and view apache version and more detailed information

apachectl -V

The mac side prints as follows:

Server version: Apache/2.4.46 (Unix)
Server built:   Oct 29 2020 20:35:15
Server's Module Magic Number: 20120211:93
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_FLOCK_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/usr"
 -D SUEXEC_BIN="/usr/bin/suexec"
 -D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
 -D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"

As you can see, the apache configuration file address is in /private/etc/apache2/httpd.conf

apache common commands

start apache

sudo apachectl start

According to the default configuration, you can visit http://localhost/, and you can see that the service is up.

restart apache

apachectl restart

close apache

apachectl stop

Check apache configuration for errors

apache configtest

If there is an error, the following example will be prompted

AH00526: Syntax error on line 559 of /private/etc/apache2/httpd.conf:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Configuration interpretation

<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 80
</IfDefine>

apache default service port: 80

#LoadModule proxy_module libexec/apache2/mod_proxy.so

Many services are not loaded by default, such as this proxy forwarding service. If you want to use the corresponding function, you need to open the comment here

<VirtualHost 80>
	ServerName youname
	ProxyPass /hello http://localhost:8080/
  ProxyPassReverse /hello http://localhost:8080/
</VirtualHost>

The meaning of the above is to forward all /hello requests on port 80 to the http://localhost:8080/ address

Change the configuration of apache

vi /private/etc/apache2/httpd.conf

Sometimes there will be a problem of not being able to save, prompting read-only, this is a permission problem, the default permissions are as follows: terminal input ls -al /private/etc/apache2/httpd.conf

-rw-r--r--  1 root  wheel  21641 Jan  1  2020 /private/etc/apache2/httpd.conf

As you can see, it is read-only. This is the read only permission we need to change the file with admin rights

sudo chmod 744 /private/etc/apache2/httpd.conf

Check permissions again

-rwxr--r--  1 root  wheel  21733 Dec  3 07:18 /private/etc/apache2/httpd.conf

Then, go to vi to edit and save

View apache access log

It can be found that the default log logs are placed in /private/var/log/apache2/access_log

At this time, execute

cat /private/var/log/apache2/access_log

You can see the command

View apache error log

First, go to find the apache log storage address, go to httpd.conf to find

ErrorLog "/private/var/log/apache2/error_log"

It can be found that the default log logs are placed in /private/var/log/apache2/error_log

At this time, execute

cat /private/var/log/apache2/error_log

You can see the command

Debug apache (adjust the apache log log level to see more debugging information)

LogLevel alert rewrite:trace3

Domain name forwarding (interface forwarding)

Edit the apache configuration file

Requirements: localhost:8080 vuepress has been run locally, and now it is required to access localhost/blog/ to access the same content.
Idea: localhost/blog/ interface forwarded to localhost:8080

way 1

<Location "/">
    ProxyPass "http://localhost:8080/"
</Location>

way 2

<VirtualHost *:80>
    ServerName blog
    RewriteEngine  on
    RewriteRule "^/blog/(.*)" http://localhost:8080/blog/$1 [P]
</VirtualHost>

Note that the base url of many front-end constructions is /. If we add the relative path of the blog here, we often need to add the relative path to the configuration of the front-end page construction. For example, the configuration of vuepress is as follows:

// docs/.vuepress/config.js
base: '/blog/',

apache common errors

Invalid command 'RewriteEngine'

AH00526: Syntax error on line 559 of /private/etc/apache2/httpd.conf:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

The reason may be because you did not open the mod_rewrite module, go to the configuration file and remove the introduction comment of the open module.

More great articles to read my blog , if there are any mistakes, please correct me and make progress together

Tags: Apache server

Posted by pourmeanother on Wed, 04 May 2022 15:14:27 +0300