【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端口搭建了兩個網站

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