Start Apache
Open terminal and enter sudo apachectl-V (you may need to enter machine secrets ). The Apache version is shown as follows:
Enter sudo apachectl start to start Apache. Open the address bar of safari and enter "http: // localhost". The content is "It works !" . It is located under "/Library (resource library)/webserver/documents/", which is the default root directory of Apache.
The installation directory of Apache is:/etc/apache2/, and etc is hidden by default. There are three ways to view:
- Right-click finder under the dock, select "go to folder", and enter "/etc"
- Go to --- Under finder and enter/etc.
- You can enter "Open/etc" in terminal"
Set VM
- Run"
sudo vi /etc/apache2/httpd.conf
Open the Apche configuration file.
- In httpd. conf, find"
#Include /private/etc/apache2/extra/httpd-vhosts.conf
", Remove the previous"#
", Save and exit.
- Run"
sudo apachectl restart
", Restart Apache and then enable the VM configuration function.
- Run"
sudo vi /etc/apache2/extra/httpd-vhosts.conf
", Opened the configure virtual host file httpd-vhost.conf, configure the virtual host. Note that two virtual hosts are enabled for this file by default:<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/usr/docs/dummy-host.example.com" ServerName dummy-host.example.com ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common</VirtualHost><VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/usr/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common</VirtualHost>
In fact, these two virtual hosts do not exist. when no other virtual hosts are configured, the following prompt may appear when accessing localhost:
ForbiddenYou don‘t have permission to access /index.php on this server
The simplest way is to add # in front of each line and comment it out, so that it can be referenced without causing other problems.
- Add the following configuration
<VirtualHost *:80> DocumentRoot "/Library/WebServer/Documents" ServerName localhost ErrorLog "/private/var/log/apache2/localhost-error_log" CustomLog "/private/var/log/apache2/localhost-access_log" common</VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/snandy/work" ServerName mysites ErrorLog "/private/var/log/apache2/sites-error_log" CustomLog "/private/var/log/apache2/sites-access_log" common <Directory /> Options Indexes FollowSymLinks MultiViews AllowOverride None Order deny,allow Allow from all </Directory></VirtualHost>
Save and exit, and restart Apache.
- Run"
sudo vi /etc/hosts
", Open the hosts configuration file, and add"127.0.0.1 mysites
", In this way, you can configure the sites virtual host and access" http: // mysites ". The content of Mac OS X and" http: // localhost /~ [User name] "is completely consistent.
- Note:
ErrorLog "/private/var/log/apache2/sites-error_log"
It can also be deleted, but it is a good habit to record logs. It can help us determine when a problem occurs. If the Log Code is retained, the path of the log file must exist. If you modify the Log Code that does not exist, Apache cannot be served and no error prompt is displayed. This is quite disgusting.