Linux 笔记 - 第十六章 LNMP 之(一) 环境搭建

博客地址:http://www.moonxy.com

一、前言

LNMP 中的 N 指 Nginx,在静态页面的处理上,Nginx 较 Apache 更胜一筹;但在动态页面的处理上,Nginx 并不比 Apache 有优势,目前有很多爱好者对 Nginx 比较热衷。

二、安装 LNMP

LNMP 中 MySQL 的安装步骤和 LAMP 中的一样,前面的文章已经介绍,此处只讲解 PHP 和 Nginx 的安装。

2.1 安装 PHP

下载 PHP 的源码包 php-7.2.8.tar.gz 到 /usr/local/src 目录下,解压得到 php-7.2.8。

预编译

[root@ryan ~]# cd /usr/local/src/php-7.2.8
[root@ryan php-7.2.8]# useradd -s /sbin/nologin php-fpm
[root@ryan php-7.2.8]# ./configure --prefix=/usr/local/lnmp/php7 --with-config-file-path=/usr/local/lnmp/php7/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --enable-soap --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --without-pear --with-curl --with-openssl
[root@ryan php-7.2.8]# echo $?
0

其实此处预编译参数中还应该添加上 --with-mysqli,表示添加 MySQL 模块支持,以前版本可以添加的 --with-mysql 参数在 PHP7 中已经过期失效。如果在预编译时没有添加参数 --with-mysqli,则需要按照下面的 2.2 所示的步骤来安装 mysqli 动态扩展 。

PHP7 相对与 PHP5 的最大变化之一是移除了mysql 扩展,推荐使用 mysqli 或者 pdo_mysql,实际上在 PHP5.5 开始,PHP 就着手开始准备弃用 mysql 扩展,如果你使用 mysql 扩展,可能看到过这样的提示 "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in"。所以在以后的程序中,为了保持兼容性,要尽量减少使用 mysql 扩展用于数据库连接。

编译

[root@ryan php-7.2.8]# make
[root@ryan php-7.2.8]# echo $?
0

安装

[root@ryan php-7.2.8]# make install
[root@ryan php-7.2.8]# echo $?
0

修改配置文件

[root@ryan php-7.2.8]# cp php.ini-production /usr/local/lnmp/php7/etc/php.ini
[root@ryan php-7.2.8]# cp /usr/local/lnmp/php7/etc/php-fpm.conf.default /usr/local/lnmp/php7/etc/php-fpm.conf

将以下内容写入 php-fpm.conf 文件,如下:

[root@ryan php-7.2.8]# vim /usr/local/lnmp/php7/etc/php-fpm.conf
[global]
pid = /usr/local/lnmp/php7/var/run/php-fpm.pid
error_log = /usr/local/lnmp/php7/var/log/php-fpm.log
[www]
listen = 127.0.0.1:9000
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

验证配置文件

[root@ryan php-7.2.8]# /usr/local/lnmp/php7/sbin/php-fpm -t
[12-Aug-2018 18:52:29] NOTICE: configuration file /usr/local/lnmp/php7/etc/php-fpm.conf test is successful

如果显示 "test is successful" 则说明配置没有问题;否则就要根据提示检查配置文件。

复制服务启动脚本

[root@ryan php-7.2.8]# cp /usr/local/src/php-7.2.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@ryan php-7.2.8]# chmod 755 /etc/init.d/php-fpm

启动 php-fpm 服务

[root@ryan php-7.2.8]# service php-fpm start
Starting php-fpm  done

查看进程

[root@ryan php-7.2.8]# ps aux |grep php-fpm
root      15607  0.0  0.3 106596  6112 ?        Ss   19:22   0:00 php-fpm: master process (/usr/local/lnmp/php7/etc/php-fpm.conf)                                                                   
php-fpm   15608  0.0  0.2 106596  5556 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15609  0.0  0.2 106596  5560 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15610  0.0  0.2 106596  5560 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15611  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15612  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15613  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15614  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15615  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15616  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15617  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15618  0.0  0.2 106596  5568 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15619  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15620  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15621  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15622  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15623  0.0  0.2 106596  5560 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15624  0.0  0.2 106596  5560 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15625  0.0  0.2 106596  5568 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15626  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
php-fpm   15627  0.0  0.2 106596  5564 ?        S    19:22   0:00 php-fpm: pool www                                                                                                                 
root      15629  0.0  0.0 103312   880 pts/0    S+   19:22   0:00 grep php-fpm

可以看到启动了很多进程,大约有二十多个。

也可以使用 php-fpm -m 查看模块和 php-fpm -i 查看phpinfo,如下:

[root@ryan conf]# /usr/local/lnmp/php7/sbin/php-fpm -m
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

[root@ryan conf]# /usr/local/lnmp/php7/sbin/php-fpm -i
phpinfo()
PHP Version => 7.2.8

System => Linux ryan 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64
Build Date => Aug 12 2018 15:24:13
Configure Command =>  './configure'  '--prefix=/usr/local/lnmp/php7' '--with-config-file-path=/usr/local/lnmp/php7/etc' '--enable-fpm' '--with-fpm-user=php-fpm' '--with-fpm-group=php-fpm' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--enable-soap' '--enable-ftp' '--enable-mbstring' '--enable-exif' '--disable-ipv6' '--without-pear' '--with-curl' '--with-openssl'
Server API => FPM/FastCGI
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/lnmp/php7/etc
Loaded Configuration File => /usr/local/lnmp/php7/etc/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20170718
PHP Extension => 20170718
Zend Extension => 320170718
Zend Extension Build => API320170718,NTS
PHP Extension Build => API20170718,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => disabled
DTrace Support => disabled
……

2.2 PHP 动态扩展模块安装

由于在上面安装 PHP 的时候,没有安装 MySQL 的支持模块 mysqli,这会导致后面访问系统时,有可能出现如下错误提示:Oops!System file lost class/class_mysqli.php

所以此处讲解如何给已经安装完成的 PHP 安装动态扩展模块。

进入 PHP 扩展模块目录:/usr/local/src/php-7.2.8/ext

[root@ryan ~]# cd /usr/local/src/php-7.2.8/ext
[root@ryan ext]# ll
total 320
drwxrwxr-x  4 root root  4096 Jul 17 13:35 bcmath
drwxrwxr-x  3 root root  4096 Jul 17 13:35 bz2
drwxrwxr-x  3 root root  4096 Jul 17 13:35 calendar
drwxrwxr-x  3 root root  4096 Jul 17 13:35 com_dotnet
drwxrwxr-x  4 root root  4096 Aug 12 15:21 ctype
drwxrwxr-x  4 root root  4096 Aug 12 15:21 curl
drwxrwxr-x  5 root root  4096 Aug 12 15:19 date
drwxrwxr-x  6 root root  4096 Jul 17 13:35 dba
drwxrwxr-x  5 root root  4096 Aug 12 15:21 dom
drwxrwxr-x  4 root root  4096 Jul 17 13:35 enchant
drwxrwxr-x  4 root root  4096 Aug 12 15:21 exif
-rwxrwxr-x  1 root root  9432 Jul 17 13:35 ext_skel
-rw-rw-r--  1 root root  1169 Jul 17 13:35 ext_skel_win32.php
drwxrwxr-x  5 root root  4096 Aug 12 15:21 fileinfo
drwxrwxr-x  5 root root  4096 Aug 12 15:21 filter
drwxrwxr-x  4 root root  4096 Aug 12 15:21 ftp
drwxrwxr-x  5 root root  4096 Aug 12 15:21 gd
drwxrwxr-x  3 root root  4096 Jul 17 13:35 gettext
drwxrwxr-x  3 root root  4096 Jul 17 13:35 gmp
drwxrwxr-x  5 root root  4096 Aug 12 15:22 hash
drwxrwxr-x  4 root root  4096 Aug 12 15:22 iconv
drwxrwxr-x  3 root root  4096 Jul 17 13:35 imap
drwxrwxr-x  3 root root  4096 Jul 17 13:35 interbase
drwxrwxr-x 21 root root  4096 Jul 17 13:35 intl
drwxrwxr-x  4 root root  4096 Aug 12 15:22 json
drwxrwxr-x  3 root root  4096 Jul 17 13:35 ldap
drwxrwxr-x  4 root root  4096 Aug 12 15:19 libxml
drwxrwxr-x  7 root root  4096 Aug 12 15:23 mbstring
drwxrwxr-x  3 root root  4096 Jul 17 13:35 mysqli
drwxrwxr-x  2 root root  4096 Jul 17 13:35 mysqlnd
drwxrwxr-x  3 root root  4096 Jul 17 13:35 oci8
drwxrwxr-x  3 root root  4096 Jul 17 13:35 odbc
drwxrwxr-x  5 root root  4096 Aug 12 15:19 opcache
drwxrwxr-x  4 root root  4096 Aug 12 15:19 openssl
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pcntl
drwxrwxr-x  5 root root  4096 Aug 12 15:20 pcre
drwxrwxr-x  4 root root  4096 Aug 12 15:23 pdo
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_dblib
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_firebird
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_mysql
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_oci
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_odbc
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pdo_pgsql
drwxrwxr-x  4 root root  4096 Aug 12 15:23 pdo_sqlite
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pgsql
drwxrwxr-x  5 root root  4096 Aug 12 15:26 phar
drwxrwxr-x  4 root root  4096 Aug 12 15:23 posix
drwxrwxr-x  3 root root  4096 Jul 17 13:35 pspell
drwxrwxr-x  3 root root  4096 Jul 17 13:35 readline
drwxrwxr-x  3 root root  4096 Jul 17 13:35 recode
drwxrwxr-x  4 root root  4096 Aug 12 15:23 reflection
drwxrwxr-x  4 root root  4096 Aug 12 15:23 session
drwxrwxr-x  3 root root  4096 Jul 17 13:35 shmop
drwxrwxr-x  5 root root  4096 Aug 12 15:23 simplexml
drwxrwxr-x  3 root root  4096 Jul 17 13:35 skeleton
drwxrwxr-x  3 root root  4096 Jul 17 13:35 snmp
drwxrwxr-x  5 root root  4096 Aug 12 15:23 soap
drwxrwxr-x  3 root root  4096 Jul 17 13:35 sockets
drwxrwxr-x  3 root root  4096 Jul 17 13:35 sodium
drwxrwxr-x  6 root root  4096 Aug 12 15:23 spl
drwxrwxr-x  5 root root  4096 Aug 12 15:20 sqlite3
drwxrwxr-x  5 root root 12288 Aug 12 15:24 standard
drwxrwxr-x  3 root root  4096 Jul 17 13:35 sysvmsg
drwxrwxr-x  3 root root  4096 Jul 17 13:35 sysvsem
drwxrwxr-x  3 root root  4096 Jul 17 13:35 sysvshm
drwxrwxr-x  4 root root  4096 Jul 17 13:35 tidy
drwxrwxr-x  4 root root  4096 Aug 12 15:24 tokenizer
drwxrwxr-x  3 root root  4096 Jul 17 13:35 wddx
drwxrwxr-x  4 root root  4096 Aug 12 15:24 xml
drwxrwxr-x  5 root root  4096 Aug 12 15:24 xmlreader
drwxrwxr-x  4 root root  4096 Jul 17 13:35 xmlrpc
drwxrwxr-x  5 root root  4096 Aug 12 15:24 xmlwriter
drwxrwxr-x  3 root root  4096 Jul 17 13:35 xsl
drwxrwxr-x  2 root root  4096 Jul 17 13:35 zend_test
drwxrwxr-x  5 root root  4096 Jul 17 13:35 zip
drwxrwxr-x  4 root root  4096 Aug 12 15:21 zlib
[root@ryan ext]# cd mysqli
[root@ryan mysqli]# ll
total 344
-rw-rw-r-- 1 root root  4597 Jul 17 13:35 config.m4
-rw-rw-r-- 1 root root  1619 Jul 17 13:35 config.w32
-rw-rw-r-- 1 root root    61 Jul 17 13:35 CREDITS
-rw-rw-r-- 1 root root 76537 Jul 17 13:35 mysqli_api.c
-rw-rw-r-- 1 root root 50510 Jul 17 13:35 mysqli.c
-rw-rw-r-- 1 root root  5012 Jul 17 13:35 mysqli_driver.c
-rw-rw-r-- 1 root root  3443 Jul 17 13:35 mysqli_embedded.c
-rw-rw-r-- 1 root root  2654 Jul 17 13:35 mysqli_exception.c
-rw-rw-r-- 1 root root 25883 Jul 17 13:35 mysqli_fe.c
-rw-rw-r-- 1 root root  5334 Jul 17 13:35 mysqli_fe.h
-rw-rw-r-- 1 root root  6571 Jul 17 13:35 mysqli_libmysql.h
-rw-rw-r-- 1 root root  2620 Jul 17 13:35 mysqli_mysqlnd.h
-rw-rw-r-- 1 root root 36916 Jul 17 13:35 mysqli_nonapi.c
-rw-rw-r-- 1 root root  5649 Jul 17 13:35 mysqli_priv.h
-rw-rw-r-- 1 root root 16317 Jul 17 13:35 mysqli_prop.c
-rw-rw-r-- 1 root root  2455 Jul 17 13:35 mysqli_report.c
-rw-rw-r-- 1 root root  5115 Jul 17 13:35 mysqli_result_iterator.c
-rw-rw-r-- 1 root root  8587 Jul 17 13:35 mysqli_warning.c
-rw-rw-r-- 1 root root  4368 Jul 17 13:35 package.xml
-rw-rw-r-- 1 root root  1678 Jul 17 13:35 php_mysqli.h
-rw-rw-r-- 1 root root  9987 Jul 17 13:35 php_mysqli_structs.h
drwxrwxr-x 2 root root 20480 Jul 17 13:35 tests
-rw-rw-r-- 1 root root    31 Jul 17 13:35 TODO

进入你要安装的模块目录,执行命令:/usr/local/lnmp/php7/bin/phpize,如下

[root@ryan mysqli]# /usr/local/lnmp/php7/bin/phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@ryan mysqli]# ll
total 1588
-rw-r--r-- 1 root root  85825 Aug 12 23:07 acinclude.m4
-rw-r--r-- 1 root root 316202 Aug 12 23:07 aclocal.m4
drwxr-xr-x 2 root root   4096 Aug 12 23:07 autom4te.cache
drwxr-xr-x 2 root root   4096 Aug 12 23:07 build
-rwxr-xr-x 1 root root  42938 Aug 12 23:07 config.guess
-rw-r--r-- 1 root root   1764 Aug 12 23:07 config.h.in
-rw-rw-r-- 1 root root   4597 Jul 17 13:35 config.m4
-rwxr-xr-x 1 root root  35967 Aug 12 23:07 config.sub
-rwxr-xr-x 1 root root 467868 Aug 12 23:07 configure
-rw-r--r-- 1 root root   4698 Aug 12 23:07 configure.ac
-rw-rw-r-- 1 root root   1619 Jul 17 13:35 config.w32
-rw-rw-r-- 1 root root     61 Jul 17 13:35 CREDITS
-rw-r--r-- 1 root root      0 Aug 12 23:07 install-sh
-rw-r--r-- 1 root root 199728 Aug 12 23:07 ltmain.sh
-rw-r--r-- 1 root root   7135 Aug 12 23:07 Makefile.global
-rw-r--r-- 1 root root      0 Aug 12 23:07 missing
-rw-r--r-- 1 root root      0 Aug 12 23:07 mkinstalldirs
-rw-rw-r-- 1 root root  76537 Jul 17 13:35 mysqli_api.c
-rw-rw-r-- 1 root root  50510 Jul 17 13:35 mysqli.c
-rw-rw-r-- 1 root root   5012 Jul 17 13:35 mysqli_driver.c
-rw-rw-r-- 1 root root   3443 Jul 17 13:35 mysqli_embedded.c
-rw-rw-r-- 1 root root   2654 Jul 17 13:35 mysqli_exception.c
-rw-rw-r-- 1 root root  25883 Jul 17 13:35 mysqli_fe.c
-rw-rw-r-- 1 root root   5334 Jul 17 13:35 mysqli_fe.h
-rw-rw-r-- 1 root root   6571 Jul 17 13:35 mysqli_libmysql.h
-rw-rw-r-- 1 root root   2620 Jul 17 13:35 mysqli_mysqlnd.h
-rw-rw-r-- 1 root root  36916 Jul 17 13:35 mysqli_nonapi.c
-rw-rw-r-- 1 root root   5649 Jul 17 13:35 mysqli_priv.h
-rw-rw-r-- 1 root root  16317 Jul 17 13:35 mysqli_prop.c
-rw-rw-r-- 1 root root   2455 Jul 17 13:35 mysqli_report.c
-rw-rw-r-- 1 root root   5115 Jul 17 13:35 mysqli_result_iterator.c
-rw-rw-r-- 1 root root   8587 Jul 17 13:35 mysqli_warning.c
-rw-rw-r-- 1 root root   4368 Jul 17 13:35 package.xml
-rw-rw-r-- 1 root root   1678 Jul 17 13:35 php_mysqli.h
-rw-rw-r-- 1 root root   9987 Jul 17 13:35 php_mysqli_structs.h
-rw-r--r-- 1 root root  85732 Aug 12 23:07 run-tests.php
drwxrwxr-x 2 root root  20480 Jul 17 13:35 tests
-rw-rw-r-- 1 root root     31 Jul 17 13:35 TODO

发现会多创建出来一些文件。

在预编译之前先将 mysqlnd 扩展复制到 mysqli 目录下,因为 mysqli 需要依赖 mysqlnd,如下:

[root@ryan mysqli]# mkdir ext
[root@ryan mysqli]# cd ../
[root@ryan ext]# cp -r mysqlnd mysqli/ext
[root@ryan ext]# cd mysqli

预编译

[root@ryan mysqli]# ./configure --with-php-config=/usr/local/lnmp/php7/bin/php-config --with-mysqli=/usr/local/mysql/bin/mysql_config
[root@ryan mysqli]# echo $?
0

在此安装中,--with-php-config=/usr/local/lnmp/php7/bin/php-config 参数是固定的。可以 find 来搜索使用 php-config 和 mysql_config。

编译

[root@ryan mysqli]# make
[root@ryan mysqli]# echo $?
0

安装

[root@ryan mysqli]# make install
……
----------------------------------------------------------------------
Installing shared extensions:     /usr/local/lnmp/php7/lib/php/extensions/no-debug-non-zts-20170718/
Installing header files:          /usr/local/lnmp/php7/include/php/
[root@ryan mysqli]# echo $?
0

查看动态模块文件

[root@ryan mysqli]# ll /usr/local/lnmp/php7/lib/php/extensions/no-debug-non-zts-20170718/
total 6252
-rwxr-xr-x 1 root root  749614 Aug 12 23:43 mysqli.so
-rwxr-xr-x 1 root root 3565484 Aug 12 15:29 opcache.a
-rwxr-xr-x 1 root root 2078551 Aug 12 15:29 opcache.so

可以看到已经生成 mysqli.so 模块文件,此目录为 PHP 的动态扩展模块目录,这与查看phpinfo中动态扩展模块得到的目录一致,如下:

[root@ryan mysqli]# /usr/local/lnmp/php7/bin/php -i |grep -i extension_dir
extension_dir => /usr/local/lnmp/php7/lib/php/extensions/no-debug-non-zts-20170718 => /usr/local/lnmp/php7/lib/php/extensions/no-debug-non-zts-20170718
sqlite3.extension_dir => no value => no value

添加扩展的模块

[root@ryan mysqli]# vim /usr/local/lnmp/php7/etc/php.ini
extension=mysqli.so

php.ini 里面的分号;是注释符。

查看 PHP 加载的模块

[root@ryan mysqli]# /usr/local/lnmp/php7/sbin/php-fpm -m
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

发现 mysqli 动态扩展模块已经添加完成。只要是使用 extension 手动添加的模块,均属于动态(shared)扩展模块,否则即为静态(static)模块。

2.3 安装 Nginx

访问 Nginx 官方网站:http://nginx.org,可以发现其更新速度很快,此处我们选择下载当前最新文档版,stable 1.14.0,如下:

下载和解压 Nginx

[root@ryan php-7.2.8]# cd /usr/local/src
[root@ryan src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@ryan src]# tar zxvf nginx-1.14.0.tar.gz

预编译

[root@ryan src]# cd nginx-1.14.0
[root@ryan nginx-1.14.0]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module
[root@ryan nginx-1.14.0]# echo $?
0

编译

[root@ryan nginx-1.14.0]# make
[root@ryan nginx-1.14.0]# echo $?
0

安装

[root@ryan nginx-1.14.0]# make install
[root@ryan nginx-1.14.0]# echo $?
0

启动 Nginx

[root@ryan nginx-1.14.0]# ll /usr/local/lnmp/nginx/sbin
total 5864
-rwxr-xr-x 1 root root 6000765 Aug 12 20:06 nginx
[root@ryan nginx-1.14.0]# /usr/local/lnmp/nginx/sbin/nginx
[root@ryan nginx-1.14.0]# ps aux |grep nginx
root      18259  0.0  0.0  46856  1192 ?        Ss   20:15   0:00 nginx: master process /usr/local/lnmp/nginx/sbin/nginx
nobody    18260  0.0  0.0  47288  1772 ?        S    20:15   0:00 nginx: worker process
root      18262  0.0  0.0 103308   876 pts/0    S+   20:15   0:00 grep nginx

看到 Nginx 默认使用系统自带的 nobody 用户。

配置解析 php

找到 /usr/local/lnmp/nginx/conf/nginx.conf,将如下内容前面的 # 删掉,如下:

[root@ryan nginx-1.14.0]# cd /usr/local/lnmp/nginx/conf
[root@ryan conf]# vim /usr/local/lnmp/nginx/conf/nginx.conf
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

并将其修改为:

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   /data/www/DiscuzX$fastcgi_script_name;
    include        fastcgi_params;
}

将 root 网站根目录 /data/www/DiscuzX; 放在上层,作为全局配置,并将 location / 中的 root he error_page 中的 root 注释掉。

验证 nginx 配置文件

[root@ryan conf]# /usr/local/lnmp/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

启动 Nginx 的命令

[root@ryan mysqli]# /usr/local/lnmp/nginx/sbin/nginx

重新加载

[root@ryan conf]# /usr/local/lnmp/nginx/sbin/nginx -s reload

测试是否正常解析

添加 index.php 到 index 索引页后,访问如下:

综上:

php 的配置文件为:/usr/local/lnmp/php7/etc/php.ini

php-fpm 的配置文件为:/usr/local/lnmp/php7/etc/php-fpm.conf

启动 MySQL 的命令:

# service mysqld start

启动 php-fpm 的命令:

# service php-fpm start

启动 nginx 的命令:

# /usr/local/lnmp/nginx/sbin/nginx

停止 nginx 的命令:

# /usr/local/lnmp/nginx/sbin/nginx -s stop

重新加载 nginx 配置文件的命令:

# /usr/local/lnmp/nginx/sbin/nginx -s reload

其中 -s signal:给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)

ldd命令用于打印程序或者库文件所依赖的共享库列表。如下:

[root@ryan ~]# which ls
alias ls='ls --color=auto'
        /bin/ls
[root@ryan ~]# ldd /bin/ls
        linux-vdso.so.1 =>  (0x00007ffc292e1000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f5b47388000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f5b47180000)
        libcap.so.2 => /lib64/libcap.so.2 (0x00007f5b46f7b000)
        libacl.so.1 => /lib64/libacl.so.1 (0x00007f5b46d73000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f5b469df000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f5b467da000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5b475b3000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5b465bd000)
        libattr.so.1 => /lib64/libattr.so.1 (0x00007f5b463b8000)

注意:在 ldd 命令打印的结果中,"=>"左边的表示该程序需要连接的共享库之 so 名称,右边表示由 Linux 的共享库系统找到的对应的共享库在文件系统中的具体位置。默认情况下,/etc/ld.so.conf 文件中包含有默认的共享库搜索路径。

附录

LNMP架构的搭建(Linux+Nginx+MySQL+PHP源码安装 )+Discuz论坛的搭建

centos php7 安装mysqli扩展心得:https://www.cnblogs.com/fengxuezhiyi/p/6896702.html

LNMP 一键安装包:https://lnmp.org/install.html

php-fpm 启动后没有监听端口9000:https://blog.csdn.net/bujidexinq/article/details/70209625

nginx命令详解:https://www.cnblogs.com/linux-centos/p/5790506.html

linux下php添加mysqli扩展mysqli_api.lo错误:https://jingyan.baidu.com/article/a3f121e4cb684afc9152bb73.html

Linux下 config/configure/Configure、make 、make test/make check、sudo make install 的作用

linux系统命令make.clean的用法讲解

posted on 2018-08-14 22:40  沐小悠  阅读(589)  评论(0编辑  收藏  举报