和LAMP不同的是,LNMP中的N指的是Nginx(类似于Apache的一种web服务软件),并且php是作为一个独立服务存在的,这个服务叫做php-fpm,Nginx直接处理静态请求,动态请求会转发给php-fpm。
目前LNMP环境的应用非常多。在静态页面的处理上,Nginx比Apache更强;但在动态页面的处理上,Nginx并不占优势。
12.1 安装MySQL
LNMP中MySQL的安装步骤和LAMP一样。
下载软件包:
1
2
3
初始化:
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper #有报错,安装所缺包
perl-Data-Dumper.x86_64 2.145-3.el7 @base
perl-XML-Dumper.noarch 0.81-17.el7 base
Installing MySQL system tables…/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory #再次报错,安装所缺包
0 #检验上条命令是否执行成功,0表示执行成功
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
配置MySQL:
cp:是否覆盖"/etc/my.cnf"? y
1
2
3
4
[mysqld]
innodb_buffer_pool_size = 128M
basedir = /usr/local/mysql #这里做3处修改,basedir 是MySQL包所在的路径,datadir 是定义的存放数据的地方,port 定义MySQL服务监听的端口,如果不定义默认就是3306
datadir = /usr/local/mysql/data/mysql
port = 3306
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/mysql #修改成这样
1
2
3
4
5
6
7
8
9
10
11
启动MySQL:
Starting MySQL.Logging to ‘/usr/local/mysql/data/mysql/localhost.localdomain.err’.
. SUCCESS! #mysqld服务启动成功
tcp6 0 0 :::3306 :: LISTEN 3655/mysqld
1
2
3
4
5
6
12.2 安装PHP
下载源码包:
1
2
3
解压源码包,创建账号:
1
2
3
配置编译选项:
–prefix=/usr/local/php-fpm
–with-config-file-path=/usr/local/php-fpm/etc
–enable-fpm
–with-fpm-user=php-fpm
–with-fpm-group=php-fpm
–with-mysql=/usr/local/mysql
–with-mysql-sock=/tmp/mysql.sock
–with-libxml-dir
–with-gd
–with-jpeg-dir
–with-png-dir
–with-freetype-dir
–with-iconv-dir
–with-zlib-dir
–with-mcrypt
–enable-soap
–enable-gd-native-ttf
–enable-ftp
–enable-mbstring
–enable-exif
–disable-ipv6
–with-pear
–with-curl
–with-openssl #多了–enable-fpm,如果不加该参数,则不会有php-fpm执行文件生成,更不能启动php-fpm服务
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
错误1:
checking for cc… no
checking for gcc… no
configure: error: in/usr/local/src/php-5.6.36': configure: error: no acceptable C compiler found in $PATH See
config.log’ for more details
1
2
3
4
5
6
7
8
9
错误2:
checking for xml2-config path…
configure: error: xml2-config not found. Please check your libxml2 installation.
libxml2.x86_64 2.9.1-6.el7_2.3 @anaconda
libxml2.i686 2.9.1-6.el7_2.3 base
libxml2-devel.i686 2.9.1-6.el7_2.3 base
libxml2-devel.x86_64 2.9.1-6.el7_2.3 base
libxml2-python.x86_64 2.9.1-6.el7_2.3 base
libxml2-static.i686 2.9.1-6.el7_2.3 base
libxml2-static.x86_64 2.9.1-6.el7_2.3 base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
错误3:
configure: error: Cannot find OpenSSL’s
1
2
3
4
5
错误4:
checking for cURL in default path… not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in /include/curl/
1
2
3
4
5
6
7
错误5:
configure: error: jpeglib.h not found.
1
2
3
4
5
错误6:
configure: error: png.h not found.
1
2
3
4
5
错误7:
configure: error: freetype-config not found.
1
2
3
4
5
错误8:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
1
2
3
4
5
6
7
终于不再提示错误,有这样的信息:
±-------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
±-------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
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
这就说明PHP配置编译参数完成。
编译php:
Build complete.
Don’t forget to run ‘make test’.
0
1
2
3
4
5
6
编译完成(在这一步,也有可能会遇到问题)。
安装php:
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.36/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers: /usr/local/php-fpm/include/php/ext/pdo/
0
1
2
3
4
5
6
7
8
9
安装完成。
修改配置文件:
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
[01-Jul-2018 21:08:57] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful #显示 test is successful ,说明配置没有问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
启动php-fpm:
useradd:用户“php-fpm”已存在 #如果之前进行过这一步,那这里就可以省略
Starting php-fpm done
root 30222 0.0 0.1 123452 4812 ? Ss 21:14 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 30223 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30224 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30225 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30226 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30227 0.0 0.1 123452 4584 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30228 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30229 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30230 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30231 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30232 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30233 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30234 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30235 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30236 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30237 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30238 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30239 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30240 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30241 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30242 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
root 30248 0.0 0.0 112720 980 pts/0 S+ 21:16 0:00 grep --color=auto php-fpm #说明php-fpm成功启动
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
12.3 安装Nginx
下载和解压Nginx:
1
2
3
4
5
配置编译选项:
Configuration summary
nginx path prefix: “/usr/local/nginx”
nginx binary file: “/usr/local/nginx/sbin/nginx”
nginx modules path: “/usr/local/nginx/modules”
nginx configuration prefix: “/usr/local/nginx/conf”
nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
nginx pid file: “/usr/local/nginx/logs/nginx.pid”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”
0
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
编译和安装Nginx:
0
0
1
2
3
4
5
6
7
8
9
编写Nginx启动脚本,并加入系统服务:
1
#!/bin/bash
. /etc/init.d/functions
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog=“Nginx”
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c N G I N X C O N F R E T V A L = NGINX_CONF RETVAL= NGINXCONFRETVAL=?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID N G I N X S B I N − T E R M r m − r f / d e v / s h m / n g i n x t e m p R E T V A L = NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL= NGINXSBIN−TERMrm−rf/dev/shm/nginxtempRETVAL=?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID N G I N X S B I N − H U P R E T V A L = NGINX_SBIN -HUP RETVAL= NGINXSBIN−HUPRETVAL=?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case “$1” in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $“Usage: $0 {start|stop|reload|restart|configtest}”
RETVAL=1
esac
exit $RETVAL
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
1
2
3
4
5
更改Nginx的配置文件:
1
2
3
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip ‘$remote_addr KaTeX parse error: Double subscript at position 7: http_x_̲forwarded_for [time_local]’
’ h o s t " host " host"request_uri" KaTeX parse error: Double superscript at position 13: status' '̲ "http_referer" "KaTeX parse error: Can't use function '\.' in math mode at position 990: … location ~ \̲.̲php
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
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
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful #显示上面两行说明配置正确
1
2
3
启动Nginx:
Starting nginx (via systemctl): [ 确定 ] #如果不能启动,可以查看/usr/local/nginx/logs/error.log文件
root 32821 0.0 0.0 20540 624 ? Ss 21:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 32822 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker process
nobody 32823 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker process
root 32825 0.0 0.0 112720 984 pts/0 S+ 21:48 0:00 grep --color=auto nginx
1
2
3
4
5
6
7
8
可以看到,Nginx服务成功启动。
测试是否正确解析PHP:
php解析正常[root@localhost nginx-1.12.1]#