系统环境:

  • 系统版本:CentOS-7-x86_64-Minimal-1908
  • 内核版本:3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

LNMP 架构介绍

LNMP 架构是一种常见的 Web 服务器架构,它由 Linux、Nginx、MySQL(或MariaDB)、PHP 组成。以下是 LNMP 架构的详细介绍:

  1. Linux(操作系统):LNMP 架构的基础是 Linux 操作系统。Linux 是一个开源的、免费的操作系统,具有稳定性、安全性和灵活性。您可以选择适合您需求的 Linux 发行版,如 Ubuntu、CentOS 等。

  2. Nginx(Web 服务器):Nginx 是一个高性能的开源 Web 服务器和反向代理服务器。它具有低内存消耗、高并发能力和出色的性能。Nginx 可以处理静态文件、动态内容和负载均衡,并提供 SSL/TLS 加密等功能。

  3. MySQL(或MariaDB,数据库):MySQL 是一个流行的关系型数据库管理系统,用于存储和管理网站或应用程序的数据。它支持事务处理、复制和高可用性。MariaDB 是 MySQL 的一个分支,提供了与 MySQL 兼容的特性,并且在某些方面性能更好。

  4. PHP(服务器端脚本语言):PHP 是一种广泛使用的服务器端脚本语言,用于开发动态网页和 Web 应用程序。它与 Nginx 配合使用,可以处理用户请求并生成动态内容。PHP 具有丰富的功能和大量的开发框架,如 Laravel、Symfony 等。

LNMP 架构的优点包括:

  • 高性能:Nginx 和 PHP-FPM(PHP FastCGI 进程管理器)的组合可以提供高性能和良好的并发处理能力。
  • 可扩展性:LNMP 架构可以轻松扩展以适应高流量和大规模应用程序的需求。
  • 灵活性:通过 Nginx 的配置,可以实现负载均衡、反向代理、静态文件缓存等功能。
  • 安全性:Nginx 提供了强大的安全功能,如访问控制、反向代理和 SSL/TLS 加密。

当然,LNMP 架构也有一些注意事项,如安全性配置、性能调优和合理的服务器资源分配等。在实际使用 LNMP 架构时,您还可以根据具体需求进行定制和扩展,以满足特定的应用程序需求。

安装脚本:

一键安装脚本:

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash

set -eu


# 安装初始依赖包
yum install -y wget gcc gcc-c++ make autoconf automake unzip lrzsz vim epel-release systemd-devel systemtap-sdt-devel libxml2-devel openssl-devel bzip2-devel curl-devel libjpeg-devel libpng-devel libXpm-devel freetype-devel t1lib-devel gmp-devel libicu-devel libmcrypt-devel libedit-devel libxslt-devel gd-devel GeoIP-devel gperftools-devel libatomic_ops-devel

yum update -y


## MySQL 5.6.40

datadir='/data/mysql'

if [ -z "$(egrep mysql /etc/passwd)" ] ;then
useradd -s /sbin/nologin mysql
fi

if [ ! -d "${datadir}" ] ;then
mkdir -p ${datadir}
fi

if [ ! -f '/usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz' -a ! -d '/usr/local/mysql' ]; then
wget -O /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz

tar -zxf /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz -C /usr/local/src/

mv /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64 /usr/local/mysql
elif [ -f '/usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz' -a ! -d '/usr/local/mysql' ]; then

tar -zxf /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz -C /usr/local/src/

mv /usr/local/src/mysql-5.6.50-linux-glibc2.12-x86_64 /usr/local/mysql
else
echo 'MySQL 5.6.50 is already installed'
fi

cd /usr/local/mysql/

./scripts/mysql_install_db --datadir=${datadir} --user=mysql --skip-name-resolve

echo 'PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh



cat <<EOF >/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target

[Service]
ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql
User=mysql
Group=mysql
TimeoutSec=300
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

## PHP 5.6.40

useradd -s /sbin/nologin php-fpm

wget -O /usr/local/src/php-5.6.40.tar.gz https://www.php.net/distributions/php-5.6.40.tar.gz

tar -zxf /usr/local/src/php-5.6.40.tar.gz -C /usr/local/src/

cd /usr/local/src/php-5.6.40

./configure --prefix=/usr/local/php --enable-mod-charset --enable-fpm --with-fpm-user=php-frpm --with-fpm-group=php-fpm --with-fpm-systemd --enable-phpdbg --with-config-file-path=/usr/local/php/etc --disable-ipv6 --enable-dtrace --with-libxml-dir --with-openssl --with-pcre-regex --with-zlib --enable-bcmath --with-bz2 --with-curl --enable-dba --enable-exif --with-pcre-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-xpm-dir --with-t1lib --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-intl --enable-mbstring --with-libmbfl --with-onig --with-mcrypt --with-mysql=mysqlnd --with-mysqli=mysqlnd --enable-embedded-mysqli --enable-opcache --enable-pcntl --with-pdo-mysql=mysqlnd --with-libedit --enable-shmop --enable-soap --enable-sockets --enable-wddx --enable-zip --with-pcre-dir --enable-mysqlnd --enable-maintainer-zts --enable-zend-signals

make && make install

cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/php-fpm.service /lib/systemd/system/
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf
sed -i 's/php-frpm/php-fpm/' /usr/local/php/etc/php-fpm.conf


##NGINX 1.18.0
useradd -s /sbin/nologin nginx
# yum install -y libxslt-devel gd-devel GeoIP-devel gperftools-devel libatomic_ops-devel

wget -O /usr/local/src/openssl-1.1.1w.tar.gz https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -zxf /usr/local/src/openssl-1.1.1w.tar.gz -C /usr/local/src/

wget -O /usr/local/src/zlib.tar.gz https://www.zlib.net/current/zlib.tar.gz
tar -zxf /usr/local/src/zlib.tar.gz -C /usr/local/src/

wget -O /usr/local/src/nginx-1.18.0.tar.gz https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxf /usr/local/src/nginx-1.18.0.tar.gz -C /usr/local/src/

cd /usr/local/src/nginx-1.18.0/

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-google_perftools_module \
--with-compat \
--with-zlib=/usr/local/src/zlib-1.3 \
--with-pcre \
--with-libatomic \
--with-openssl=/usr/local/src/openssl-1.1.1w

make && make install

PHP configure error

1
2
3
4
5
6
7
8
9
checking for pkg-config... /usr/bin/pkg-config
checking for sd_notify in -lsystemd-daemon... no
checking systemd/sd-daemon.h usability... no
checking systemd/sd-daemon.h presence... no
checking for systemd/sd-daemon.h... no
configure: error: Your system does not support systemd.


yum install -y systemd-devel
1
2
3
4
5
6
7
checking for crypt_r... no

General settings
checking whether to include gcov symbols... yes
checking for lcov... no
checking for genhtml... no
configure: error: To enable code coverage reporting you must have LTP installed
1
2
3
4
5
6
7
8
General settings
checking whether to include gcov symbols... yes
checking for lcov... no
checking for genhtml... no
configure: error: To enable code coverage reporting you must have LTP installed


去掉了 --enable-gcov \
1
2
3
4
5
6
7
8
9
10
11
12
13
14
General settings
checking whether to include gcov symbols... no
checking whether to include debugging symbols... no
checking layout of installed files... PHP
checking path to configuration file... /usr/local/php/etc
checking where to scan for configuration files...
checking whether to enable PHP's own SIGCHLD handler... no
checking whether to explicitly link against libgcc... no
checking whether to enable short tags by default... yes
checking whether to enable dmalloc... yes
checking for dmalloc_error in -ldmalloc... no
configure: error: Problem with enabling dmalloc. Please check config.log for details.

去掉 --enable-dmalloc \
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
General settings
checking whether to include gcov symbols... no
checking whether to include debugging symbols... no
checking layout of installed files... PHP
checking path to configuration file... /usr/local/php/etc
checking where to scan for configuration files...
checking whether to enable PHP's own SIGCHLD handler... no
checking whether to explicitly link against libgcc... no
checking whether to enable short tags by default... yes
checking whether to enable dmalloc... no
checking whether to enable IPv6 support... no
checking whether to enable DTrace support... yes
checking sys/sdt.h usability... no
checking sys/sdt.h presence... no
checking for sys/sdt.h... no
configure: error: Cannot find sys/sdt.h which is required for DTrace support


systemtap-sdt-devel

1
2
3
4
5
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

[root@localhost php-5.6.40]# yum install -y libxml2-devel

1
2
3
4
5
6
7
8
9
checking for Kerberos support... no
checking whether to use system default cipher list instead of hardcoded value... no
checking for DSA_get_default_method in -lssl... no
checking for X509_free in -lcrypto... no
checking for RAND_egd... no
configure: error: Cannot find OpenSSL's <evp.h>

[root@localhost php-5.6.40]# yum install -y openssl-devel

1
2
3
4
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

[root@localhost php-5.6.40]# yum install -y bzip2-devel
1
2
3
4
5
6
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/

[root@localhost php-5.6.40]# yum install -y curl-devel

1
2
3
4
If configure fails try --with-vpx-dir=<DIR>
configure: error: jpeglib.h not found.

libjpeg-devel
1
2
3
4
5
If configure fails try --with-vpx-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
configure: error: png.h not found.

libpng-devel
1
2
3
4
5
6
If configure fails try --with-vpx-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
checking for png_write_image in -lpng... yes
configure: error: xpm.h not found.

libXpm-devel
1
2
3
configure: error: freetype-config not found.

freetype-devel
1
2
3
configure: error: Your t1lib distribution is not installed correctly. Please reinstall it.

t1lib-devel
1
2
3
4
configure: error: Unable to locate gmp.h

gmp-devel

1
2
3
4
checking for location of ICU headers and libraries... not found
configure: error: Unable to detect ICU prefix or yes/bin/icu-config failed. Please verify ICU install prefix and make sure icu-config works.

libicu-devel 不生效
1
2
3
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

libmcrypt-devel
1
2
3
4
checking for libedit readline replacement... yes
configure: error: Please reinstall libedit - I cannot find readline.h

libedit-devel

nginx

1
2
3
4
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

libxslt-devel
1
2
3
4
5
6
7
8
9
hecking for GD library ... not found
checking for GD library in /usr/local/ ... not found
checking for GD library in /usr/pkg/ ... not found
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

gd-devel
1
2
3
4
5
6
7
8
9
checking for GeoIP library in /usr/local/ ... not found
checking for GeoIP library in /usr/pkg/ ... not found
checking for GeoIP library in /opt/local/ ... not found

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

[root@localhost nginx-1.18.0]# yum install -y GeoIP-devel

1
2
3
4
5
6
7
8
9
checking for Google perftools ... not found
checking for Google perftools in /usr/local/ ... not found
checking for Google perftools in /opt/local/ ... not found

./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.

gperftools-devel

1
2
3
4
5
6
7
8
9
10
checking for GD WebP support ... not found
checking for GeoIP library ... found
checking for GeoIP IPv6 support ... found
checking for Google perftools ... found
checking for atomic_ops library ... not found

./configure: error: libatomic_ops library was not found.

libatomic_ops-devel

make

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost nginx-1.18.0]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/local/src/nginx-1.18.0'
cd /usr/local/ssl \
&& if [ -f Makefile ]; then make clean; fi \
&& ./config --prefix=/usr/local/ssl/.openssl no-shared no-threads \
&& make \
&& make install_sw LIBDIR=lib
/bin/sh: line 2: ./config: No such file or directory
make[1]: *** [/usr/local/ssl/.openssl/include/openssl/ssl.h] Error 127
make[1]: Leaving directory `/usr/local/src/nginx-1.18.0'
make: *** [build] Error 2

要指定 openssl 源码解压目录,不能指定 opnessl 安装目录
1
2
3
4
5
6
7
8
9
10
11
12
&& CFLAGS="-O2 -fomit-frame-pointer -pipe " CC="cc" \
./configure \
&& make libz.a
make[2]: Entering directory `/usr'
make[2]: *** No rule to make target `distclean'. Stop.
make[2]: Leaving directory `/usr'
make[1]: *** [/usr/libz.a] Error 2
make[1]: Leaving directory `/usr/local/src/nginx-1.18.0'
make: *** [build] Error 2


--with-zlib=/usr/local/src/zlib