环境

  • CentOS
  • Nginx
  • PHP 7+
  • PHP-FPM
  • MariaDB

系统安装

系统:CentOS 7.x

下载Centos Minimal版本

使用VMware 来安装CentOS 系统

Nginx 安装

安装

1
2
3
4
5
6
7
8
9
10
11
# 下载Nginx包,并解压到/root/nginx
# cd 到nginx包(/root/nginx) 目录中
$ cd /root/nginx

# 安装
## 没有权限需要执行授权命令
$ chmod +x ./configure
## 安装配置
$ ./configure
# 执行安装
$ make && make install

Nginx 管理脚本

1
$ vi /etc/init.d/nginx

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

# Author: licess
# website: http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
case "$1" in
start)
echo -n "Starting $NAME... "

if netstat -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi

$NGINX_BIN -c $CONFIGFILE

if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;

stop)
echo -n "Stoping $NAME... "

if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi

$NGINX_BIN -s stop

if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;

status)
if netstat -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;

force-quit)
echo -n "Terminating $NAME... "

if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi

kill `pidof $NAME`

if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;

restart)
$0 stop
sleep 1
$0 start
;;

reload)
echo -n "Reload service $NAME... "

if netstat -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;

configtest)
echo -n "Test $NAME configure files... "

$NGINX_BIN -t
;;

*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;

esac

授权于使用

1
$ chmod a+x /etc/init.d/nginx

使用chkconfig进行管理

为了方便管理,可以将nginx加到chkconfig中进行管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## 添加到chkconfig中
$ chkconfig --add /etc/init.d/nginx
## 启动Nginx
$ service nginx start
## 停止Nginx
$ service nginx stop
## 设置开机启动
$ chkconfig nginx on
```

## PHP 7.2.3

### Nginx PHP集成
> 自PHP-5.3.3起,PHP-FPM加入到了PHP核心,编译时加上--enable-fpm即可提供支持。 PHP-FPM以守护进程在后台运行,Nginx响应请求后,自行处理静态请求,PHP请求则经过fastcgi_pass交由PHP-FPM处理,处理完毕后返回。 Nginx和PHP-FPM的组合,是一种稳定、高效的PHP运行方式,效率要比传统的Apache和mod_php高出不少


### 安装

``` code
## 下载依赖的包
$ yum install -y git gcc gcc-c++ libxml2 libxml2-devel openssl pkgconfig openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp-devel mariadb-devel aspell-devel recode-devel autoconf bison pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel

## 下载PHP源码包
$ cd ~/
$ wget http://am1.php.net/distributions/php-7.2.3.tar.gz
$ tar xfvz php-7.2.3.tar.gz
$ cd php-7.2.3
$ ./configure --with-config-file-path=/usr/local/lib --with-config-file-scan-dir=/usr/local/lib/ext --with-mysql-sock=/dev/shm/mysql.sock --enable-mysqlnd --enable-fpm --disable-ipv6 --enable-mbstring --with-zlib --without-sqlite3 --without-pdo-sqlite --with-openssl --disable-pdo --enable-opcache --enable-sockets --without-pear --disable-phar --with-freetype-dir --with-jpeg-dir --with-png-dir

## 编译安装
$ make && make install

## php.ini
$ cp php-7.2.3/php.ini-development /usr/local/lib/ext/php.ini

PHP-FPM 模块

自PHP-5.3.3起,PHP-FPM加入到了PHP核心,编译时加上–enable-fpm即可提供支持。 PHP-FPM以守护进程在后台运行,Nginx响应请求后,自行处理静态请求,PHP请求则经过fastcgi_pass交由PHP-FPM处理,处理完毕后返回。 Nginx和PHP-FPM的组合,是一种稳定、高效的PHP运行方式,效率要比传统的Apache和mod_php高出不少

关于FPM模块可以查看https://github.com/php/php-src/tree/master/sapi/fpm

对于PHP-FPM管理可以参考源码的脚本https://github.com/php/php-src/blob/master/sapi/fpm/init.d.php-fpm.in

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cp ~/php-7.2.3/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm
$ chmod +x /etc/init.d/php-fpm

## php-fpm脚本使用
$ /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest}

## php-fpm配置

$ cp ~/php-7.2.3/sapi/fpm/php-fpm.conf.in /usr/local/etc/php-fpm.conf

$ mkdir /usr/local/etc/php-fpm.d/

$ cp ~/php-7.2.3/sapi/fpm/www.conf.in /usr/local/etc/php-fpm.d/www.conf

修改php-fpm.conf

1
2
3
4
$ vi /usr/local/etc/php-fpm.conf

## include 值
include=/usr/local/etc/php-fpm.d/www.conf

修改/etc/init.d/php-fpm

1
2
3
php_fpm_BIN=/usr/local/sbin/php-fpm
php_fpm_CONF=/usr/local/etc/php-fpm.conf
php_fpm_PID=/usr/local/php/run/php-fpm.pid

PHP扩展

PHP源码集成一些比较流行的PHP组件,可以在ext/目录查看

组件安装

1
2
3
4
5
6
7
8
## 进入扩展目录
$ cd ext/
## 进入需要扩展的组件目录,例如:gd组件
$ cd gd
## 可以使用-help,查看组件说明
$ ./configure -help
## 编译安装
$ phpize && ./configure && make && make install

PHP 常用命令

1
2
3
4
# 查看php版本
$ php -v
# 查看php php.ini文件位置
$ php --ini

删除PHP

1
$ yum remove php*

数据库搭建

MariaDB 10.2.13

MariaDB有2个分支版本5.x和10.x,5.x主要兼容MYSQL5.x而10.x为MariaDB自身版本

安装

配置MariaDB官方源

1
$ vi /etc/yum.repos.d/MariaDB.repo

源内容如下:

1
2
3
4
5
6
7
# MariaDB 10.2 CentOS repository list - created 2018-03-21 08:03 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
1
$ sudo yum install MariaDB-server

离线下载:https://downloads.mariadb.com/MariaDB/mariadb-10.2/yum/centos/7/x86_64/rpms/

MariaDB管理命令

1
2
3
4
$ systemctl start mariadb  #启动MariaDB
$ systemctl stop mariadb #停止MariaDB
$ systemctl restart mariadb #重启MariaDB
$ systemctl enable mariadb #设置开机启动

初始化

1
2
3
4
5
## 启动MariaDB,并设置开机启动 
$ systemctl start mariadb
$ systemctl enable mariadb
## 进去MariaDB,按Enter进入
$ mysql -uroot -p

允许远程连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
## 进入到mysql数据库
mysql> use mysql;
Database changed

## 查看登录用户信息,Host已经包含%的配置
mysql> SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
+--------+-----------+
| User | Host |
+--------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | gandalf |
+--------+-----------+

## 授予任何主机访问权限

## 授予任何主机访问权限
### % 为通配符,如果需要限制某IP段可以改为'root'@'192.168.%'
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

## 查看登录用户信息是否已经有Host为%通配符的数据
mysql> SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
+------+-----------------------+
| User | Host |
+------+-----------------------+
| root | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+

## 刷新配置
mysql> flush privileges;


## 查看当前MariaDB版本
mysql> select version();
+-----------------+
| version() |
+-----------------+
| 10.2.13-MariaDB |
+-----------------+
1 row in set (0.00 sec)


## 3306默认端口开放
修改防火墙规则
$ firewall-cmd --add-port=3306/tcp
$ firewall-cmd --permanent --add-port=3306/tcp

修改root密码

1
2
3
4
5
6
## 进入MariaDB 
$ mysql -u root -h 192.168.1.201 -p 3306
$ use mysql
$ update user set password=password('123456') where user='root';
$ flush privileges;
$ exit;

数据库卸载

1
2
$ yum -y remove mari*
$ rm -rf /var/lib/mysql/*

Redis 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## 安装wget包,有可以忽略
$ yum -y install wget
## 下载Redis
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
## 解压Redis
$ tar xzf redis-4.0.8.tar.gz
## 编译安装Redis
$ cd redis-4.0.8
## 安装make包,有可以忽略
$ yum install make
$ make test
$ make install
## 将Redis添加到服务
$ cd utils
$ ./install_server.sh
## 查看Redis版本
$ redis-server -v
1
$ vi /etc/init.d/redis

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: 2345 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

redis="/usr/local/bin/redis-server"
prog=$(basename $redis)

REDIS_CONF_FILE="/etc/redis.conf"

[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/run/redis.pid

start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
#redis has daemonize in it's own code!
$redis $REDIS_CONF_FILE
retval=$?
if [ $retval -eq 0 ]
then
action $"Starting $prog: " /bin/true
touch $lockfile
else
action $"Starting $prog: " /bin/false
fi
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac

增加服务开机启动服务

1
2
3
4
chmod +x /etc/init.d/redis    
chkconfig --add redis
chkconfig --level 345 redis on
chkconfig --list redis

防火墙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## 查看防火墙规则
$ sudo firewall-cmd --list-all

## 永久开放需要的端口
$ sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
$ sudo firewall-cmd --reload

## 常用命令

## 重启防火墙
systemctl restart firewalld
## 查看防火墙状态
firewall-cmd --state
firewall-cmd --list-all
## 关闭防火墙
systemctl disable firewalld
systemctl stop firewalld
## 启动防火墙
systemctl enable firewalld
systemctl start firewalld
systemctl status firewalld

问题

系统安装

CentOS 7 minimal 版本安装后网络配置

处理:https://my.oschina.net/ihanfeng/blog/601853

或者

1
2
3
4
5
$ cd /etc/sysconfig/network-scripts
## 对ifcfg-xxx网络配置文件进行修改
$ vi ifcfg-xxx
## 重启网络服务
$ service network restart

ifconfig: command not found

解决:

1
$ yum install net-tools

Nginx 安装

执行 ./configure 时候出错 1

1
2
3
4
5
checking for OS
+ Linux 2.6.32-642.el6.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

处理:
如果安装出现在下面的错误是缺少编译环境,安装编译源码所需的工具和库

1
$ yum -y install gcc

执行 ./configure 时候出错 2

1
2
3
4
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
1
$ yum -y install pcre-devel openssl openssl-devel

安装Nginx 加载插件

1
./configure --with-stream  --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=../nginx-sticky-module-1.1  --add-module=../nginx_upstream_check_module-master

MacOS openssl 问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ git clone git://git.openssl.org/openssl.git  
$ sudo ./config --prefix=/usr/local/openssl
$ make
$ make install
## 查看版本
$ openssl version


打开nginx源文件下的/usr/local/src/nginx-1.9.9/auto/lib/openssl/conf文件:
找到这么一段代码:
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
修改成以下代码:
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
然后再进行Nginx的编译安装即可

可以查看openssl error

Nginx读取php目录权限问题

1
nginx: [emerg] getgrnam("root") failed in /usr/local/nginx/conf/nginx.conf:1

可以修改/usr/local/nginx/conf/nginx.conf

添加

1
$ user root owner;

Redis安装

编译问题 cc: command not found

1
2
3
4
5
6
7
8
[root@localhost redis-4.0.8]# make install
cd src && make install
make[1]: Entering directory `/root/redis-4.0.8/src'
CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/root/redis-4.0.8/src'
make: *** [install] Error 2

解决:

1
$ yum  install  gcc

编译问题 fatal error: jemalloc/jemalloc.h: No such file or directory

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost redis-4.0.8]# make install
cd src && make install
make[1]: Entering directory `/root/redis-4.0.8/src'
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-4.0.8/src'
make: *** [install] Error 2

解决:

1
$ make MALLOC=libc

编译问题 You need tcl 8.5 or newer in order to run the Redis test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost redis-4.0.8]# make install
cd src && make install
make[1]: Entering directory `/root/redis-4.0.8/src'

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/root/redis-4.0.8/src'
[root@localhost redis-4.0.8]# make test
cd src && make test
make[1]: Entering directory `/root/redis-4.0.8/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-4.0.8/src'
make: *** [test] Error 2

解决:

1
$ yum install tcl

PHP 安装 freetype问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装freetype 
$ wget http://ftp.twaren.net/Unix/NonGNU/freetype/freetype-2.5.5.tar.gz

$ tar -xf freetype-2.5.5.tar.gz

$ cd freetype-2.5.5
$ ./configure --prefix=/usr/local/freetype

$ make && make install

# 跳转到php源码GD目录
$ cd php-7.2.3/ext/gd/ ##定位到GD目录

$ phpize

$ ./configure --with-php-config=/usr/local/bin/php-config --with-jpeg-dir --with-png-dir --with-freetype-dir=/usr/include/freetype2/freetype/

$ make && make install

数据库

时区问题

传送门 -> https://mariadb.com/kb/en/library/time-zones/

参考资料