Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Ubuntu16.04 and Apache Virtual Host configuration

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of Ubuntu16.04 and Apache virtual host configuration. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Preface

It is well known that under window, the configuration file of Apache is httpd.conf, but after installing Apache under Linux, it is found that its configuration is not as simple as that under window. Apache under Linux divides each setting item into different configuration files. There is also a good reason to think about it. This allows users to freely match their different functional modules in the development process and improve efficiency. However, if you do not know Apache in advance It is possible to circle during configuration.

The configuration file for Apache in Ubuntu is / / etc/apache2/apache2.conf. Apache finds this file at startup and automatically reads the configuration information in it, while other configuration files are included through the include directive, and you can see these incoming lines in apache2.conf. Of course, you can also put all the configuration in apache2.conf or httpd.conf or other files, Apache this division is a better habit.

In Ubuntu, the root directory of the Web document is in / var/www. How do you know that? There is a paragraph like this in / etc/apache2/sites-enabled/000-default

NameVirtualHost * ServerAdmin webmaster@localhostDocumentRoot / var/www/

This is to set up the virtual machine, of course, if you think it is useless, you can also comment out the Include / etc/apache2/sites-enabled/ line in apache2.conf and set the DocumentRoot in htttpd.conf to the directory of a project, which is convenient for development.

Under the / etc/apache2 directory, you find the sites-enabled directory, but there is also a sites-available directory, so what exactly are these two directories for? In fact, the sites-available directory contains the configuration files for Apache virtual hosts. Virtual hosts allow Apache to configure multiple sites and configure different parameters for each site. The function of the sites-enabled directory is to hold links to the files in the / etc/apache2/sites-available directory. When Apache restarts, the sites contained in this directory will be activated. If multiple virtual machines are configured on the apache, and the configuration files of each virtual machine are placed under the sites-available, then it is very convenient to start when the virtual host is disabled, and there is no need to move the configuration file to operate a virtual host.

-I am the beautiful dividing line-

Mods-available and mods-enabled directories similar to sites-enabled and sites-available are also used under / etc/apache2, so what is the use of these two directories? In fact, directories similar to sites-enabled and sites-available,mods-available contain modules and module configuration files, not all modules have configuration files. For example, when apt-get install php5 installs the php module, there are php5.load, php5.conf, and links to these two files in these two directories. This is very convenient for apache to enable and disable a module.

In the / etc/apache2 directory, there is also a file ports.conf, which configures the port on which Apache listens. If you find it unnecessary, you can first remove the Include / etc/apache2/ports.conf line from apache2.conf and set the Apache port in httpd.conf.

The directory installed by default in Ubuntu is a little different from the others. In ubuntu, the configuration of module and virtual host has two directories, one is available, and the other is enabled,available directory, which stores valid content, but does not work. It can only work if you connect to enabled with ln. This is convenient for development and debugging.

-I am the beautiful dividing line-

What is a virtual host (Virtual Hosting)

The so-called virtual host is to divide a server running on the Internet into multiple "virtual" servers, each of which has an independent domain name and a complete Internet server (supporting WWW, FTP, E-mail, etc.). To put it simply, the same server can process more than one domain name (domain) at the same time. Assuming that the www.example1.com and www.example2.com domain names both point to the same server, and the web server supports virtual hosts, www.example1.com and www.example2.com can access different web spaces on the same server.

In Apache2, valid site information is stored in the / etc/apache2/sites-available/ user name (file). We can add the following information to add a valid virtual space, just copy most of the things in / etc/apache2/sites-available/000-default.conf (the older version of ubuntu may be called default), name it the name you want, remember to change DocumentRoot as the default directory, set the path in Directory, and make sure that the port number is not duplicated with other virtual hosts:

# add your website name ServerName www.linyupark.com# after ServerName. If you want multiple website names to get the same website, you can add other website aliases after ServerAlias. # aliases are separated by spaces. ServerAlias ftp.linyupark.com mail.linyupark.com# adds the email address of the webmaster after the ServerAdmin, so that if you have any questions, you can contact the webmaster. ServerAdmin webmaster@linyupark.com# adds the directory path where the website content is stored after DocumentRoot (user's personal directory) DocumentRoot / home/linyupark/public_htmlOptions Indexes FollowSymLinks MultiViewsAllowOverride NoneOrder allow,denyallow from allScriptAlias / cgi-bin/ / usr/lib/cgi-bin/AllowOverride NoneOptions ExecCGI-MultiViews + SymLinksIfOwnerMatchAllow from allErrorLog / home/linyupark/public_html/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog / home/linyupark/public_html/access.log combinedServerSignature On

If your server has more than one IP, and different IP have different virtual users, you can change it to:

...

In general, we only need to do two operations: 1, add "ServerName www.example.com", 2, modify the directory defined by "DocumentRoot / var/www".

Enable configuration

1. Now the content of our configuration is only a valid virtual host. If it really works under the / etc/apache2/sites-enabled folder, you need to establish an association through ln:

Sudo ln-s / etc/apache2/sites-available/example.conf / etc/apache2/sites-enabled/example.conf

2. Modify the / etc/hosts file to add the IP address of the current host and the virtual host name to be set, such as 127.0.0.1 www.jiaoxue.com:

127.0.0.1 localhost127.0.1.1 shanlei-Lenovo-ideapad-110-15ISK127.0.0.1 www.jiaoxue.com # The following lines are desirable for IPv6 capable hosts::1 ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters~

Check the syntax for no errors, and if there are no errors, restart Apache:

This is the end of the sudo / etc/init.d/apache2 restart// or service apache2 restart article on "sample Analysis of Ubuntu16.04 and Apache Virtual Host configuration". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report