MacOS Sierra:apachectl正在使用/usr/local/etc/apache2/2.4中的配置而不是/ etc / apache2 /

时间:2017-09-07 12:39:22

标签: macos apache apache2 macos-sierra apache2.4

我在/etc/apache2/中定义了所有配置文件和虚拟主机。

然而,当我运行apachectl -V时,我得到以下输出:

Server version: Apache/2.4.26 (Unix)
Server built:   Jul  8 2017 19:15:17
Server's Module Magic Number: 20120211:68
Server loaded:  APR 1.6.2, APR-UTIL 1.6.0
Compiled using: APR 1.6.2, APR-UTIL 1.6.0
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_SYSVSEM_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/local/Cellar/httpd24/2.4.26"
 -D SUEXEC_BIN="/usr/local/Cellar/httpd24/2.4.26/bin/suexec"
 -D DEFAULT_PIDLOG="/usr/local/var/run/apache2/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="/usr/local/etc/apache2/2.4/mime.types"
 -D SERVER_CONFIG_FILE="/usr/local/etc/apache2/2.4/httpd.conf"

这显然是使用其他一些我甚至不知道的apache2配置文件。如何强制apache2使用正确的配置文件?

1 个答案:

答案 0 :(得分:6)

You likely had an apache installation already on your computer, then installed another version. In this case, you likely have multiple apachectl binaries. Different apachectl binaries will be compiled to be used with different config files.

do:

find /usr -name apachectl

This will find everything in the /usr directory with the name apachectl. My output is:

/usr/local/bin/apachectl
/usr/local/Cellar/httpd/2.4.27/bin/apachectl
/usr/sbin/apachectl

Now look at the information for each executable, using -V, until you find the version compiled with the configuration file you desire:

    /usr/sbin/apachectl -V

out:

    ...
    ...
    ...
    -D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"

Now we know which one you want to have precedence. So this is how you set precedence.

Even though there are multiple binaries with the same name, when you type in "apachectl" on the terminal, there is no "fight" to determine which one is executed, because precedence is determined by:

  1. the /etc/paths file
  2. your bash profile

You have a few options:

A) (Not recommended) You could change your /etc/paths file to reflect that you'd like one version of apachectl to take precedence over the other

The paths file is for general precedence of entire file hierarchies. You would tell it, "In general, when there are multiple binaries with the same name, execute the one in this directory hierarchy first". Consequently, the file is very simple and just an ordering of directories. Mine looks like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

I wouldn't change the paths file because that is sort of like getting rid of a hangnail by ripping off the entire nail.

B) You could change precedence (for a single terminal session) by typing this into your terminal:

export PATH="/usr/sbin/apachectl:$PATH"

This will export a new PATH variable with /usr/sbin/apachectl coming before everything else already in the $PATH. This will prepend the path for your executable to the beginning of the path, making it have precedence over every other possible executable. So when your system gets the call for the apachectl binary from the terminal, it checks the path and stops checking when it comes across a direct path to your executable as the first thing in the Path. This won't stick beyond a single session. So if you close your terminal, the PATH will revert back to what it was.

C) You could make this permanent by adding the previous export command to your ~/.bashrc file (or ~/.bash_profile on a Mac):

export PATH="/usr/sbin/apachectl:$PATH"

Having this in your .bashrc file will cause this statement to be executed everytime you begin a new session, because that is when your profile is read.

If there is another line in your profile that deliberately gives the other version of apachectl precedence, you could either delete it, or just make sure your export... line comes after it in the file.

D) You could make an alias, to refer directly to this executable.

An alias is a word you can use to refer to an executable located in a particular location. Each time you type in your alias, you are referring to that exact executable at that exact location. I recommend doing this above all others:

alias apachectl_a1='/usr/sbin/apachectl'

This also needs to be put in your ~/.bashrc file (~/.bash_profile on Mac) to last beyond a single session.