LoginSignup
12
20

More than 5 years have passed since last update.

CentOS7(x86_64)+nginx+PHP7+php-fpm+MySQL5.7環境構築手順

Last updated at Posted at 2017-02-20

前提:CentOS-7-x86_64がインストール済みであること。rootでログインしていること。

●SELinuxを無効化する

vi /etc/selinux/config

下記のように編集する

##SELINUX=enforcing
SELINUX=disabled

再起動する

sudo shutdown -r now

●EPELリポジトリ

yum update -y
yum install epel-release -y
yum -y update epel-release

●Remiリポジトリ

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y update remi-release

●nginxインストール

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y update nginx-release-centos
yum -y --enablerepo=nginx install nginx

●ファイアウォール解除

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

●nginx起動

systemctl start nginx.service
systemctl enable nginx.service

この時点で、ブラウザにてhttpでサーバのIPアドレスにアクセスすれば、
nginxのページ
「Welcome to nginx!」
が表示される。

●/etc/nginx/conf.d の設定

cd /etc/nginx/conf.d
cp -p default.conf default.conf.org
vi default.conf

default.conf に、次のように記述する。

server {
  listen 80;
  server_name localhost;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
  }
}

●PHP7のインストール

yum -y --enablerepo=remi-php70,epel install php-fpm php-gd php-gmp php-mbstring php-mcrypt php-opcache php-pdo php-pear-MDB2-Driver-mysqli php-pecl-memcached php-pecl-msgpack php-xml php-devel php-gd

●php-fpmの設定

cd /etc/php-fpm.d/
cp -p www.conf www.conf.org
vi www.conf

下記のように編集する。

user = nginx
group = nginx
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx

●php-fpmの起動

systemctl start php-fpm
systemctl enable php-fpm
systemctl restart nginx.service

●PHPスクリプトの動作確認

cd /usr/share/nginx/html
vi index.php
<?php echo 'hoge'; ?>

ブラウザにてhttpでサーバのIPアドレスにアクセスして、hogeと画面に表示されたらOK。

●MySQL5.7のインストール

rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
mysqld --version

●MySQL5.7の起動

systemctl enable mysqld.service
systemctl start mysqld.service

●MySQL5.7の設定

vi /etc/my.cnf

skip-grant-tables を追加する。

[mysqld]
skip-grant-tables

mysqldを再起動する。

systemctl restart mysqld.service

●MySQL5.7のrootのパスワードを設定する
パスワードは、最低1つの数値文字を含み、1つの小文字および大文字を含み、1つの特殊文字(英数字以外)を含む必要がある。

mysql -u root
use mysql
UPDATE user SET authentication_string=password('任意のパスワード') WHERE user='root';
flush privileges;
exit

設定を戻す。

vi /etc/my.cnf
skip-grant-tables を削除する。
systemctl restart mysqld.service

パスワード付きで入れることを確認する。

mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '任意のパスワード';

●PHP7とMySQL5.7の接続を確認する

<?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=mysql;charset=utf8','root','MySQLのパスワード',
  array(PDO::ATTR_EMULATE_PREPARES => false));
  $result = $pdo->query('show tables');
  while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    print_r($row);
  }
} catch (PDOException $e) {
  exit('データベース接続失敗。'.$e->getMessage());
}
?>

●gitのインストール

yum install git -y

●Laravel5のインストール
http://qiita.com/kenichiro-yamato/items/1f8e2c80c8067fc51f56

●laravelアプリケーション作成の下準備

yum install php-pear --disablerepo=* --enablerepo=remi,remi-php70
yum -y install cc gcc
yum -y install zlib-devel
pecl install zip

vi /etc/php.ini

つぎのように追記する。

[PHP]
extension=zip.so

nginx再起動

systemctl restart nginx.service

●laravelアプリケーション作成

cd /usr/share/nginx/html/
/root/.config/composer/vendor/bin/laravel new hoge

・nginxのドキュメントルールをlaravelのpublicに合わせる。
・再起動する systemctl restart nginx.service
・laravelのパーミッションを開ける。 chmod -R 777 storage
・ブラウザにてhttpでサーバのIPアドレスにアクセスして、Laravelのトップが表示されたらOK。

12
20
0

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
12
20