新規記事の投稿を行うことで、非表示にすることが可能です。
2020年05月01日
php-fpmのインストール(sockets通信)
ルートユーザに変更する
$ sudo su -
yumのアップデート
$ yum update
EPELリポジトリの追加
$ yum -y install epel-release
remiリポジトリの追加
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
使いそうなものをインストールしておく
vimのインストール (ついvimって打ってしまうので入れとく)
$ yum -y install vim
wgetのインストール (wordpressインストールとかに使う)
$ yum -y install wget
自動アップデートの設定
$ yum install yum-cron
$ vim /etc/yum/yum-cron.conf
Vagrantfile内のapply_updatesをnoからyesに修正する
# Whether updates should be applied when they are available. Note
# that download_updates must also be yes for the update to be applied.
apply_updates = yes
nginxの状態確認(起動前)
$ systemctl status nginx
Active: inactive (dead)
nginxの起動
$ systemctl start nginx
nginxの状態確認(起動後)
$ systemctl status nginx
Active: active (running)
サーバ再起動時にnginxが自動起動するように設定するようにする
$ systemctl enable nginx.service
ブラウザでアクセス(vagrantfileでコメントアウトを外したip)
http://192.168.33.10/
welcome to nginx!
nginxの設定
$ vim /etc/nginx/conf.d/default.conf
以下を編集する
(〜.confのファイルが読み込まれるらしい。本やブログごとに名前が違って紛らわしいが、だぶんそういうこと。)
server {
listen 80;
server_name localhost;
root /var/www/html/;
charset UTF-8;
access_log /var/log/nginx/sample.com.access.log main;
error_log /var/log/nginx/sample.com.error.log;
location / {
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginxの再起動
$ systemctl restart nginx
phpとphp-fpmのインストール
$ yum -y install --enablerepo=epel,remi,remi-php74 php php-mbstring php-pdo php-mysqlnd php-fpm
PHPのバージョン確認
$ php -v
PHP 7.4.5
php-confの設定を行う
$ vim /etc/php-fpm.d/www.conf
それぞれの行を修正する
修正前
listen = 127.0.0.1:9000
修正後
listen = /var/run/php-fpm/php-fpm.sock
修正前
user = apache
修正後
user = nginx
修正前
group = apache
修正後
group = nginx
修正前
;listen.owner = nobody
;listen.group = nobody
修正後
listen.owner = nginx
listen.group = nginx
php-fpmの起動
$ systemctl start php-fpm
php-fpmの状態確認
$ systemctl status php-fpm
サーバ再起動時にphp-fpmが自動起動するように設定するようにする
$ systemctl enable php-fpm
とりあえず、phpが使えるか確認してみる。
$ cd /var/www/html
$ vim index.php
みんな大好き hello world! 以下を記述して保存する。
$ sudo su -
yumのアップデート
$ yum update
EPELリポジトリの追加
$ yum -y install epel-release
remiリポジトリの追加
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
使いそうなものをインストールしておく
vimのインストール (ついvimって打ってしまうので入れとく)
$ yum -y install vim
wgetのインストール (wordpressインストールとかに使う)
$ yum -y install wget
自動アップデートの設定
$ yum install yum-cron
$ vim /etc/yum/yum-cron.conf
Vagrantfile内のapply_updatesをnoからyesに修正する
# Whether updates should be applied when they are available. Note
# that download_updates must also be yes for the update to be applied.
apply_updates = yes
nginxの状態確認(起動前)
$ systemctl status nginx
Active: inactive (dead)
nginxの起動
$ systemctl start nginx
nginxの状態確認(起動後)
$ systemctl status nginx
Active: active (running)
サーバ再起動時にnginxが自動起動するように設定するようにする
$ systemctl enable nginx.service
ブラウザでアクセス(vagrantfileでコメントアウトを外したip)
http://192.168.33.10/
welcome to nginx!
nginxの設定
$ vim /etc/nginx/conf.d/default.conf
以下を編集する
(〜.confのファイルが読み込まれるらしい。本やブログごとに名前が違って紛らわしいが、だぶんそういうこと。)
server {
listen 80;
server_name localhost;
root /var/www/html/;
charset UTF-8;
access_log /var/log/nginx/sample.com.access.log main;
error_log /var/log/nginx/sample.com.error.log;
location / {
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginxの再起動
$ systemctl restart nginx
phpとphp-fpmのインストール
$ yum -y install --enablerepo=epel,remi,remi-php74 php php-mbstring php-pdo php-mysqlnd php-fpm
PHPのバージョン確認
$ php -v
PHP 7.4.5
php-confの設定を行う
$ vim /etc/php-fpm.d/www.conf
それぞれの行を修正する
修正前
listen = 127.0.0.1:9000
修正後
listen = /var/run/php-fpm/php-fpm.sock
修正前
user = apache
修正後
user = nginx
修正前
group = apache
修正後
group = nginx
修正前
;listen.owner = nobody
;listen.group = nobody
修正後
listen.owner = nginx
listen.group = nginx
php-fpmの起動
$ systemctl start php-fpm
php-fpmの状態確認
$ systemctl status php-fpm
サーバ再起動時にphp-fpmが自動起動するように設定するようにする
$ systemctl enable php-fpm
とりあえず、phpが使えるか確認してみる。
$ cd /var/www/html
$ vim index.php
みんな大好き hello world! 以下を記述して保存する。
新規ユーザーでのssh鍵認証 ssh -i 秘密鍵 username@サーバ(192.168.33.10 )
vagrant sshでログインし下記コマンドでrootにスイッチ
sudo su -
パスワード認証をYesにする
[root@sv11-centos74 ~]# vi /etc/ssh/sshd_config
PasswordAuthentication yes
[root@sv11-centos74 ~]# systemctl restart sshd
サーバからログアウトする。
ローカルPCで秘密鍵と公開鍵を設定する
ssh-keygen -t rsa -b 4096 -C "hoge@example.com" -f ~/.ssh/username_id_rsa
パスワードを使用して新規作成ユーザでログイン
ssh sampleuser@192.168.33.10
sshフォルダを作成
[vagrant@localhost ~]$ mkdir .ssh
[vagrant@localhost ~]$ chmod 700 .ssh
公開鍵を送信
scp /home/[user名]/.ssh/id_rsa.pub [リモートユーザー]@[リモートサーバーのホスト名]:~/.ssh
再度パスワードでログイン
sshフォルダへ移動し、authorized_keyに名前を変更
[vagrant@localhost .ssh]$ mv /id_rsa.pub authorized_keys
権限を変更
[vagrant@localhost .ssh]$ chmod 600 authorized_keys
ログアウトしてsshでログインしてみる
ssh -i id_rsa menta@192.168.33.10
パスワード認証をNoに戻す
[root@sv11-centos74 ~]# vi /etc/ssh/sshd_config
PasswordAuthentication no
[root@sv11-centos74 ~]# systemctl restart sshd
sudo su -
パスワード認証をYesにする
[root@sv11-centos74 ~]# vi /etc/ssh/sshd_config
PasswordAuthentication yes
[root@sv11-centos74 ~]# systemctl restart sshd
サーバからログアウトする。
ローカルPCで秘密鍵と公開鍵を設定する
ssh-keygen -t rsa -b 4096 -C "hoge@example.com" -f ~/.ssh/username_id_rsa
パスワードを使用して新規作成ユーザでログイン
ssh sampleuser@192.168.33.10
sshフォルダを作成
[vagrant@localhost ~]$ mkdir .ssh
[vagrant@localhost ~]$ chmod 700 .ssh
公開鍵を送信
scp /home/[user名]/.ssh/id_rsa.pub [リモートユーザー]@[リモートサーバーのホスト名]:~/.ssh
再度パスワードでログイン
sshフォルダへ移動し、authorized_keyに名前を変更
[vagrant@localhost .ssh]$ mv /id_rsa.pub authorized_keys
権限を変更
[vagrant@localhost .ssh]$ chmod 600 authorized_keys
ログアウトしてsshでログインしてみる
ssh -i id_rsa menta@192.168.33.10
パスワード認証をNoに戻す
[root@sv11-centos74 ~]# vi /etc/ssh/sshd_config
PasswordAuthentication no
[root@sv11-centos74 ~]# systemctl restart sshd
Linuxにユーザを追加する
ユーザを追加
useradd sampleuser
パスワードを設定
passwd sampleuser
sudo実行を許可するためにグループ追加
usermod -aG wheel sampleuser
参考URL
https://qiita.com/Esfahan/items/a159753d156d23baf180
useradd sampleuser
パスワードを設定
passwd sampleuser
sudo実行を許可するためにグループ追加
usermod -aG wheel sampleuser
参考URL
https://qiita.com/Esfahan/items/a159753d156d23baf180
CentOS7にcronを設定
以下の形でインストールしたイメージにはCronは初期から入っている
vagrant init CentOS/7
ルートユーザになり
sudo su -
以下のコマンドでcron起動
systemctl start crond.service
再起動のコマンド。
systemctl restart crond.service
自動起動の設定
systemctl enable crond.service
cron設定したユーザにて以下のコマンドを実行
crontab -e
* * * * * [実行したいファイルパス]
分 時 日 月 曜日の順に設定する
対象ファイルには実行権限を付与すること
chmod +x [ファイルパス]
例
chmod +x /var/backup/sh/mysql-backup.sh
権限が付与されていることを確認する
ls -al /var/backup/sh/mysql-backup.sh
xがついてればOK
-rwxr-xr-x. 1 root root 951 May 1 12:41 /var/backup/sh/mysql-backup.sh
参考
https://time4vps.ysklog.net/cron.html
vagrant init CentOS/7
ルートユーザになり
sudo su -
以下のコマンドでcron起動
systemctl start crond.service
再起動のコマンド。
systemctl restart crond.service
自動起動の設定
systemctl enable crond.service
cron設定したユーザにて以下のコマンドを実行
crontab -e
* * * * * [実行したいファイルパス]
分 時 日 月 曜日の順に設定する
対象ファイルには実行権限を付与すること
chmod +x [ファイルパス]
例
chmod +x /var/backup/sh/mysql-backup.sh
権限が付与されていることを確認する
ls -al /var/backup/sh/mysql-backup.sh
xがついてればOK
-rwxr-xr-x. 1 root root 951 May 1 12:41 /var/backup/sh/mysql-backup.sh
参考
https://time4vps.ysklog.net/cron.html
nginxを監視するシェルスクリプト
$ vim process_check.sh
#! /bin/bash
#監視するプロセス名を定義する
PROCESS_NAME=nginx
#監視するプロセスが何個起動しているかカウントする
count=`pgrep $PROCESS_NAME | wc -l`
# pgrep プロセスIDを表示
# wc -l 行数を取得
#監視するプロセスが0個場合に、処理を分岐する
if [ $count = 0 ]; then
#0個場合は、サービスが停止しているので起動する
echo "$PROCESS_NAME Down"
echo "$PROCESS_NAME Start"
systemctl start nginx
else
echo "$PROCESS_NAME OK"
fi
$ sudo chmod +x process_check.sh
#! /bin/bash
#監視するプロセス名を定義する
PROCESS_NAME=nginx
#監視するプロセスが何個起動しているかカウントする
count=`pgrep $PROCESS_NAME | wc -l`
# pgrep プロセスIDを表示
# wc -l 行数を取得
#監視するプロセスが0個場合に、処理を分岐する
if [ $count = 0 ]; then
#0個場合は、サービスが停止しているので起動する
echo "$PROCESS_NAME Down"
echo "$PROCESS_NAME Start"
systemctl start nginx
else
echo "$PROCESS_NAME OK"
fi
$ sudo chmod +x process_check.sh
vagrantでCentOS7とnginx環境を構築する
以下のコマンドを入力しvagrant環境を構築
mkdir -p ~/vagrant/
cd ~/vagrant/
vagrant box add centos/7
vagrant init centos/7
vim Vagrantfile
config.vm.network "private_network", ip: "192.168.33.10" #追加
vagrant up
vagrantに接続しnginxをインストール
vagrant ssh
rootユーザになる
sudo su -
vi /etc/yum.repos.d/nginx.repo
nginx.reposを作成
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
コピペ元
https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
nginxインストール
sudo yum install nginx
Is this ok [y/d/N]: y
nginx起動
sudo systemctl start nginx
ブラウザに192.168.33.10でアクセス
open http://192.168.33.10/index.html
mkdir -p ~/vagrant/
cd ~/vagrant/
vagrant box add centos/7
vagrant init centos/7
vim Vagrantfile
config.vm.network "private_network", ip: "192.168.33.10" #追加
vagrant up
vagrantに接続しnginxをインストール
vagrant ssh
rootユーザになる
sudo su -
vi /etc/yum.repos.d/nginx.repo
nginx.reposを作成
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
コピペ元
https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
nginxインストール
sudo yum install nginx
Is this ok [y/d/N]: y
nginx起動
sudo systemctl start nginx
ブラウザに192.168.33.10でアクセス
open http://192.168.33.10/index.html