所以我一直提倡抛弃面板管理站点,不仅可以提高自己的技术力,还能依靠自己的喜好更改站点配置,于是乎我撰写了这篇文章,文章面向的受众为有一定计算机基础(起码知道内外网吧)的准萌新,通过学习本篇教程,你应该可以抛弃面板独自配置 Web 环境,完善的建立并管理一个自己的站点(或是 CMS,或是一个静态网站)。
如果你使用的 Linux 发行版作为你的站点服务器系统,那么你就需要了解一下根目录里面的文件夹都是用来做什么的,这样你才能随心所欲的更改你想要更改的配置或者检索应用运行日志等操作。相对于 Windows 操作系统来说,Linux 发行版的入门确实要更为困难,但是作为初学者的我们,只需要掌握几个特定的文件夹作用就可以了,下面列出了本篇教程所需要的文件夹及其作用
>>>ssh -p [port] [user]@[host] # 如果ssh端口为22默认端口,-p [port]参数可省略 The authenticity of host 'HostName (HostIP)' can't be established. ECDSA key fingerprint is SHA256:HostKey. Are you sure you want to continue connecting (yes/no/[fingerprint])? # 确认该主机的ECDSA密钥指纹,输入yes password: # 输入用户账户密码,注意输入时不会显示 # 所以你键入键盘后依然空白属于正常现象 # 输入完成后回车即可
主流的富文本编辑器亦有很多种,如巨硬做的 VS Code,JB 做的 Fleet、个人开发的 Sublime Text(收费)、GitHub 做的 Atom 和 Linux 的 Vim,任选其一安装即可
我将使用 VS Code(注意区分 Visual Studio/VS,后者为 IDE 集成开发环境)进行演示,其他富文本编辑器的功能大抵都是大同小异,进入官网Visual Studio Code - Code Editing. Redefined,您可以直接点击主页上的“Download For Windows,Stable Build”进入下载页面;或者可以选择下面一行中的“other platforms”进入多平台选择页面,推荐选择“System Installer”一行(64 位、32 位和 ARM)。
安装应用以自己喜欢的方式安装即可,这里不再赘述,安装完成后运行会首先出现欢迎界面,您可以在其中自行选择配置主题、文件关联和学习技巧,右下角会提示安装“简体中文语言包/Simple Chinese Language Package”,选择 Install 安装重启应用即可,有关 VS Code 的配置、美化教程可自行检索。
域名的解析也需要在 ISP 控制台进行,推荐先使用*的泛域名 A 记录解析到你的服务器公网 IP 之后在分别增加诸如 www 或 blog 的二级域名,这样有二级域名输入错误的同学也能重定向到你的默认站点,增加访问的容错性。具体如何使用不同的域名访问到一个服务器的不同站点,我在后面的“虚拟站点”一节会有详细介绍,这里有一个小贴士:请尽量规范化你的网址且尽量简短。
增加易用性
安装必备软件
Git,Zip/Unzip,vim,
修改软件源
将系统更新
Web Server
该章节
安装 Apache
在 Linux 发行版中,有的系统可能直接内置了诸如 Apache 或 Nginx 之类的 Web 服务器,你可以使用各自的指令运行测试(详见下一节),如果没有安装也没关系,Linux 拥有功能极为强大的包管理工具。你完全不必像寻找检索 Windows 软件那样,重定向到一些下载站或被捆绑安装一系列全家桶,Linux 发行版系统包管理工具将会自动检索该软件所需要的依赖问题,以及高度自动化的安装、更新和卸载,并且拥有校验软件源。
>>>apt update && upgrade # 非必须,只是确保系统、软件处于最新的版本 >>>apt install apache2 -y # Debian系的包管理工具为Apt,具体使用可以参考附录 # 在Debian及其下游的发行版中,Apache的包名为apache2 # ...省略安装过程 >>> >>>apache2 -v Server version: Apache/2.4.54 (Debian) Server built: 2022-06-09T04:26:43 >>>apache2 -k start [Wed Dec 28 09:22:09.129079 2022] [core:warn] [pid 4027] AH00111: Config variable ${APACHE_RUN_DIR} is not defined apache2: Syntax error on line 80 of /etc/apache2/apache2.conf: DefaultRuntimeDir must be a valid directory, absolute or relative to ServerRoot
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
检查无误后,你可以访问你站点的 IP 地址或者已经正确解析 A 记录的域名地址(注意使用外网地址),如果不出什么意外的话,你会正确的定向到“HTTP SERVER TEST PAGE”、“Apache2 Debian Default Page”或类似的页面,不必惊慌,这个页面本来就是为了让你测试站点的!当你测试正常后,你便可以继续接下来的教程,如果出现了什么问题,可以试图参照下面的一份问题检查清单检查你的站点配置
无法正常访问测试站点的检查清单
站点显示 HTTP 错误码
站点提示诸如 403、404、500 之类的数字 HTTP 状态码?这证明站点的 Web 服务器已经正常安装,但是看起来运行起来有点错误,你可以先行参考后面的异常处理章节,或者按部就班进行学习配置后再次尝试
站点显示连接超时等等
如果你的站点提示为浏览器的异常:诸如访问超时,无法访问此站点等等,那这问题出在了服务器本身,重新检查测试 TCP 80/443/8080 端口是否开放且能正常访问,检查 IP 地址或 URL 是否输入正确,检查 Web 服务器是否成功安装、运行等等
还是无法访问
请使用清除缓存强制刷新“Ctrl+F5”操作,或者更换浏览器,或者使用 IP 直接访问(防止 DNS 污染),依然不行就只能求助身边的大佬了
主流服务器
除外 Apache 外,你还可以使用其他的主流 Web 服务器例如 Nginx(常说的 LNMP 环境中的 N)和 IIS(捆绑于 Windows Server)等,这里简要概述一下,不做具体讲解。
Nginx
IIS
Internet Information Services(IIS,互联网信息服务),作为巨硬家的 Web 服务器,IIS 一般内置在 Windows Server 系统里面不需要独立安装,你可以使用 IIS 在 Windows 系统下开设 Web 网页服务,用来搭载网站运行程序的平台,还能提供 FTP,SMTP 等服务。IIS 提供对.Net、PHP、JSP 等后端语言环境或后端框架的支持,也继承了 Windows 全图形化的配置更改模式,除非必要否则不建议个人使用 IIS 作为你的 Web 服务器。
如果你非要使用 Windows Server 作为你的站点管理系统,你可以在 Windows 下使用 Apache 或者 Nginx,二者均提供了 Windows 平台的二进制分发包或应用管理程序,配置文件也大多通用,相比 IIS,这两种主流服务器应用程序对于个人来说意味着拥有更新更好的网络教程和安全措施,且对 PHP 等后端语言环境的支持是 IIS 所不能媲美的。当然,如果你说我就想用 IIS,我就想微软服务为我背锅,那请使用较新的 Windows Server 系统譬如 2022,以及使用正版商业授权确保可以得到巨硬的技术支持。
IIS 并非一无是处,其在商业领域拥有可以媲美 Apache 的份额,且性能和安全也有巨硬背锅,是微软的战略产品,但是面向个人我少有看到个站站长使用 Windows+IIS 的奇怪组合,本篇教程也是面对 Linux 发行版的 LAMP/LNMP(Linux 发行版+Apache/Nginx 服务器+MySQL/MariaDB 数据库+PHP/Python 语言环境)的主流 Web 服务器运行模式,所以 IIS 相关的内容在后文不再予以提及,请自行参阅技术文档。
# This is the main Apache server configuration file. It contains the # configuration directives that give the server its instructions. # See http://httpd.apache.org/docs/2.4/ for detailed information about # the directives and /usr/share/doc/apache2/README.Debian about Debian specific # hints. # # # Summary of how the Apache 2 configuration works in Debian: # The Apache 2 web server configuration in Debian is quite different to # upstream's suggested way to configure the web server. This is because Debian's # default Apache2 installation attempts to make adding and removing modules, # virtual hosts, and extra configuration directives as flexible as possible, in # order to make automating the changes and administering the server as easy as # possible.
# It is split into several files forming the configuration hierarchy outlined # below, all located in the /etc/apache2/ directory: # # /etc/apache2/ # |-- apache2.conf # | `-- ports.conf # |-- mods-enabled # | |-- *.load # | `-- *.conf # |-- conf-enabled # | `-- *.conf # `-- sites-enabled # `-- *.conf # # # * apache2.conf is the main configuration file (this file). It puts the pieces # together by including all remaining configuration files when starting up the # web server. # # * ports.conf is always included from the main configuration file. It is # supposed to determine listening ports for incoming connections which can be # customized anytime. # # * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/ # directories contain particular configuration snippets which manage modules, # global configuration fragments, or virtual host configurations, # respectively. # # They are activated by symlinking available configuration files from their # respective *-available/ counterparts. These should be managed by using our # helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See # their respective man pages for detailed information. # # * The binary is called apache2. Due to the use of environment variables, in # the default configuration, apache2 needs to be started/stopped with # /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not # work with the default configuration.
# Global configuration #
# # ServerRoot: The top of the directory tree under which the server's # configuration, error, and log files are kept. # # NOTE! If you intend to place this on an NFS (or otherwise network) # mounted filesystem then please read the Mutex documentation (available # at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>); # you will save yourself a lot of trouble. # # Do NOT add a slash at the end of the directory path. # ServerRoot "/etc/apache2"
# # The accept serialization lock file MUST BE STORED ON A LOCAL DISK. # # Mutex file:${APACHE_LOCK_DIR} default
# # The directory where shm and other runtime files will be stored. #
DefaultRuntimeDir ${APACHE_RUN_DIR}
# # PidFile: The file in which the server should record its process # identification number when it starts. # This needs to be set in /etc/apache2/envvars # PidFile ${APACHE_PID_FILE}
# # Timeout: The number of seconds before receives and sends time out. # Timeout 300
# # KeepAlive: Whether or not to allow persistent connections (more than # one request per connection). Set to "Off" to deactivate. # KeepAlive On
# # MaxKeepAliveRequests: The maximum number of requests to allow # during a persistent connection. Set to 0 to allow an unlimited amount. # We recommend you leave this number high, for maximum performance. # MaxKeepAliveRequests 100
# # KeepAliveTimeout: Number of seconds to wait for the next request from the # same client on the same connection. # KeepAliveTimeout 5
# These need to be set in /etc/apache2/envvars User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP}
# # HostnameLookups: Log the names of clients or just their IP addresses # e.g., www.apache.org (on) or 204.62.129.132 (off). # The default is off because it'd be overall better for the net if people # had to knowingly turn this feature on, since enabling it means that # each client request will result in AT LEAST one lookup request to the # nameserver. # HostnameLookups Off
# ErrorLog: The location of the error log file. # If you do not specify an ErrorLog directive within a <VirtualHost> # container, error messages relating to that virtual host will be # logged here. If you *do* define an error logfile for a <VirtualHost> # container, that host's errors will be logged there and not here. # ErrorLog ${APACHE_LOG_DIR}/error.log
# # LogLevel: Control the severity of messages logged to the error_log. # Available values: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the log level for particular modules, e.g. # "LogLevel info ssl:warn" # LogLevel warn
# Include module configuration: IncludeOptional mods-enabled/*.load IncludeOptional mods-enabled/*.conf
# Include list of ports to listen on Include ports.conf
# 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>
# AccessFileName: The name of the file to look for in each directory # for additional configuration directives. See also the AllowOverride # directive. # AccessFileName .htaccess
# # The following lines prevent .htaccess and .htpasswd files from being # viewed by Web clients. # <FilesMatch "^\.ht"> Require all denied </FilesMatch>
# # The following directives define some format nicknames for use with # a CustomLog directive. # # These deviate from the Common Log Format definitions in that they use %O # (the actual bytes sent including headers) instead of %b (the size of the # requested file), because the latter makes it impossible to detect partial # requests. # # Note that the use of %{X-Forwarded-For}i instead of %h is not recommended. # Use mod_remoteip instead. # LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %O" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent
# Include of directories ignores editors' and dpkg's backup files, # see README.Debian for details.
# Include generic snippets of statements IncludeOptional conf-enabled/*.conf
# Include the virtual host configurations: IncludeOptional sites-enabled/*.conf
# If you just change the port or add more ports here, you will likely also # have to change the VirtualHost statement in # /etc/apache2/sites-enabled/000-default.conf