Detailed steps to configure Ubuntu 16.04 and Apache virtual host

Foreword

As we all know, under the window, the Apache configuration file is httpd.conf, but after installing Apache under Linux, it is found that the configuration is not as simple as under the window. Under Linux, Apache divides each setting item into different configuration files. There is also a big reason to think that this allows users to freely match different functional modules during the development process and improve efficiency. However, if you do not know Apache in advance, you may be trapped during configuration.

The configuration file of Apache in Ubuntu is //etc/apache2/apache2.conf. Apache will find this file and automatically read the configuration information in the file when it starts up, while other configuration files are included through the include directive. You can see these introduction lines in apache2.conf. Of course, you can also put all the configuration in apache2.conf or httpd.conf or other files. This is a good habit for Apache.

In Ubuntu, the root directory of web documents is in /var/www, how do you know? There is such a content in /etc/apache2/sites-enabled/000-default

NameVirtualHost *<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/

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

In the /etc/apache2 directory, I found the sites-enabled directory, but there is also a sites-available directory, so what are the functions of these two directories? In fact, the sites-available directory contains configuration files for Apache virtual hosts. Virtual hosts allow Apache to configure multiple sites and configure different parameters for each site. The role of the sites-enabled directory is to hold links to 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 apache, and the configuration file of each virtual machine is placed under sites-available, then it is very convenient to start the virtual host for deactivation, and it is not necessary to move the configuration file to operate a virtual host

——- I am a beautiful dividing line——-

Two directories, mods-available and mods-enabled, similar to the sites-enabled and sites-available directories are also used under /etc/apache2. So what are the functions of these two directories? In fact, similar to sites-enabled and sites-available, the directory mods-available contains modules and module configuration files. Not all modules have configuration files. For example, when apt-get install php5 installs the php module, there will be 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 certain module

In the /etc/apache2 directory, there is also a file ports.conf. This file configures the ports that Apache listens to. If you think it is unnecessary to discard it, you can remove the Include /etc/apache2/ports.conf line in apache2.conf first. Set the Apache port in httpd.conf.

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

——- I am a beautiful dividing line——-

What is Virtual Hosting

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

In Apache2, valid site information is stored in /etc/apache2/sites-available/username (file). We can add information in the following format to add an effective virtual space, and just copy most of the contents in /etc/apache2/sites-available/000-default.conf (older ubuntu versions may be called default), Name it the name you want, remember to change DocumentRoot as the default directory, set the path in the Directory, and pay attention to the port number not to repeat with other virtual hosts:

< VirtualHost *Custom port>
# Add your website name after ServerName
ServerName www.linyupark.com
# If you want multiple website names to get the same website, you can add other website aliases after ServerAlias.
# The aliases are separated by spaces.
ServerAlias ftp.linyupark.com mail.linyupark.com
# Add the email address of the website administrator after ServerAdmin, so that others can contact the website administrator if they have questions.
ServerAdmin [email protected]
# Add the directory path for storing website content after DocumentRoot(User&#39;s personal directory)
DocumentRoot /home/linyupark/public_html
< Directory /home/linyupark/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
< /Directory>
ScriptAlias /cgi-bin//usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
< /Directory>
ErrorLog /home/linyupark/public_html/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /home/linyupark/public_html/access.log combined
ServerSignature On
< /VirtualHost>

If your server has multiple IPs and different IPs have different virtual users, you can modify it to:

< VirtualHost IP address[:port]>...</VirtualHost>

Under normal circumstances, we only need to do two operations: 1. Add "ServerName www.example.com", 2. Modify "DocumentRoot /var/www" as a self-defined directory.

Enable configuration

1 Now, what we have configured is only a valid virtual host. If it is really put into play under the /etc/apache2/sites-enabled folder, it needs to be associated with ln:

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

2 , Modify the /etc/hosts file, 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 localhost
127.0.1.1 shanlei-Lenovo-ideapad-110-15ISK
127.0.0.1 www.jiaoxue.com
 
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
~

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

sudo /etc/init.d/apache2 restart
//or
service apache2 restart

to sum up

The above is the entire content of this article. I hope that the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message and exchange. Thank you for your support to ZaLou.Cn.

Recommended Posts

Detailed steps to configure Ubuntu 16.04 and Apache virtual host
How to set up an Apache virtual host on Ubuntu 16.04
Detailed steps to install and configure k8s cluster in centos 7
Detailed steps to install virtual machines with VMware 15 and use CentOS 8
Ubuntu configure Apache
How to install and configure Gogs on Ubuntu 18.04
How to install and configure Cyberpanel on Ubuntu 18.04
Detailed steps to install Anaconda on Linux (Ubuntu 18.04)
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure GitLab on Ubuntu 18.04
Detailed steps to install Anaconda on Linux (Ubuntu 18.04)
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and configure VNC on Ubuntu 18.04
How to install and configure Sphinx on Ubuntu 16.04
How to install and configure OrientDB on Ubuntu 14.04
How to configure Apache content caching on Ubuntu 14.04
How to install and configure AppScale on Ubuntu 12.04
How to install and configure PostGIS on Ubuntu 14.04
Embedded Linux development environment to build and configure Ubuntu
Detailed steps to install Sogou input method on Ubuntu 20.04
How to set up an Apache virtual host on CentOS 7
Detailed steps to automatically set the line number in ubuntu16.04
ubuntu 12.04 configure the kernel crash to automatically restart and dump
How to upgrade CentOS7 to CentOS8 (detailed steps)
How to install Apache on Ubuntu 20.04
Install and configure MySQL on Ubuntu
Install and configure Docker in Ubuntu
ubuntu modify and configure ip address
Detailed steps to install MySQL to change the default password in Ubuntu
CentOS7.2 and Nginx configuration virtual host
How to install Apache on Ubuntu 20.04
Ubuntu change password and username steps
Steps to upgrade Laravel 5.4 to 5.6 in Ubuntu 16.04
ubuntu16.04 method steps to upgrade the kernel
Steps to build LNMP environment on ubuntu 20.04
How to configure Redis cluster on Ubuntu 14.04
Ubuntu20.04 steps to install cuda10.1 (graphic tutorial)
How to install Apache Kafka on Ubuntu 18.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to configure Redis replication on Ubuntu 16.04
How to install Ubuntu20.04 and install NVIDIA driver
Configure node js and npm under Ubuntu 14.04
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
Install JDK and configure environment variables on Ubuntu 16.04
How to install and configure Elasticsearch on CentOS 7
How to modify time zone and time in ubuntu
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and use Docker on Ubuntu 20.04
How to install and configure VNC on CentOS 8
Virtual machine installation and configuration ubuntu shared folder_
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Redis on CentOS 8
Use Ubuntu and windows system in virtual machine
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
Steps to add toolkit for Matlab_Linux under ubuntu