使用Vagrant来搭建CentOS7的LAMP环境
这是我的备忘录,用于使用Vagrant+VirtualBox构建LAMP环境。
我正在参考以下网站:Vagrant+VirtualBox上搭建LAMP环境。
大致相同,但使用的是CentOS7.5上的标准MariaDB而不是MySQL,并进行了一些小的添加和修改。
关于环境
我假设以下的环境。
ホスト
OS:Windows7 64bit
VirtualBox 5.2.22 (2018/11/26時点 最新)
Vagrant 2.2.1 (2018/11/26時点 最新)
ゲストOS
CentOS7.5
MariaDB 5.5.6
Apache 2.4.6
安装VirtualBox
安装已完成。由于通常情况下基本不需要操作VirtualBox,因为是通过Vagrant来操作VirtualBox,所以请取消勾选”安装完成后启动Oracle VM VirtualBox 5.2.22″,然后点击”完成”以完成安装。
安装Vagrant
由于有时候不重新启动可能无法正常运行,请在重新启动后继续进行以下操作。
为Vagrant创建一个文件夹。
为Vagrant创建一个文件夹。位置和名称可随意,但我认为最好不要使用太多日语。
C:\Users\ユーザ名>mkdir c:\vagrant
C:\Users\ユーザ名>cd /d c:\vagrant
c:\vagrant>
使用上述命令转移到Vagrant文件夹中,然后创建Vagrantfile。
创建Vagrant文件
c:\vagrant>vagrant init "bento/centos-7.5"
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
c:\vagrant>
现在,有了这个Vagrantfile,我们可以开始编辑它了。
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10" ←コメントになっているので#を外す
现在,Vagrantfile的编辑完成了,我们可以启动Vagrant了。
c:\vagrant>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'bento/centos-7.5' could not be found. Attempting to find and i
nstall...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'bento/centos-7.5'
default: URL: https://vagrantcloud.com/bento/centos-7.5
==> default: Adding box 'bento/centos-7.5' (v201808.24.0) for provider: virtualb
ox
default: Downloading: https://vagrantcloud.com/bento/boxes/centos-7.5/versio
ns/201808.24.0/providers/virtualbox.box
default: Download redirected to host: vagrantcloud-files-production.s3.amazo
naws.com
default: Progress: 59% (Rate: 702k/s, Estimated time remaining: 0:03:32))
・・・(中略)・・・
c:\vagrant>
最开始需要下载虚拟机镜像,可能会花费一些时间。
等待下载完成并启动。
连接到虚拟机
当命令提示符返回时,我们将通过SSH连接到启动的虚拟机上。
c:\vagrant>vagrant ssh
[vagrant@localhost ~]$ ←コマンドプロンプトが変わって仮想PCに接続した事が分かる
我们将继续建立LAMP环境。
添加存储库
为了安装PHP7.2,在CentOS7上需要添加软件仓库,因为默认安装的是PHP5.x版本。
添加epel软件包
[vagrant@localhost ~]$ sudo yum -y install epel-release.noarch
Loaded plugins: fastestmirror
・・・(中略)・・・
Installed:
epel-release.noarch 0:7-11
Complete!
[vagrant@localhost ~]$
为了始终禁用epel,需要编辑/etc/yum.repos.d/epel.repo的内容。
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=0 ←enabled=1を0に変更して保存
安装remi包
[vagrant@localhost ~]$ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Retrieving http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
warning: /var/tmp/rpm-tmp.DK8gr1: Header V4 DSA/SHA1 Signature, key ID 00f97f56:
NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:remi-release-7.5-2.el7.remi ################################# [100%]
[vagrant@localhost ~]$
同样地,编辑/etc/yum.repos.d/remi.repo以确保它不会始终处于有效状态。
[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror
enabled=0 ←enabled=1を0に変更して保存
安装Apache
[vagrant@localhost ~]$ sudo yum -y install httpd
・・・(中略)・・・
Installed:
httpd.x86_64 0:2.4.6-80.el7.centos.1
Dependency Installed:
apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7
Complete!
[vagrant@localhost ~]$
Apache自动启动设置
[vagrant@localhost ~]$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[vagrant@localhost ~]$
通过这样设置,下次启动时httpd将自动启动。
Apache的启动
[vagrant@localhost ~]$ sudo systemctl start httpd
如果需要手动启动,则使用上述命令。同样地,如果要手动关闭,则使用以下命令。
[vagrant@localhost ~]$ sudo systemctl stop httpd
安装MariaDB。
[vagrant@localhost ~]$ sudo yum -y install mariadb-server mariadb-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
・・・(中略)・・・
Complete!
[vagrant@localhost ~]$
请确认MariaDB的版本
[vagrant@localhost ~]$ mysql --version
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
MariaDB的設定
-
- 以下の設定を行います。
文字コードのデフォルトをUTF-8に設定。
デフォルトのデータベースタイプをInnoDBに設定。
InnoDBのデータやインデックスをファイルを分割
デフォルトの照合順序をutf8_generic_ciに設定。
クライアント、コマンドのデフォルト文字コードをUTF-8に設定。
使用vi编辑器编辑/etc/my.cnf.d/server.cnf文件。
[vagrant@localhost ~]$ sudo vi /etc/my.cnf.d/server.cnf
# this is only for the mysqld standalone daemon
[mysqld]
default-storage-engine = InnoDB ←追加
innodb_file_per_table ←追加
character-set-server = utf8 ←追加
collation-server = utf8_general_ci ←追加
[mysql]
default-character-set = utf8 ←追加
[client]
default-character-set = utf8 ←追加
启动MariaDB
[vagrant@localhost ~]$ sudo systemctl start mariadb
[vagrant@localhost ~]$
MariaDB的自动启动设置
[vagrant@localhost ~]$ sudo systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service
to /usr/lib/systemd/system/mariadb.service.
[vagrant@localhost ~]$
MariaDB的初始设置
[vagrant@localhost ~]$ sudo 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): ←rootパスワード入力(空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] ←空Enter入力
New password: ←任意のDB管理者(root)用パスワードを入力
Re-enter new password: ←管理者用パスワード再入力(確認)
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] ←空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] ←空Enter入力
... skipping.
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] ←空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] ←空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!
[vagrant@localhost ~]$
检查MariaDB连接
[vagrant@localhost ~]$ mysql -u root -p
Enter password: ←mariaDB管理者用パスワード入力
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
如果MariaDB的命令提示符出现,则表示连接成功。您可以使用”quit”退出MariaDB提示符。
安装PHP7.2
由于需要mcrypt,因此请先安装mcrypt。
[vagrant@localhost ~]$ sudo yum -y --enablerepo=epel install libmcrypt
・・・(中略)・・・
Complete!
[vagrant@localhost ~]$
接下来,我们将安装PHP7.2。
[vagrant@localhost ~]$ sudo yum -y --enablerepo=epel,remi,remi-php72 install php
・・・(中略)・・・
Complete!
[vagrant@localhost ~]$
请确认PHP版本
[vagrant@localhost ~]$ php -v
PHP 7.2.12 (cli) (built: Nov 6 2018 16:40:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies[vagrant@localhost ~]$
php.ini的配置文件
为了谨慎起见,可能要考虑做个备份。
[vagrant@localhost ~]$ cp -p /etc/php.ini /etc/php.ini.org
我們將設置php.ini。由於每個設定項目之間的行距離比較遠,所以請隨時進行相應的搜索。
另外,由於「←」後面的文字是註解,所以不需要輸入。
[vagrant@localhost ~]$ sudo vi /etc/php.ini
expose_php = Off ←OnからOffに変更(PHPのバージョンを非表示にする)
post_max_size = 20M ← POSTデータの最大サイズを8M→20Mに変更
upload_max_filesize = 20M ←アップロードの最大サイズも20Mに変更
date.timezone = "Asia/Tokyo" ←タイムゾーンの設定を東京に設定
;マルチバイト文字列の設定(各設定は行頭に;が入ってコメントになっているので外す)
mbstring.language = Japanese ←マルチバイト処理で使用される言語のデフォルトを日本語に設定
mbstgring.internal_encoding = UTF-8 ←内部文字のエンコードをUTF-8に設定。
mbstring.http_input = UTF-8 ←HTTP通信時の入力文字コードをUTF-8に設定
mbstring.http_output = pass ←HTTP出力文字コードの自動変換を行わない
mbstring.encoding_translation = On ←文字コードの自動変換を有効にする(文字化けなどが発生した場合はOffに戻す)
mbstring.detect_order = auto ←文字コード自動検出の優先順位を自動にする。自動にした場合UTF-8が最初に選択される。
mbstring.substitute_character = none ←コードとして変換出来ない文字がある場合に、代替の文字を出力しないようにする。
如果Apache httpd已经在运行,那么我们需要重新启动它以应用PHP的配置设置。
[vagrant@localhost ~]$ sudo systemctl restart httpd
确认HTTPD操作
[vagrant@localhost ~]$ sudo vi /var/www/html/index.php
<?php phpinfo(); ?> ←1行目に左記を追加して保存
[vagrant@localhost ~]$
根据在VagrantFile编辑的修改,虚拟机的IP地址为192.168.33.10。
当httpd正在运行时,尝试从浏览器输入上述IP地址以访问文件。如果出现以下类似的画面,那就表示OK。
不过以上是基本的内容,如果能进行VSCode + xdebug的设置并进行调试,将会更加方便。关于这方面,其他人也已经写了很多,所以建议参考他们的内容。(原本只是为了便于备忘而已…)
ご参考記事
VisualStudioCode + Vagrant + XdebugでPHPをリモートデバッグ
VisualStudioCodeのデバッグでどうしてもブレークで止まらない場合 (PHP Xdebug)
VS CodeとXdebugでPHPのデバッグするのにえらく苦労した件