LoginSignup
0
0

More than 1 year has passed since last update.

vagrant centos7.7 備忘録

Last updated at Posted at 2022-09-06

centos7.7 環境構築

使用するツール

vagrant
virtualbox

使用するbox

bento/centos7.7

インストール準備

以下のコマンドを実行する。
実行後エラーが表示される場合があるのでkernelを最新版に更新する。

linux
vagrant init bento/centos-7.7

起動準備

Vagrantfileのnetwork部分のコメントアウトを消す。

プロビジョニング
# config.vm.network "private_network", ip: "192.168.33.10"
↓
config.vm.network "private_network", ip: "192.168.33.10"

vagrantを起動する。

コマンド
vagrant up

エラー内容

vagrantの共有フォルダがマウントできないというエラーが出るがssh接続はできるので
ターミナルからkernelのアップデートを行う。
No package kernel-devel-3.10.0-1127.el7.x86_64 available.
Error: Nothing to do

kernelアップデート

コマンド
sudo yum -y update kernel

アップデート後再度vagrant upする。(もしかしたらvagrant upだけでも良いかも)

コマンド
vagrant reload --provision

ターミナルでの作業

ここからはrootユーザーで諸々のセットアップをしていく。まずユーザー変更

putty
sudo su -

まず日本語環境へ変更する。

localeコマンドで現在の状態を確認する。
usになっているはずなので↓を実行後、再起動してdateで確認をする。

putty
localectl set-locale LANG=ja_JP.UTF-8

タイムゾーンの設定変更

確認後タイムゾーンをAsia/Tokyoへ変更 、変更後dateで状態を確認する。

putty
timedatectl set-timezone Asia/Tokyo

selinuxの状態確認>停止まで

disabledになっていなければファイルを編集して停止する。

putty
getenforce

↓のファイルを編集する。

putty
vi /etc/selinux/config

SELINUX=enforcing → SELINUX=disabled

編集後に再起動してgetenforceで状態確認Disabledが出ればOK

bash-completion パッケージの導入

systemctlをtabで補完できるようにパッケージをインストールする。

コマンド
yum -y install bash-completion

インストール後パッケージの確認

コマンド
yum list installed | grep bash-completion

firewalldサービスの無効化

iptablesに入れ替える前に設定を確認して動いているようであれば止める。

systemctl list-unit-files | grep firewall

止める場合はこちら

systemctl disable firewalld.service

他初期インストールパッケージを最新にする。不要なsqlは削除

yum list installed | grep mariadb-libs
yum -y remove mariadb-libs mysql*
yum -y update
yum -y install git vim wget epel-release dkms httpd zip unzip

DKMSのインストール

VirtualBoxでLinuxを運用する際の推奨として、DKMS (Dynamic Kernel Module Support) のインストールしたらいいらしいので、インストール。
拡張リリースパッケージEPELをインストールし、そのリポジトリを使って導入。

EPELがあるか確認

コマンド
yum repolist all | grep -i epel

できる限り標準のリポジトリからパッケージを使い、EPELを指定した時しか使用したくない方針とする場合

viコマンドで、enabled=1をenapled=0に書き換え

コマンド
vi /etc/yum.repos.d/epel.repo
enabled=1 → enabled=0

Remiリポジトリをインストール

コマンド
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

念の為設定ファイルも確認(enabled=0になっていればOK)
vi /etc/yum.repos.d/remi.repo

Apache

起動と自動起動と起動チェック

コマンド
systemctl start httpd.service
systemctl enable httpd.service
service httpd status

Apacheの文法チェック

コマンド
apachectl configtest

Apacheの設定調整

  1. バックアップ
コマンド
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk
  1. vimで編集(vimの方がわかりやすい)
コマンド
vim /etc/httpd/conf/httpd.conf
  1. phpが使えるように以下2行を追加
コマンド
※AddTypeタイプで検索(検索は「/〇〇」)
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
  1. cssが読み込めるようにする。
コマンド
AllowOverride None → AllowOverride All
  1. バーチャルホストの設定
コマンド
EnableSendfile on → EnableSendfile off
  1. 共有フォルダへのシンボリックリンク設定
    初期のディレクトリを削除する。
コマンド
rm -rf /var/www/html

共有のvagrantへシンボリックリンクを貼る。

コマンド
ln -fs /vagrant /var/www/html

Laravelを作るときはnode.js使うときにエラーになるのでLaravel環境は別途構築する必要がある。
※Laravel環境はxamppを使った…

  1. バーチャルホストの設定
コマンド
sudo vim /etc/httpd/conf.d/virtualhost-00.conf
vim
<VirtualHost *:80>
  ServerName 192.168.33.10
  DocumentRoot /var/www/html/フォルダ名
</VirtualHost>
  1. 個別の設定ファイルを持っておく。(各ディレクトリごとのDNSはバーチャルホストとhostファイルで管理)
コマンド
sudo vim /etc/httpd/conf.d/virtualhost-〇〇.conf

◆↓の設定を入れる※ServerNameに「_」(アンダースコア)は使用出来ないので注意

vim
   <VirtualHost *:80>
      ServerName hoge.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /var/www/html/hoge
      <Directory /var/www/html/hoge>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options Indexes ExecCGI FollowSymLinks MultiViews
         Order allow,deny
         Allow from all
         AllowOverride all
         # Uncomment this if you're on Apache >= 2.4:
         #Require all granted
      </Directory>
   </VirtualHost>

各設定が完了してから

コマンド
apachectl configtest
service httpd configtest
systemctl restart httpd.service
service httpd status

mysql8のインストール

  1. リポジトリを追加
コマンド
yumリポジトリをインストール
yum -y localinstall http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  1. リポジトリの追加が終わったらインストール可能なバージョンを確認する。
コマンド
yum info mysql-community-server

↓の結果出ればOK

コマンド結果
------------------------------------------
利用可能なパッケージ
名前                : mysql-community-server
アーキテクチャー    : x86_64
バージョン          : 8.0.28
リリース            : 1.el7
容量                : 451 M
------------------------------------------
  1. MySqlをインストール
コマンド
yum -y install mysql-community-server
  1. 公開鍵がインストールされていません。と怒られてしまったら↓を実行して、もう一度インストールする「yum -y install mysql-community-server」
コマンド
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  1. バージョンの確認
コマンド
mysqld --version

成功すると↓が出る。

コマンド
/usr/sbin/mysqld  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
  1. 初期設定
    設定をバックアップ
コマンド
cp /etc/my.cnf /etc/my.cnf.org
  1. 認証プラグインの設定
    vim /etc/my.cnfでcnfファイルを開いて以下を追加。
コマンド
default_authentication_plugin=mysql_native_password
  1. MySQL起動
コマンド
systemctl start mysqld
  1. 状態確認
コマンド
systemctl status mysqld

以下のようになっていればOK

コマンド
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 土 2022-04-02 09:09:46 JST; 30s ago

10.自動起動の設定

コマンド
systemctl enable mysqld

11.初回のパスワードについて
mysqlを初回に起動すると一時パスワードがlogに吐き出されので以下のコマンドを打って出てきたパスをメモとして残しておく。

コマンド
grep password /var/log/mysqld.log

12.パスワードの更新と初期設定
mysql_secure_installationで初期設定をする。先程吐き出されたlogから変更する。

13.ログインと状態確認をする。

(ログイン)mysql -u root -p
(状態確認)status

↓の結果が帰ってくればまぁ大丈夫。

コマンド
mysql  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          11
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.28 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:            /var/lib/mysql/mysql.sock
Binary data as:         Hexadecimal
Uptime:                 7 min 47 sec

Threads: 2  Questions: 16  Slow queries: 0  Opens: 133  Flush tables: 3  Open tables: 49  Queries per second avg: 0.034

文字コードの確認

コマンド
show variables like '%char%';

↓の結果が帰ってくればまぁ大丈夫。

コマンド
mysql> show variables like '%char%';
+--------------------------------------+--------------------------------+
| Variable_name                        | Value                          |
+--------------------------------------+--------------------------------+
| character_set_client                 | utf8mb4                        |
| character_set_connection             | utf8mb4                        |
| character_set_database               | utf8mb4                        |
| character_set_filesystem             | binary                         |
| character_set_results                | utf8mb4                        |
| character_set_server                 | utf8mb4                        |
| character_set_system                 | utf8mb3                        |
| character_sets_dir                   | /usr/share/mysql-8.0/charsets/ |
| validate_password.special_char_count | 1                              |
+--------------------------------------+--------------------------------+
9 rows in set (0.01 sec)

万が一mysqlのパスワードを忘れてしまった場合

コマンド
ALTER USER 'root'@'localhost' IDENTIFIED BY 'SxB&+ExpZ-(5';

この後必要なnode.jpをインストールする。(参考ページ執筆時点の最新版とのこと)

1.リポジトリ追加

コマンド
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -

2.インストール

コマンド
yum install -y nodejs

3.確認

コマンド
node -v
----------------
v16.14.2
----------------
コマンド
npm -v
----------------
8.5.0
----------------

phpのインストール

1.必要なパッケージを落とす。

コマンド
yum -y install --enablerepo=epel,remi,remi-php73 php php-mbstring php-xml php-xmlrpc php-gd php-pdo php-pecl-mcrypt php-mysqlnd php-pecl-mysql phpmyadmin

2.上記がダウンロードできたらバージョンの確認

コマンド
php -v

3.php.iniファイルの編集

コマンド
vim /etc/php.ini

4.変更点

memory_limit = 512M
post_max_size = 100M
upload_max_filesize = 100M
date.timezone = "Asia/Tokyo"
display_errors = On
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.encoding_translation = On
mbstring.http_output = pass
mbstring.detect_order = auto

5.phpmyadminの設定
vim /etc/httpd/conf.d/phpMyAdmin.conf

6.phpmyadminconfの調整

コマンド
★phpmyadmin設定ファイル
# phpMyAdmin - Web based MySQL browser written in php
#
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip ::1
       Require ip 192.168.33.10
       Require all granted
     </RequireAny>
   </IfModule>
</Directory>

<Directory /usr/share/phpMyAdmin/setup/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip ::1
       Require ip 192.168.33.10
       Require all granted
     </RequireAny>
   </IfModule>
</Directory>

# These directories do not require access over HTTP - taken from the original
# phpMyAdmin upstream tarball
#
<Directory /usr/share/phpMyAdmin/libraries/>
    Require all denied
</Directory>

<Directory /usr/share/phpMyAdmin/templates/>
    Require all denied
</Directory>

<Directory /usr/share/phpMyAdmin/setup/lib/>
    Require all denied
</Directory>

<Directory /usr/share/phpMyAdmin/setup/frames/>
    Require all denied
</Directory>

# This configuration prevents mod_security at phpMyAdmin directories from
# filtering SQL etc.  This may break your mod_security implementation.
#
#<IfModule mod_security.c>
#    <Directory /usr/share/phpMyAdmin/>
#        SecRuleInheritance Off
#    </Directory>
#</IfModule>

composerのインストール

1.composerをインストールする。

コマンド
yum -y install --enablerepo=remi,remi-php73 php-pecl-zip composer

2.composerの配置とインストール確認
#CentOSのどこからでも使えるようにするために、/usr/local/binフォルダにcomposer.pharを移動させる

コマンド
mv composer.phar /usr/local/bin/composer

boxのバックアップ

コマンド
vagrant package --output laravelos7.box

●gitの初期設定

コマンド
git config --global user.name "太一 石坂"
git config --global user.email "taiti048235921@gmail.com"

●ssh鍵作成

コマンド
ssh-keygen -t rsa -C "taiti048235921@gmail.com"

●鍵登録
catコマンドで鍵の内容をコピーしてgitに登録する。

コマンド
cat ~/.ssh/id_rsa.pub

★参考ページ
●CentOS 7のインストール後におこなうLinuxの基本設定 13ポイント
https://www.rem-system.com/linux-first-setting/

●EPELに関して
https://qiita.com/yamada-hakase/items/fdf9c276b9cae51b3633

●CentOS7 に最新版の Git をインストールする方法
https://qiita.com/tomy0610/items/66e292f80aa1adc1161d

●vagrantを用いたPHPの環境構築
https://qiita.com/tiwu_dev/items/f135e6b6fbbe3ec6aa54

●Apache 2.4.25 以降でホスト名のアンダースコアで 400 が出る場合の対処方法
https://gotohayato.com/content/148/

●【Apache】httpd.conf ファイルの設定の解説
https://go-journey.club/archives/14030

●CentOS7にMySQL8をインストールして初期パスワードを変更する
https://www.suzu6.net/posts/196-mysql/

●CentOS 7 に Node.js(npm, node) をインストールする手順
https://mekou.com/linux-magazine/centos-7-%E3%81%AB-node-jsnpm-node-%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8B%E6%89%8B%E9%A0%86/

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0