34
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7+PHP7+Nginx+MariaDB+Redisの環境を作る方法

Last updated at Posted at 2016-03-25

パッケージのアップデート

sudo yum update

epel,remiレポジトリの登録

sudo yum -y install epel-release
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo rpm -ivh remi-release-7.rpm

vim,gitをインストール

sudo yum install git vim

タイムゾーンを東京に設定

sudo timedatectl set-timezone Asia/Tokyo

サーバ名をexample.comに変更する

example.com

Nginxインストール

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

Nginxの設定

設定はいい感じで適当に

/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  example.com;

    access_log  /var/log/nginx/laos.access.log;
    error_log  /var/log/nginx/laos.error.log warn;

    root   /vagrant/laos/webroot;
    index  index.html index.php;

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

    location ~ \.php$ {
        try_files      $uri =404;
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
~

最低限に良さ気な設定

/etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;
worker_rlimit_nofile 100000;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  2048;
    multi_accept on;
    use epoll;
}


http {
    server_tokens off;
    include       /etc/nginx/mime.types;
    default_type  text/html;
    charset UTF-8;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile       off;
    tcp_nopush     on;
    tcp_nodelay    on;


    keepalive_timeout 10;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;

    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 100;

    gzip on;
    gzip_http_version 1.0;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
    open_file_cache off;

    client_max_body_size 20m;
    server_names_hash_bucket_size 64;
    include /etc/nginx/conf.d/*.conf;
}

Nginx起動

sudo systemctl start nginx
sudo systemctl enable nginx

PHP7をインストール

必要そうなパッケージをインストール

sudo yum --enablerepo=epel,remi,remi-php70 install php70 php70-php-mcrypt php70-php-mbstring php70-php-fpm php70-php-gd php70-php-pecl-xdebug php70-php-pecl-redis php70-php-pecl-imagick-devel php70-php-pecl-imagick php70-php-mysqlnd php70-php-intl php70-php-bcmath php70-php-pecl-zip php70-php-xmlrpc php70-php-xml  php70-php-pecl-http php70-php-pecl-http-devel php70-php-opcache

PHPのパスを通す

cat /opt/remi/php70/enable
export PATH=/opt/remi/php70/root/usr/bin:/opt/remi/php70/root/usr/sbin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/opt/remi/php70/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export MANPATH=/opt/remi/php70/root/usr/share/man:${MANPATH}
~/.bashrc
export PATH=/opt/remi/php70/root/usr/bin:/opt/remi/php70/root/usr/sbin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/opt/remi/php70/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export MANPATH=/opt/remi/php70/root/usr/share/man:${MANPATH}
source ~/.bashrc
確認
php -v
PHP 7.0.4 (cli) (built: Mar  2 2016 17:13:39) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

php-fpmの設定

/etc/opt/remi/php70/php-fpm.d/www.conf
user = nginx
group = nginx
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

php-fpm起動

/etc/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/var/run/php-fpm.pid
ExecStart=/opt/remi/php70/root/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/opt/remi/php70/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID


[Install]
WantedBy=multi-user.target
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

ファイアウォールの設定でhttpアクセスできるようにする

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

MariaDBのインストール

公式からサイトからリポジトリを選択する
https://downloads.mariadb.org/mariadb/repositories/

今回はCentOS7なので以下のように設定

/etc/yum.repos.d/mariadb.repo
# MariaDB 10.1 CentOS repository list - created 2016-03-24 14:55 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

MariaDBのインストール

sudo yum install MariaDB-server MariaDB-client MariaDB-devel 

設定する

sudo mkdir /home/mysql
sudo chown mysql. /home/mysql
/etc/my.cnf.d/server.cnf
[mysqld]
character-set-server=utf8
plugin-load = handlersocket.so
log-bin=mysql-bin
expire_logs_days = 30
secure_file_priv=/home/mysql

起動

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

初回設定

mysql_secure_installation

接続

mysql -u root -p
CREATE SCHEMA `laos` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

Redisのインストール

wget http://download.redis.io/releases/redis-stable.tar.gz
tar zxvf redis-stable.tar.gz
cd redis-stable
sudo make
sudo make install

Redisの設定ファイルを作成

sudo mkdir /etc/redis
sudo cp redis.conf /etc/redis/
/etc/redis/redis.conf
daemonize yes
logfile "/var/log/redis.log"

Redis起動設定

/etc/systemd/system/redis.service
[Unit]
Description=Load redis daemon.

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
sudo systemctl start redis
sudo systemctl enable redis

composerインストール

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

CakePHP3をインストール

cd /vagrant
composer create-project --prefer-dist cakephp/app laos
laos/config/app.php
    'Asset' => [
        'timestamp' => true,
    ],
.
.
.    
    'Cache' => [
        'default' => [
            'className' => 'Redis',
            'path' => CACHE,
        ],


        '_cake_core_' => [
            'className' => 'Redis',
            'prefix' => 'myapp_cake_core_',
            'path' => CACHE . 'persistent/',
            'serialize' => true,
            'duration' => '+2 minutes',
            'url' => env('CACHE_CAKECORE_URL', null),
        ],

        '_cake_model_' => [
            'className' => 'Redis',
            'prefix' => 'myapp_cake_model_',
            'path' => CACHE . 'models/',
            'serialize' => true,
            'duration' => '+2 minutes',
            'url' => env('CACHE_CAKEMODEL_URL', null),
        ],
    ],
.
.
.
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => '127.0.0.1',
            //'port' => 'non_standard_port_number',
            'username' => 'laos',
            'password' => 'YOURPASSWORD',
            'database' => 'laos',
            'encoding' => 'utf8',
            'timezone' => 'Asia/Tokyo',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
.
.
.            
            
    'Session' => [
        'defaults' => 'cache',
    ],

タイムゾーン設定

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p

アクセス

34
43
2

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
34
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?