创建 CentOS7+PHP7+Nginx+MariaDB+Redis 的环境的方法

更新包。

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的配置

設定很好,隨意地合適。

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;
    }
}
~

最低限看起来还不错的设定

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}
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的配置

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

启动php-fpm。


[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系统,所以请按照以下方式进行配置。

# 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
[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

初始设置 (Chū huí shè

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/
daemonize yes
logfile "/var/log/redis.log"

Redis启动配置

[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
    '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

访问

广告
将在 10 秒后关闭
bannerAds