在服务器中部署Web页面服务的笔记
Author | MemoryShadow |
---|---|
MemoryShadow@outlook.com | |
Github | 链接 |
在服务器中部署Web页面服务的笔记
在已有的Ubuntu或者CentOS 7服务器中部署Web页面服务
注:如无特定提醒,则在两个系统中应该是大体相同的,可能有细微差别
已准备的材料列表:
- 一台部署了Ubuntu 18.04.4 LTS或者CentOS 7的云服务器
- 一个已经备案的域名(国外不用备案)
- SSL证书
安装httpd服务
使用apt安装apache
# 安装 apache2 服务
sudo apt install apache2 -y
使用yum安装apache
# 安装 apache2 服务
sudo yum install httpd -y
安装完成后,先启动一下服务
# 启动 apache2
sudo systemctl start apache2
# 如果你是CentOS请使用下面这条
# 启动 apache2
sudo systemctl start httpd
修改页面目录
为了未来权限分配/便于管理,我在服务器中新建了一个用户Web,来储存页面内容,并且为它分配了ssh登录所需要的密钥文件(其中具体的步骤我会开新的文章来写),接下来我们需要修改目录.
apache2和apache配置文件不大一样,它的配置文件其实是储存在/etc/apache2/sites-available/000-default.conf
这个位置的,并没有传说中的http.conf文件(但是CentOS是有的),我们用vim打开一下
# 使用 vim 打开 apache2 配置文件
sudo vim /etc/apache2/sites-available/000-default.conf
# 如果你是CentOS 7请使用下面这条命令
# 使用 vim 打开 httpd.conf 配置文件
sudo vim /etc/httpd/conf/httpd.conf
打开此文件后,第12行(CentOS在119行)就是我们此行的目标了,我们看到DocumentRoot /var/www/
这一行(其实直接使用vim搜索DocumentRoot
关键字即可),这就是我们需要修改的内容,把这个改成我们之前准备的/home/Web/Root/
即可.
那么除此以外呢,我们还需要顺便留下我们的联系方式,以免有人遇到问题时联系不上我们,也就是我们的第11行(CentOS在86行)中的ServerAdmin字段,我们将其改为我们自己的邮箱 :
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin MemoryShadow@outlook.com
DocumentRoot /home/Web/Root/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
在完成修改后,我们重启一下apache2服务,并且curl一下我们的页面
# 重启服务
sudo systemctl restart apache2
# 拉取页面
curl 127.0.0.1
我们可以得到如下信息
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at 127.0.0.1 Port 80</address>
</body></html>
发现页面目前是403的状态,说明可能是权限尚未配置好,于是我就将目录/home/Web/Root/
和其子文件的权限都给予了读取权限
# 赋予同组读取权限
sudo chmod -R g+r /home/Web/Root/
# 赋予其他人读取权限
sudo chmod -R o+r /home/Web/Root/
完成后,重启apache2服务,发现依旧是403页面,这时,我想到了apache2本身自带了一个目录权限配置,就在文件/etc/apache2/apache2.conf
中,于是我使用vim打开它,并搜索Require相关字段
# 打开文件
sudo vim /etc/apache2/apache2.conf
# 接下来的命令应当在vim的一般模式下输入
# 这是vim相关帮助 https://www.cnblogs.com/mabingxue/p/10281132.html
/Require
果然,直接跳转到了权限控制的部分,这里贴出原文文档,便于日后参考.
...152
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
...
我将最下面的/var/www
目录改为我希望存放的目录/home/Web/Root/
重启apache2服务,页面就能正常访问了
但是CentOS还需要做一步,就是重新设置我们运行服务的用户的权限,在文件/etc/httpd/conf/httpd.conf
中搜索User/Group关键字
# 打开文件
sudo vim /etc/httpd/conf/httpd.conf
# 以下指令在vim的命令模式中输入(以斜杠开头的文本会被认为是搜索)
/User/Group
我们会看到User和Group字段,将他们改为你希望的用户和组,例如:
User Web
Group Web
重启httpd服务,页面就能正常访问了
配置HTTPS
接下来,我们来参照这篇文章配置HTTPS服务
首先解压之前下载在服务器上的证书文件压缩包中的Apache目录在你的自定义目录中,你应该能看到以下三个文件,他们分别是
- 证书链文件:
1_root_bundle.crt
或者随机数字_你的域名_chain.crt
- 证书文件:
2_你的域名.crt
或者随机数字_你的域名_public.crt
- 私钥文件:
3_你的域名.key
或者随机数字_你的域名.key
接下来,我们链接一下配置文件从我们的模板目录到启用目录来启用SSL的配置
sudo ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/000-default-ssl.conf
而同时,CentOS 7用户则需要安装SSL模块后才能配置SSL相关
# 安装ssl模块
sudo yum install mod_ssl openssl
# 再打开对应的配置文件
sudo vim /etc/httpd/conf.d/ssl.conf
这个文件的配置和我们的/etc/apache2/sites-available/000-default.conf
是不是很像?是的,你就要像上面配置这个文件那样去配置它,输入你的邮箱和页面根目录,再在根目录下新增一个ServerName字段,这个字段中填写你的域名.
接下来就是重头戏了
我们向下翻,找到这几个字段,并填入对应的值:
- SSLEngine 将值改为on,这个操作将会启用SSL加密
- SSLCertificateFile 证书文件
2_你的域名.crt
对应的路径 - SSLCertificateKeyFile 私钥文件
3_你的域名.key
对应的路径 - SSLCertificateChainFile 证书链文件
1_root_bundle.crt
对应的路径
在填完这几个字段后,就能保存并退出了,接下来启用一下SSL模块
# 启用SSL模块
sudo a2enmod ssl
# 重启apache2服务
sudo systemctl restart apache2
CentOS用户此时只需要重启httpd服务即可
至此,网站可以使用https打开了.
强制HTTPS访问
但是仅仅是支持怎么能保护用户信息呢?肯定要强制跳转的呀
回到/etc/apache2/sites-available/000-default.conf
这个文件,在VirtualHost
标签中(CentOS 7用户直接放在/etc/httpd/conf/httpd.conf
文件的末尾然后重启服务即可)新增以下代码:
# 页面重定向功能
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
并且在终端中执行以下命令:
# 启用重定向模块
sudo a2enmod rewrite
sudo systemctl restart apache2
接下来执行curl http://127.0.0.1
命令,看到的就是302重定向页面了,并且在浏览器中打开也会自动跳转到https页面中
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://127.0.0.1/">here</a>.</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at 127.0.0.1 Port 80</address>
</body></html>
自定义错误页面
有时候我们需要自定义错误页面,例如404时,我们需要显示走失儿童的信息.
要实现自定义错误页面有两种方法
使用.htaccess文件
我们可以在网站目录放置一个名为.htaccess的文件,然后写入配置文件
注意: 在实际生产环境中,并不建议使用此文件,引用来自此文章的相关信息:
.htaccess文件一般情况下,不应该使用.htaccess文件,除非对主配置文件没有访问权限。用户认证不但能使用.htaccess文件实现,把用户认证写在主配置文件中是完全可行的,而且是一种很好的方法。
任何希望放在.htaccess 文件中的配置,都可以在主配置文件的<Directory>段中,而且更高效。避免使用.htaccess文件有两个主要原因:
①性能。如果AllowOverride启用了.htaccess文件,则Apache需要在每个目录查找.htaccess文件,无论是否找到,启用都会导致性能下降。另外,对每一个请求,都需要读取一次.htaccess文件,还有Apache必须在所有上级的目录中查找.htaccess文件,以使所有有效的指令都起作用,例如:
如果请求/www/htdocs/example中的页面,Apache必须查找以下文件:
/.htaccess /www/.htaccess /www/htdocs/.htaccess /www/htdocs/example/.htaccess
②安全。这样会允许用户自己修改服务器的配置,这会导致某些意想不到的修改。注意:在/www/htdocs/example目录下的.htaccess文件中的放置指令,与在主配置文件中<Directory /www/htdocs/example>段中放置相同的指令,是完全等效的。
接下来,我们在主配置文件/etc/apache2/apache2.conf
中搜索条目AllowOverride
来改写我们的规则.
# 打开主配置文件
sudo vim /etc/apache2/apache2.conf
# 接下来的命令应当在vim的一般模式下输入
# 这是vim相关帮助 https://www.cnblogs.com/mabingxue/p/10281132.html
/AllowOverride
我们看到的配置信息是不是很眼熟?对的,就是在我们上面解决403问题时用到过,找到你当时改写的条目,查看其中的AllowOverride
条目,将它默认的None
选项改为ALL
表示此目录下所有文件接受.htaccess的重写,再重启apache2的服务
<Directory /home/Web/Root/>
Options Indexes FollowSymLinks
AllowOverride ALL
Require all granted
</Directory>
sudo systemctl restart apache2
至此,错误页面就会依照.htaccess中配置的规则1运作了
使用主配置文件
我们也可以将规则写入主配置文件,这样子规则就对站点下所有的目录生效,但是每一次更改规则都需要重启服务
打开我们的主配置文件/etc/apache2/apache2.conf
,搜索条目Directory
sudo vim /etc/apache2/apache2.conf
# 接下来的命令应当在vim的一般模式下输入
# 这是vim相关帮助 https://www.cnblogs.com/mabingxue/p/10281132.html
/Directory
没错,这里还是我们熟悉的配置信息,找到你之前配置的目录条目,在Directory
标签中,为其新增条目
例如:
<Directory /home/Web/Root/>
Options Indexes FollowSymLinks
AllowOverride None #注意这里不用启用规则重写
Require all granted
ErrorDocument 404 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=404
</Directory>
新增完毕后,重启服务,规则就应用到此站点下的所有目录了.
sudo systemctl restart apache2
这两种方法的区别与优劣
使用.htaccess文件 | 使用主配置文件 |
---|---|
不需要重启服务 | 每次更改需要重启服务 |
能够为站点中每个目录配置单独的规则 | 站点中采用一致的规则 |
只要能够写入站点目录就能更改规则 | 更改站点规则需要管理员权限 |
具有显著的性能问题 | 相比起.htaccess方案更高效 |
配置语法以及示例
ErrorDocument 404 你的404页面路径
在这里放出一个简单的404页面代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404页面不存在</title>
</head>
<body>
<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/hybrid/app/404/search_children.js" charset="utf-8" homePageUrl="http://yoursite.com/yourPage.html" homePageName="回到我的主页"></script>
</body>
</html>
当然,我们有时候可能还需要为其他的错误代码显示错误页面,在相同的文件中留下对应的条目即可,这里留下我的网站使用的错误代码规则:
# 此错误代码提供项目API手册:https://github.com/MemoryShadow/MemoryShadow.github.io/blob/master/README.md#ErrorMsg
ErrorDocument 401 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=401
ErrorDocument 403 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=403
ErrorDocument 404 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=404
ErrorDocument 500 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=500
ErrorDocument 501 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=501
ErrorDocument 503 https://memoryshadow.freetzi.com/ErrorMsg/?ErrorCode=503