在 CentOS 7 上安装 Apache 和 MariaDB 10.2,并进行 SFTP 连接
引入
因为重新安装樱花VPS的操作系统升级到了CentOS 7,花费了很多时间和精力,所以我把这个过程作为备忘录留下来。
假设已经使用OpenSSH,以下描述。客户端在Windows 7上使用FileZilla Client。此外,在创建密钥时使用了Tera Term。
阿帕奇
安装
在CentOS官方的yum仓库中提供了Apache 2.4的RPM软件包,因此我们将进行安装。
对于樱花VPS来说,Apache已经被安装,但还需要进行启用和启动。
# Apacheのインストール
sudo yum install httpd
# httpd.service の有効化 (OS起動時に自動的に起動)
sudo systemctl enable httpd
# 起動
sudo systemctl start httpd
我将根据以下文章参考来进行Apache的设置。
-
- Apache に関する SELinux の設定 – Qiita
- Apache 2.4 設定ファイルの記述例 – Qiita
防火墙设置
#【mod_sslを利用していない場合】TCPの80番ポートの通信を許可
sudo firewall-cmd --permanent --add-service=http
#【mod_sslを利用している場合】TCPの80番ポートと443番ポートの通信を許可
sudo firewall-cmd --permanent --add-service=http --add-service=https
# firewalld の設定を再読み込み
sudo firewall-cmd --reload
如果你正在使用非默认端口,我们将按照以下方式进行允许。
# (例) TCPの49152番ポートの通信を許可
sudo firewall-cmd --permanent --add-port=49152/tcp
对Apache的操作
httpd -v
バージョンを表示します。sudo -u apache apachectl configtest
設定ファイルに文法ミスが無いか確認します。sudo systemctl start httpd
サービスを開始します。sudo httpd -k stop
サービスを停止します。1 2sudo systemctl stop httpd
サービスを緩やかに停止します。3sudo httpd -k restart
サービスを再起動します。1sudo service httpd reload
サービスを緩やかに再起動します。4sudo httpd -k stop; sleep 1s; sudo systemctl start httpd
サービスを親プロセスも含めて完全に再起動します。1sudo systemctl restart httpd
サービスが緩やかに停止するのを待機した後で再起動します。sudo tail /var/log/httpd/error_log
tail
コマンドでエラーログの末尾10行を表示します。5sudo tail /var/log/httpd/access_log
tail
コマンドでアクセスログの末尾10行を表示します。5 6MariaDB 是一個開源的關聯式資料庫管理系統。
*.repo 文件的语法高亮
在CentOS的标准yum软件源中提供的除了Vim之外的文本编辑器,在初始状态下无法实现仓库定义文件的语法高亮功能。需要进行一些设置文件的编辑等操作,以启用语法高亮功能。
GNU Emacs是一款功能强大的文本编辑器和开发环境。
创建(或追加)个人设定文件,并添加一个设置,使以.repo为后缀的文件在conf-mode下打开。
;; .repo ファイルを Conf モードで開く
(add-to-list 'auto-mode-alist '("\\.repo$" . conf-mode))
文本编辑器
将以.repo结尾的文件作为INI文件打开,并在每行注释的起始处添加 # 符号。
<!--##############
## 以上省略 ##
##############-->
<metadata>
<property name="mimetypes">text/x-ini-file;application/x-ini-file</property>
<property name="globs">*.ini;*.repo</property><!--## *.repo を追加 ##-->
<property name="line-comment-start">;</property>
</metadata>
<!--##############
## 中略 ##
##############-->
<definitions>
<context id="line-comment" style-ref="comment" end-at-line-end="true">
<start>;</start>
</context>
<!--## ここから ##-->
<context id="bash-line-comment" style-ref="comment" end-at-line-end="true">
<start>#</start>
</context><!--## ここまで ##-->
<context id="group" style-ref="keyword">
<start>^\[</start>
<end>\]$</end>
</context>
<!--##############
## 中略 ##
##############-->
<context id="ini">
<include>
<context ref="line-comment"/>
<context ref="bash-line-comment"/><!--## 追加 ##-->
<context ref="group"/>
<context ref="non-standard-key"/>
<context ref="language"/>
<context ref="variable"/>
<context ref="boolean-value"/>
<context ref="single-quoted-string"/>
<context ref="double-quoted-string"/>
<context ref="decimal-number"/>
<context ref="integer"/>
</include>
</context>
</definitions>
</language>
凯特
将以.repo结尾的文件作为INI文件打开。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="INI Files" section="Configuration" extensions="*.ini;*.pls;*.kcfgc;*.repo" mimetype="" version="1.1" kateversion="2.0" author="Jan Janssen (medhefgo@web.de)" license="LGPL"> <!-- extensions属性値に *.repo を追加 -->
<!--##############
## 以下省略 ##
##############-->
纳米
使用 Arch Linux 上的 nano-syntax-highlighting-git 软件包。7 8
# INIファイルのシンタックスハイライトを行う設定ファイルをダウンロード
wget https://raw.githubusercontent.com/scopatz/nanorc/master/ini.nanorc
# .repo で終わるファイルをINIファイルとして扱うようにする
sed --in-place 's/)\$/|repo)$/' ini.nanorc
# シンタックスハイライトを行う設定ファイル保管場所に移動
sudo mv ini.nanorc /usr/share/nano/
# nanoの個人設定ファイルから読み込むようにする
echo $'\n\n'## INI files$'\n'include "/usr/share/nano/ini.nanorc" >> ~/.nanorc
安裝
在CentOS的官方仓库中,提供了旧版本的MariaDB 5.5。因此,我们需要添加MariaDB的官方仓库,并从该仓库安装最新版本。
使用yum安装MariaDB- MariaDB知识库
阅读上一页,并按照步骤完成安装。
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
# MariaDBのサーバー・コマンドラインクライアントをインストール
sudo yum install MariaDB-server MariaDB-client
# 起動
sudo systemctl start mariadb
RPM包下载完成后,会询问是否可以导入GPG公钥,请确认指纹与https://mariadb.com/kb/ja/gpg/中的指纹是否大小写不敏感一致。
设定符号化方式
如果要在 MariaDB 的設定文件 (*.cnf) 中啟用語法高亮,請將 *.repo 的語法高亮設置轉成 cnf 並進行設定。
服务器
MariaDB默认的字符集是latin1,这表示它使用的是”windows-1252″编码方式。通过编辑配置文件,可以将MariaDB的字符集修改为UTF-8的字符集utf8mb4。需要注意的是,MariaDB的utf8字符集表示的是”无法处理辅助字符(4字节字符)的UTF-8″。
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
character-set-server=utf8mb4
log-bin=mariadb-bin
## ↑この2行を追加 ##
event-scheduler=ON
## ↑イベントスケジューラを有効化する場合はこの行も追加 ##
# this is only for the mysqld standalone daemon
[mysqld]
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.0 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.0]
根据 character_set_server 的设置,character_set_database、collation_server、collation_database 会自动配置。log-bin 的值 mariadb-bin 是二进制日志文件名的前缀,可以使用任意值。客户端的编码方式取决于客户端的设置。
# 設定の反映
sudo systemctl restart mariadb
在重新加载 mariadb 时,系统变量的设置将不会生效。
命令行客户端
将MariaDB-client中包含的命令(如mysql等)的默认编码方式从auto更改为utf8mb4。
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
default-character-set=utf8mb4
## ↑この行を追加 ##
# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
另外,default_character_set已被废弃,并更名为character_set_server,这是关于服务器系统变量的问题。在mysql命令等中,仍然是–default-character-set选项,请注意。
其他初始设置
mysql_secure_installation – MariaDB 知识库- 进行安全安装
查看上一页,执行mysql_secure_installation命令。由于尚未设置root密码,对于Set root password? [Y/n],请直接按Enter键。11
[100@localhost ~]$ mysql_secure_installation ⏎
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): ⏎ ## 現在のルートパスワード。未設定なのでそのままEnter ##
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] ⏎ ## MariaDBのrootユーザーにパスワードを設定するか訊かれる。「Y」(Yes) が既定値なのでそのままEnter ##
New password: ******** ⏎ ## MariaDBのrootユーザーのパスワードを入力してEnter ##
Re-enter new password: ******** ⏎ ## 同じパスワードを再入力してEnter ##
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] ⏎ ## 匿名ユーザーを削除するか訊かれる。「Y」(Yes) が既定値なのでそのままEnter ##
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] ⏎ ## rootユーザーのリモートログインを禁止するか訊かれる。「Y」(Yes) が既定値なのでそのままEnter ##
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] ⏎ ## testデータベースを削除するか訊かれる。「Y」(Yes) が既定値なのでそのままEnter ##
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] ⏎ ## 権限テーブルを再読み込みするか訊かれる。「Y」(Yes) が既定値なのでそのままEnter ##
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
进行数据库恢复和创建用户。
执行mysql –user=root –password命令,以MariaDB的root用户登录。
-- リストア (ファイル名が backup.sql の場合)
source backup.sql;
-- 一般ユーザーの作成と ⦅データベース名A⦆ に対する権限の付与
GRANT ALL PRIVILEGES ON '⦅データベース名A⦆'.* TO '⦅MariaDBのユーザー名⦆'@localhost IDENTIFIED BY '⦅MariaDBのユーザーのパスワード⦆';
-- 作成したユーザーに ⦅データベース名B⦆ に対する権限も付与
GRANT ALL PRIVILEGES ON '⦅データベース名B⦆'.* TO '⦅MariaDBのユーザー名⦆'@localhost;
-- 権限テーブルの再読み込み
FLUSH PRIVILEGES;
-- ログアウト
exit
安全文件传输协议 (SFTP)
创建SFTP用户。
我们将拥有以主目录作为SFTP根目录的用户称为”homeroot”,而将以主目录下属目录public_html作为SFTP根目录的用户称为”dirroot”。
# シェルにログインできないユーザーを作成
sudo adduser --shell /sbin/nologin homeroot
sudo passwd homeroot
sudo adduser --shell /sbin/nologin dirroot
sudo passwd dirroot
设置SSH公钥。
-
- 从Tera Term菜单栏中打开”设置” – “SSH密钥生成”。
-
- 点击”生成”按钮将生成一把密钥。
-
- 输入密码短语。11
点击”保存私钥”按钮,并将其保存到任意位置。12
点击”保存公钥”按钮,并将其保存到任意位置。12
将保存的公钥拖放到Tera Term窗口中,使用”SCP”进行文件传输。
以下是对「homeroot」公钥命名为homeroot.pub、「dirroot」公钥命名为dirroot.pub的说明。
# SSHの設定ディレクトリを作成
sudo mkdir --mode=500 /home/{homeroot,dirroot}/.ssh
# 公開鍵を設定ディレクトリに配置
sudo mv ~/homeroot.pub /home/homeroot/.ssh/authorized_keys
sudo mv ~/dirroot.pub /home/dirroot/.ssh/authorized_keys
# SSHの設定ディレクトリ、および公開鍵のアクセス権・所有者を変更
sudo chmod 400 /home/{homeroot,dirroot}/.ssh/authorized_keys
sudo chown --recursive homeroot:homeroot /home/homeroot/.ssh
sudo chown --recursive dirroot:dirroot /home/dirroot/.ssh
#【SELinux】公開鍵のラベルを貼り直す
sudo restorecon /home/{homeroot,dirroot}/.ssh/authorized_keys
设置SFTP根目录
如果SFTP根目录及其上级目录的所有者不是root,则在设置了ChrootDirectory时将被拒绝连接。
# SFTPルートディレクトリの作成、ディレクトリの所有者を変更
sudo chown root /home/{homeroot,dirroot}
sudo chmod 750 /home/{homeroot,dirroot}
sudo mkdir --mode=775 /home/dirroot/public_html
sudo chgrp dirroot /home/dirroot/public_html
#【SELinux】SSHによるホームディレクトリの読み書きを許可
sudo setsebool -P ssh_chroot_rw_homedirs on
sudo restorecon -R /home/homeroot
在这里,我们禁止对主目录进行写入操作。
从文档根目录进行挂载
以下是在HTTP服务器的文档根目录下直接上传文件的例子。
##############
## 以上省略 ##
##############
# ↓この行を追記
/var/www/common /home/ftpuser/common none bind 0 0
请务必在 /etc/fstab 文件的末尾插入换行符。
# ドキュメントルートのグループを変更、グループにも書き込み権限を与える
sudo chgrp homeroot /var/www/html
sudo chmod 775 /var/www/html
# マウント先のディレクトリ (マウントポイント) を作成
sudo mkdir /home/homeroot/html
# /etc/fstab の設定を適用
sudo mount -a
OpenSSH的配置
##############
## 以上省略 ##
##############
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
#Subsystem sftp /usr/libexec/openssh/sftp-server
## ↑この行をコメントアウト ##
Subsystem sftp internal-sftp
## ↑この行を追加 ##
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
## ↓この行以降を追記 ##
Match User homeroot,dirroot
ForceCommand internal-sftp
Match User homeroot
ChrootDirectory %h
Match User dirroot
ChrootDirectory %h/public_html
用于连线的是FileZilla。
设置私钥
不是逐个账户注册,而是批量注册。
-
- 从FileZilla FTP客户端的菜单栏中选择“编辑”→“设置”。
-
- 在设置对话框的左侧菜单中选择“连接”→“SFTP”。
-
- 点击设置对话框中间下方的“添加密钥文件”按钮,并注册您的私钥文件。
只能使用未受密码保护的PuTTY格式密钥。
如果尝试注册其他格式的密钥,将会被要求进行密钥转换。
FileZilla无法解析bcrypt KDF格式的密钥,也无法进行转换。
即使一个账号拥有多个密钥,也需要全部在此处添加。
连接设置
-
- 在FileZilla FTP客户端的菜单栏中,选择文件▸站点管理器。
-
- 点击左下方的“新建站点”按钮,并为其命名。
-
- 在主机栏中输入服务器的IP地址,在端口栏中输入OpenSSH所使用的端口号。
-
- 在协议的下拉菜单中选择“SFTP-SSH文件传输协议”。
-
- 在登录类型的下拉菜单中选择“普通”。
-
- 在用户栏中填写Linux的用户名,密码留空。
-
- 如果需要的话,填写“详细”标签中的“默认本地目录”和“默认远程目录”,并勾选“使用同步浏览”。
“默认远程目录”应为相对于SFTP根目录的绝对路径。
“连接”
参考网页和脚注
如果与客户端通信中,则强制断开连接。
由于未经过systemctl命令,因此在使用systemctl status httpd命令显示状态时,Active栏将显示为failed (Result: exit-code)。
不强制终止与客户端通信中的子进程,而是按照通信结束的进程顺序停止。
不强制终止与客户端通信中的子进程,而是逐步将其替换为新进程。
普通用户没有/var/log/httpd文件夹以下的读取权限,因此需要root权限才能读取错误日志和访问日志。
在初始设置中,可能将TLS连接的访问日志记录在/var/log/httpd/ssl_access_log中。
nano(日语)- ArchWiki
修改nano编辑器的配置文件nanorc | Linux战记
MySQL 5.5.3-m3, 5.5.4-m3同时发布 – sakaik的日常感受~(T)编
mysql选项 | mysql命令行客户端 – MariaDB知识库
在尝试登录时,请注意符号和加密方式的兼容性,以避免身份验证失败。
以OpenSSH格式保存。