【linux】debian安装apache2并创建虚拟站点

前言

教程将会讲解如何在debian系统上安装apache2并且在80端口部署多个网站

环境准备

1.本次使用的服务器为debian10
2.睿智头脑和一双手

教程步骤

1.更新apt
这里我就不放设置更新源头的了,网上一搜一大把

#获取apt软件更新
apt-get update

执行效果

root@vps:~# apt-get update
Hit:1 http://deb.debian.org/debian buster InRelease
Hit:2 http://deb.debian.org/debian buster-updates InRelease
Hit:3 http://security.debian.org/debian-security buster/updates InRelease
Reading package lists... Done
root@vultr:~#

2.安装apache2

apt-get install apache2

执行效果

root@vps:~# apt-get install apache2                       Reading package lists... Done
Building dependency tree
Reading state information... Done                           The following additional packages will be installed:   apache2-bin apache2-data apache2-utils libapr1              libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap   libbrotli1 libjansson4 liblua5.2-0 ssl-cert
Suggested packages:   apache2-doc apache2-suexec-pristine                         | apache2-suexec-custom www-browser openssl-blacklist
The following NEW packages will be installed:   apache2 apache2-bin apache2-data apache2-utils libapr1   libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap   libbrotli1 libjansson4 liblua5.2-0 ssl-cert
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 2627 kB of archives.
After this operation, 8950 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian buster/main amd64 libapr1 amd64 1.6.5-1+b1 [102 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 libaprutil1 amd64 1.6.1-4 [91.8 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 libaprutil1-dbd-sqlite3 amd64 1.6.1-4 [18.7 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 libaprutil1-ldap amd64 1.6.1-4 [16.8 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 libbrotli1 amd64 1.0.7-2 [270 kB]
Get:6 http://deb.debian.org/debian buster/main amd64 libjansson4 amd64 2.12-1 [38.0 kB]
Get:7 http://deb.debian.org/debian buster/main amd64 liblua5.2-0 amd64 5.2.4-1.1+b2 [110 kB]
Get:8 http://deb.debian.org/debian buster/main amd64 apache2-bin amd64 2.4.38-3+deb10u3 [1307 kB]
Get:9 http://deb.debian.org/debian buster/main amd64 apache2-data all 2.4.38-3+deb10u3 [165 kB]
Get:10 http://deb.debian.org/debian buster/main amd64 apache2-utils amd64 2.4.38-3+deb10u3 [236 kB]
Get:11 http://deb.debian.org/debian buster/main amd64 apache2 amd64 2.4.38-3+deb10u3 [251 kB]
Get:12 http://deb.debian.org/debian buster/main amd64 ssl-cert all 1.0.39 [20.8 kB]
Fetched 2627 kB in 0s (38.8 MB/s)
Preconfiguring packages ...
Selecting previously unselected package libapr1:amd64.
(Reading database ... 31466 files and directories currently installed.)
Preparing to unpack .../00-libapr1_1.6.5-1+b1_amd64.deb ...
Unpacking libapr1:amd64 (1.6.5-1+b1) ...
Selecting previously unselected package libaprutil1:amd64.
Preparing to unpack .../01-libaprutil1_1.6.1-4_amd64.deb ...Unpacking libaprutil1:amd64 (1.6.1-4) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:amd64.
Preparing to unpack .../02-libaprutil1-dbd-sqlite3_1.6.1-4_amd64.deb ...
Unpacking libaprutil1-dbd-sqlite3:amd64 (1.6.1-4) ...
Selecting previously unselected package libaprutil1-ldap:amd64.
Preparing to unpack .../03-libaprutil1-ldap_1.6.1-4_amd64.deb ...
Unpacking libaprutil1-ldap:amd64 (1.6.1-4) ...
Selecting previously unselected package libbrotli1:amd64.
Preparing to unpack .../04-libbrotli1_1.0.7-2_amd64.deb ...
Unpacking libbrotli1:amd64 (1.0.7-2) ...
Selecting previously unselected package libjansson4:amd64.
Preparing to unpack .../05-libjansson4_2.12-1_amd64.deb ...
Unpacking libjansson4:amd64 (2.12-1) ...
Selecting previously unselected package liblua5.2-0:amd64.
Preparing to unpack .../06-liblua5.2-0_5.2.4-1.1+b2_amd64.deb ...
Unpacking liblua5.2-0:amd64 (5.2.4-1.1+b2) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../07-apache2-bin_2.4.38-3+deb10u3_amd64.deb ...
Unpacking apache2-bin (2.4.38-3+deb10u3) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../08-apache2-data_2.4.38-3+deb10u3_all.deb ...
Unpacking apache2-data (2.4.38-3+deb10u3) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../09-apache2-utils_2.4.38-3+deb10u3_amd64.deb ...
Unpacking apache2-utils (2.4.38-3+deb10u3) ...
Selecting previously unselected package apache2.
Preparing to unpack .../10-apache2_2.4.38-3+deb10u3_amd64.deb ...
Unpacking apache2 (2.4.38-3+deb10u3) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../11-ssl-cert_1.0.39_all.deb ...
Unpacking ssl-cert (1.0.39) ...
Setting up libbrotli1:amd64 (1.0.7-2) ...
Setting up libapr1:amd64 (1.6.5-1+b1) ...
Setting up libjansson4:amd64 (2.12-1) ...
Setting up ssl-cert (1.0.39) ...
Setting up liblua5.2-0:amd64 (5.2.4-1.1+b2) ...
Setting up apache2-data (2.4.38-3+deb10u3) ...
Setting up libaprutil1:amd64 (1.6.1-4) ...
Setting up libaprutil1-ldap:amd64 (1.6.1-4) ...
Setting up libaprutil1-dbd-sqlite3:amd64 (1.6.1-4) ...
Setting up apache2-utils (2.4.38-3+deb10u3) ...
Setting up apache2-bin (2.4.38-3+deb10u3) ...
Setting up apache2 (2.4.38-3+deb10u3) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for systemd (241-7~deb10u2) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10) ...
root@vps:~#

3.开启伪静态功能并创建网站配置文件
开启伪静态功能并创建网站1的配置文件

a2enmod  rewrite
touch /etc/apache2/sites-available/www.oneweb.com.conf

执行过程

root@vps:~# a2enmod  rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2
root@vps:~# systemctl restart apache2
root@vps:~# touch /etc/apache2/sites-available/www.oneweb.com.conf 
root@vps:~#

创建网站2的配置文件

touch /etc/apache2/sites-available/www.twoweb.com.conf 

执行过程

root@vps:~# touch /etc/apache2/sites-available/www.twoweb.com.conf  
root@vps:~# 

4.修改网站的配置文件
编辑网站1的配置文件

 vim /etc/apache2/sites-available/www.oneweb.com.conf  

写入的网站1的配置如下:

#Listen 80
<VirtualHost *:80>
	ServerAdmin [email protected]
	ServerName www.oneweb.com
	ServerAlias www.oneweb.com
	DocumentRoot "/var/www/www.oneweb.com/html"
	ErrorLog "/var/www/www.oneweb.com/log/error.log"
	Customlog "/var/www/www.oneweb.com/log/access.log" combined
	###设置网页认证(可选)###
	#<Directory /var/www/www.oneweb.com/html/>
	#AllowOverride All
	#Options All
	#AuthType Basic
	#AuthName "By Invitation Only"
	##生成密码文件的shell命令是:htpasswd -c /var/www/oneweb.com/passwd.db test
	#AuthUserFile /var/www/oneweb.com/passwd.db
	#Require user test
	#</Directory>
</VirtualHost>

编辑网站2的配置文件

 vim /etc/apache2/sites-available/www.twoweb.com.conf  

写入的网站2的配置如下:

#Listen 80
<VirtualHost *:80>
	ServerAdmin [email protected]
	ServerName www.twoweb.com
	ServerAlias www.twoweb.com
	DocumentRoot "/var/www/www.twoweb.com/html"
	ErrorLog "/var/www/www.twoweb.com/log/error.log"
	Customlog "/var/www/www.twoweb.com/log/access.log" combined
	DirectoryIndex index.htm index.html
</VirtualHost>

5.创建网页路径
A.创建网站1的路径

#创建网站路径
mkdir /var/www/www.oneweb.com
#创建网站1存放网页路径
mkdir /var/www/www.oneweb.com/html
#创建网站1存放日志路径
mkdir /var/www/www.oneweb.com/log

执行过程

root@vps:~# mkdir /var/www/www.oneweb.com
root@vps:~# mkdir /var/www/www.oneweb.com/html
root@vps:~# mkdir /var/www/www.oneweb.com/log

B.创建网站2的路径

#创建网站路径
mkdir /var/www/www.twoweb.com
#创建网站1存放网页路径
mkdir /var/www/www.twoweb.com/html
#创建网站1存放日志路径
mkdir /var/www/www.twoweb.com/log

执行过程

root@vps:~# mkdir /var/www/www.twoweb.com
root@vps:~# mkdir /var/www/www.twoweb.com/html
root@vps:~# mkdir /var/www/www.twoweb.com/log

6.配置权限
A.设置网站1的文件夹权限及用户

#设置网站1的路径下的文件夹的权限为755
chmod -R 755 "/var/www/www.oneweb.com"
#设置网站1的路径下html的文件夹的用户为www-data
chmod www-data -R "/var/www/www.oneweb.com/html"
#设置网站1的路径下日志的文件夹的权限为750
chmod 750 /var/www/www.oneweb.com/log

执行过程

root@vps:~# chmod -R 755 "/var/www/www.oneweb.com"
root@vps:~# chmod www-data -R "/var/www/www.oneweb.com/html"
root@vps:~# chmod 750 /var/www/www.oneweb.com/log

B.设置网站2的文件夹权限及用户

#设置网站2的路径下的文件夹的权限为755
chmod -R 755 "/var/www/www.twoweb.com"
#设置网站2的路径下html的文件夹的用户为www-data
chmod www-data -R "/var/www/www.twoweb.com/html"
#设置网站1的路径下日志的文件夹的权限为750
chmod 750 /var/www/www.twoweb.com/log

执行过程

root@vps:~# chmod -R 755 "/var/www/www.twoweb.com"
root@vps:~# chmod www-data -R "/var/www/www.twoweb.com/html"
root@vps:~# chmod 750 /var/www/www.twoweb.com/log

7.放入测试页面
A.放入网站1的测试页面

echo "This is www.oneweb.com!" >"/var/www/www.oneweb.com/html/index.html"

执行过程

root@vps:~# echo "This is www.oneweb.com!" >"/var/www/www.oneweb.com/html/index.html"

B.放入网站2的测试页面

echo "This is www.twoweb.com!" >"/var/www/www.twoweb.com/html/index.html"

执行过程

root@vps:~# echo "This is www.oneweb.com!" >"/var/www/www.twoweb.com/html/index.html"

8.关闭默认站点并启动自定义的两个站点

#关闭默认站点
a2dissite 000-default.conf
#启动我们创建的站点1
a2ensite www.oneweb.com.conf
#启动我们创建的站点2
a2ensite www.twoweb.com.conf

执行过程

root@vps:~# a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:   systemctl reload apache2
root@vps:~# a2ensite www.oneweb.com.conf
Enabling site www.oneweb.com.
To activate the new configuration, you need to run:   systemctl reload apache2
root@vps:~# a2ensite www.twoweb.com.conf 
Enabling site www.twoweb.com.
To activate the new configuration, you need to run:   systemctl reload apache2 

9.重启服务

service apache2 restart

执行过程

root@vps:~# service apache2 restart
root@vps:~# 

验证

A.打开浏览器查看网站1

www.oneweb.com

在这里插入图片描述

B.打开浏览器查看网站1

www.twoweb.com

在这里插入图片描述
可以看到都成功访问了,我们成功的在一台服务器上搭建使用同一80端口搭建了两个网站

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章