LNMP

关于作者

周李杰,最后一批90后,现居湖北武汉

孤独患者,懒癌中期,无脑幻想者。但却人畜无害,无需远离。

兴趣众多,然无一精通。正努力成为一个有趣的人。

1
2
> print("👆以上是不完整的我") //请我吃大餐将会了解更多
>

[TOC]

LNMP

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

Mysql是一个小型关系型数据库管理系统

PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

LNMP特点

​ LNMP动态网站部署架构是一套由Linux + Nginx + MySQL + PHP组成的动态网站系统解决方案,具有免费、高效、扩展性强且资源消耗低等优良特性。

​ Nginx是一款相当优秀的用于部署动态网站的轻量级服务程序,它最初是为俄罗斯门户站点而开发的,因其稳定性、功能丰富、占用内存少且并发能力强而备受用户的信赖。目前国内诸如新浪、网易、腾讯等门户站点均已使用了此服务。

Nginx服务程序的稳定性源自于采用了分阶段的资源分配技术,降低了CPU与内存的占用率,所以使用Nginx程序部署的动态网站环境不仅十分稳定、高效,而且消耗的系统资源也很少。此外,Nginx具备的模块数量与Apache具备的模块数量几乎相同,而且现在已经完全支持proxy、rewrite、mod_fcgi、ssl、vhosts等常用模块。更重要的是,Nginx还支持热部署技术,可以7×24不间断提供服务,还可以在不暂停服务的情况下直接对Nginx服务程序进行升级。

坦白来讲,虽然Nginx程序的代码质量非常高,代码很规范,技术成熟,模块扩展也很容易,但依然存在不少问题,比如是由俄罗斯人开发的,所以在资料文档方面还并不完善,中文资料的质量更是鱼龙混杂。但是Nginx服务程序在近年来增长势头迅猛,相信会在轻量级Web服务器市场具有不错的未来。

​ PHP(Hypertxt Preprocessor,超文本预处理器)是一种通用的开源脚本语言,发明于1995年,它吸取了C语言、Java语言及Perl语言的很多优点,具有开源、免费、快捷、跨平台性强、效率高等优良特性,是目前Web开发领域最常用的语言之一。

LNMP优点

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。

作为负载均衡服务器:Nginx 既可以在内部直接支持RailsPHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。

作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。

源码包安装

在使用源码包安装服务程序之前,首先要让安装主机具备编译程序源码的环境,他需要具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序。因此请先配置妥当Yum软件仓库

配置yum仓库

1
2
3
4
5
6
7
8
9
10
[root@server222 ~]# tail -n 1 /etc/fstab
/dev/cdrom /mnt/cdrom iso9660 defaults 0 0
[root@server222 ~]# vim /etc/yum.repos.d/bendi.repo
[root@server222 ~]# cat /etc/yum.repos.d/bendi.repo
[bendi]
name=///mnt/cdrom
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
[root@server222 ~]# mount -a

安装软件包

1
[root@server222 ~]# yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

下载源码包以及系统软件包

​ 然后我们安装LNMP动态网站部署架构所需的16个软件源码包和1个用于检查效果的论坛网站系统软件包

此处建议把要安装的软件包存放在/usr/local/src目录中。

首先我们创建/usr/local/src目录

1
[root@server222 ~]# mkdir /usr/local/src

源码包文件已经放到百度云盘里面,下载下来传到Linux系统里面/usr/local/src目录即可

链接:https://pan.baidu.com/s/1X-eh_SRBbpXcVuxcu62VTA
提取码:ox0w
复制这段内容后打开百度网盘手机App,操作更方便哦

1
2
3
4
5
6
7
8
[root@server222 ~]# cd /usr/local/src
[root@server222 src]# ls
cmake-2.8.11.2.tar.gz libpng-1.6.12.tar.gz php-5.5.14.tar.gz
Discuz_X3.2_SC_GBK.zip libvpx-v1.3.0.tar.bz2 t1lib-5.1.2.tar.gz
freetype-2.5.3.tar.gz mysql-5.6.19.tar.gz tiff-4.0.3.tar.gz
jpegsrc.v9a.tar.gz nginx-1.6.0.tar.gz yasm-1.2.0.tar.gz
libgd-2.1.0.tar.gz openssl-1.0.1h.tar.gz zlib-1.2.8.tar.gz
libmcrypt-2.5.8.tar.gz pcre-8.35.tar.gz

如果是17个包就算OK,差一个都不行

安装编译工具

cmake是Linux系统中一款常用的编译工具。

1
2
3
[root@server222 src]# tar xzvf cmake-2.8.11.2.tar.gz
[root@server222 src]# cd cmake-2.8.11.2/
[root@server222 cmake-2.8.11.2]# ./configure ; make ; make install

此处命令./configure ; make ; make install

格式为 命令A ; 命令B ; 命令C

这里意思是先执行命令A再执行命令B最后执行命令C

配置MySQL服务

​ 在使用Yum软件仓库安装服务程序时,系统会自动根据RPM软件包中的指令集完整软件配置等工作。但是一旦选择使用源码包的方式来安装,这一切就需要自己来完成了。针对MySQL数据库来讲,我们需要在系统中创建一个名为mysql的用户,专门用于负责运行MySQL数据库。请记得要把这类账户的Bash终端设置成nologin解释器,避免黑客通过该用户登录到服务器中,从而提高系统安全性。

1
2
[root@server222 cmake-2.8.11.2]# cd ..
[root@server222 src]# useradd mysql -s /sbin/nologin

​ 创建一个用于保存MySQL数据库程序和数据库文件的目录,并把该目录的所有者和所属组身份修改为mysql。其中,/usr/local/mysql是用于保存MySQL数据库服务程序的目录,/usr/local/mysql/var则是用于保存真实数据库文件的目录。

1
2
[root@server222 src]# mkdir -p /usr/local/mysql/var
[root@server222 src]# chown -Rf mysql:mysql /usr/local/mysql

​ 接下来解压、编译、安装MySQL数据库服务程序。在编译数据库时使用的是cmake命令

-DCMAKE_INSTALL_PREFIX参数用于定义数据库服务程序的保存目录

-DMYSQL_DATADIR参数用于定义真实数据库文件的目录

-DSYSCONFDIR则是定义MySQL数据库配置文件的保存目录

1
2
3
[root@server222 src]# tar xzvf mysql-5.6.19.tar.gz
[root@server222 src]# cd mysql-5.6.19/
[root@server222 mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc ; make ; make install

​ 为了让MySQL数据库程序正常运转起来,需要先删除/etc目录中的默认配置文件,然后在MySQL数据库程序的保存目录scripts内找到一个名为mysql_install_db的脚本程序,执行这个脚本程序并使用–user参数指定MySQL服务的对应账号名称(在前面步骤已经创建),使用–basedir参数指定MySQL服务程序的保存目录,使用–datadir参数指定MySQL真实数据库的文件保存目录,这样即可生成系统数据库文件,也会生成出新的MySQL服务配置文件。

1
2
3
[root@server222 mysql-5.6.19]# rm -rf /etc/my.cnf
[root@server222 mysql-5.6.19]# cd /usr/local/mysql
[root@server222 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

​ 把系统新生成的MySQL数据库配置文件链接到/etc目录中,然后把程序目录中的开机程序文件复制到/etc/rc.d/init.d目录中,以便通过service命令来管理MySQL数据库服务程序。记得把数据库脚本文件的权限修改成755以便于让用户有执行该脚本的权限

1
2
3
[root@server222 mysql]# ln -s my.cnf /etc/my.cnf
[root@server222 mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@server222 mysql]# chmod 755 /etc/rc.d/init.d/mysqld

​ 编辑刚复制的MySQL数据库脚本文件,把第46、47行的basedir与datadir参数分别修改为MySQL数据库程序的保存目录和真实数据库的文件内容。

1
2
3
4
5
[root@server222 mysql]# vim /etc/rc.d/init.d/mysqld
45
46 basedir=/usr/local/mysql
47 datadir=/usr/local/mysql/var
48

:set nu 查看行

​ 配置好脚本文件后便可以用service命令启动mysqld数据库服务了。mysqld是MySQL数据库程序的服务名称,注意不要写错。顺带再使用chkconfig命令把mysqld服务程序加入到开机启动项中。

1
2
3
[root@server222 mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@server222 mysql]# chkconfig mysqld on

​ MySQL数据库程序自带了许多命令,但是Bash终端的PATH变量并不会包含这些命令所存放的目录,因此我们也无法顺利地对MySQL数据库进行初始化,也就不能使用MySQL数据库自带的命令了。想要把命令所保存的目录永久性地定义到PATH变量中,需要编辑/etc/profile文件并写入追加的命令目录,这样当物理设备在下一次重启时就会永久生效了。如果不想通过重启设备的方式来生效,也可以使用source命令加载一下/ect/profile文件,此时新的PATH变量也可以立即生效了。

1
2
3
4
5
6
[root@server222 mysql]# vim /etc/profile
73 done
74 export PATH=$PATH:/usr/local/mysql/bin
75 unset i
76 unset -f pathmunge
[root@server222 mysql]# source /etc/profile

​ MySQL数据库服务程序还会调用到一些程序文件和函数库文件。由于当前是通过源码包方式安装MySQL数据库,因此现在也必须以手动方式把这些文件链接过来。

1
2
3
4
[root@server222 mysql]# mkdir /var/lib/mysql
[root@server222 mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@server222 mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@server222 mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

​ 现在,MySQL数据库服务程序已经启动,调用的各个函数文件已经就位,PATH环境变量中也加入了MySQL数据库命令的所在目录。接下来准备对MySQL数据库进行初始化

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
[root@server222 mysql]# mysql_secure_installation



NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y (要为root管理员设置数据库的密码)
New password: 输入要为root管理员设置的数据库密码
Re-enter new password: 再输入一次密码
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y (删除匿名账户)
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y (禁止root管理员从远程登录)
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y (删除test数据库并取消对其的访问权限)
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y (刷新授权表,让初始化后的设定立即生效)
... Success!




All done! If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

配置Nginx服务

​ 在正式安装Nginx服务程序之前,我们还需要为其解决相关的软件依赖关系,下面来解压、编译、生成、安装Nginx服务程序的源码文件:

1
2
3
4
[root@server222 mysql]# cd /usr/local/src
[root@server222 src]# tar xzvf pcre-8.35.tar.gz
[root@server222 src]# cd pcre-8.35
[root@server222 pcre-8.35]# ./configure --prefix=/usr/local/pcre ;make ; make install

​ openssl软件包是用于提供网站加密证书服务的程序文件,在安装该程序时需要自定义服务程序的安装目录,以便于稍后调用它们的时候更可控。

1
2
3
4
[root@server222 pcre-8.35]# cd /usr/local/src
[root@server222 src]# tar xzvf openssl-1.0.1h.tar.gz
[root@server222 src]# cd openssl-1.0.1h
[root@server222 openssl-1.0.1h]# ./config --prefix=/usr/local/openssl ; make ; make install

​ openssl软件包安装后默认会在/usr/local/openssl/bin目录中提供很多的可用命令,我们需要像前面的操作那样,将这个目录添加到PATH环境变量中,并写入到配置文件中,最后执行source命令以便让新的PATH环境变量内容可以立即生效:

1
2
3
4
5
6
[root@server222 openssl-1.0.1h]# vim /etc/profile
73 done
74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
75 unset i
76 unset -f pathmunge
[root@server222 openssl-1.0.1h]# source /etc/profile

使用:set nu 命令显示数字

​ zlib软件包是用于提供压缩功能的函数库文件。

1
2
3
4
[root@server222 openssl-1.0.1h]# cd /usr/local/src
[root@server222 src]# tar xzvf zlib-1.2.8.tar.gz
[root@server222 src]# cd zlib-1.2.8
[root@server222 zlib-1.2.8]# ./configure --prefix=/usr/local/zlib ; make ; make install

​ 在安装部署好具有依赖关系的软件包之后,创建一个用于执行Nginx服务程序的账户。账户名称可以自定义,但一定别忘记,因为在后续需要用到

1
2
[root@server222 zlib-1.2.8]# cd ..
[root@server222 src]# useradd www -s /sbin/nologin

​ 在使用命令编译Nginx服务程序时,需要设置特别多的参数

–prefix参数用于定义服务程序稍后安装到的位置

–user与–group参数用于指定执行Nginx服务程序的用户名和用户组

1
2
3
[root@server222 src]# tar xzvf nginx-1.6.0.tar.gz
[root@server222 src]# cd nginx-1.6.0/
[root@server222 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35 ; make ; make install

​ 要想启动Nginx服务程序以及将其加入到开机启动项中,也需要有脚本文件。可惜的是,在安装完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
[root@server222 nginx-1.6.0]# vim /etc/rc.d/init.d/nginx
[root@server222 nginx-1.6.0]# cat /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.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
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
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|configtest}"
exit 2
esac

保存脚本文件后记得为其赋予755权限,以便能够执行这个脚本

然后以绝对路径的方式执行这个脚本,通过restart参数重启Nginx服务程序

最后再使用chkconfig命令将Nginx服务程序添加至开机启动项中

1
2
3
4
5
[root@server222 nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx
[root@server222 nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
Reloading systemd: [ 确定 ]
Restarting nginx (via systemctl): [ 确定 ]
[root@server222 nginx-1.6.0]# chkconfig nginx on

Nginx服务程序在启动后就可以在浏览器中输入服务器的IP地址来查看到默认网页了。相较于Apache服务程序的红色默认页面,Nginx服务程序的默认页面显得更加简洁。

配置PHP服务

解决PHP的程序包和其他软件的依赖关系。

需要先安装部署将近十个用于搭建网站页面的软件程序包,然后才能正式安装PHP程序。

yasm libmcrypt libvpx tiff libpng freetype jpeg libgd t1lib

yasm源码包是一款常见的开源汇编器

1
2
3
4
[root@server222 nginx-1.6.0]# cd ..
[root@server222 src]# tar zxvf yasm-1.2.0.tar.gz
[root@server222 src]# cd yasm-1.2.0
[root@server222 yasm-1.2.0]# ./configure ; make ; make install

libmcrypt源码包是用于加密算法的扩展库程序

1
2
3
4
[root@server222 yasm-1.2.0]# cd ..
[root@server222 src]# tar zxvf libmcrypt-2.5.8.tar.gz
[root@server222 src]# cd libmcrypt-2.5.8
[root@server222 libmcrypt-2.5.8]# ./configure ; make ; make install

libvpx源码包是用于提供视频编码器的服务程序

1
2
3
4
[root@server222 libmcrypt-2.5.8]# cd ..
[root@server222 src]# tar xjvf libvpx-v1.3.0.tar.bz2
[root@server222 src]# cd libvpx-v1.3.0
[root@server222 libvpx-v1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 ; make ; make install

tiff源码包是用于提供标签图像文件格式的服务程序

1
2
3
4
[root@server222 libvpx-v1.3.0]# cd ..
[root@server222 src]# tar zxvf tiff-4.0.3.tar.gz
[root@server222 src]# cd tiff-4.0.3
[root@server222 tiff-4.0.3]# ./configure --prefix=/usr/local/tiff --enable-shared ; make ; make install

libpng源码包是用于提供png图片格式支持函数库的服务程序

1
2
3
4
[root@server222 tiff-4.0.3]# cd ..
[root@server222 src]# tar zxvf libpng-1.6.12.tar.gz
[root@server222 src]# cd libpng-1.6.12
[root@server222 libpng-1.6.12]# ./configure --prefix=/usr/local/libpng --enable-shared ;make ; make install

freetype源码包是用于提供字体支持引擎的服务程序

1
2
3
4
[root@server222 libpng-1.6.12]# cd ..
[root@server222 src]# tar zxvf freetype-2.5.3.tar.gz
[root@server222 src]# cd freetype-2.5.3
[root@server222 freetype-2.5.3]# ./configure --prefix=/usr/local/freetype --enable-shared ; make ; make install

jpeg源码包是用于提供jpeg图片格式支持函数库的服务程序

1
2
3
4
[root@server222 freetype-2.5.3]# cd ..
[root@server222 src]# tar zxvf jpegsrc.v9a.tar.gz
[root@server222 src]# cd jpeg-9a
[root@server222 jpeg-9a]# ./configure --prefix=/usr/local/jpeg --enable-shared ; make ; make install

libgd源码包是用于提供图形处理的服务程序

1
2
3
4
[root@server222 jpeg-9a]# cd ..
[root@server222 src]# tar zxvf libgd-2.1.0.tar.gz
[root@server222 src]# cd libgd-2.1.0
[root@server222 libgd-2.1.0]# ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx ; make ; make install

t1lib源码包是用于提供图片生成函数库的服务程序

1
2
3
4
5
6
[root@server222 libgd-2.1.0]# cd ..
[root@server222 src]# tar zxvf t1lib-5.1.2.tar.gz
[root@server222 src]# cd t1lib-5.1.2
[root@server222 t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared ; make ; make install
[root@server222 t1lib-5.1.2]# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so
[root@server222 t1lib-5.1.2]# cp -frp /usr/lib64/libXpm.so* /usr/lib/

​ 在开始编译php源码包之前,先定义一个名为LD_LIBRARY_PATH的全局环境变量,该环境变量的作用是帮助系统找到指定的动态链接库文件,这些文件是编译php服务源码包的必须元素之一。编译php服务源码包时,除了定义要安装到的目录以外,还需要依次定义配置php服务程序配置文件的保存目录、MySQL数据库服务程序所在目录、MySQL数据库服务程序配置文件所在目录,以及libpng、jpeg、freetype、libvpx、zlib、t1lib等服务程序的安装目录路径,并通过参数启动php服务程序的诸多默认功能

1
2
3
4
5
[root@server222 t1lib-5.1.2]# cd ..
[root@server222 src]# tar -zvxf php-5.5.14.tar.gz
[root@server222 src]# cd php-5.5.14
[root@server222 php-5.5.14]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
[root@server222 php-5.5.14]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype ; make ; make install

​ 在php源码包程序安装完成后,需要删除当前默认的配置文件,然后将php服务程序目录中相应的配置文件复制过来

1
2
3
4
5
[root@server222 php-5.5.14]# rm -rf /etc/php.ini
[root@server222 php-5.5.14]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[root@server222 php-5.5.14]# cp php.ini-production /usr/local/php/etc/php.ini
[root@server222 php-5.5.14]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@server222 php-5.5.14]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf

​ php-fpm.conf是php服务程序重要的配置文件之一,我们需要启用该配置文件中第25行左右的pid文件保存目录,然后分别将第148和149行的user与group参数分别修改为www账户和用户组名称

1
2
3
4
5
6
7
8
9
10
[root@server222 php-5.5.14]# vim /usr/local/php/etc/php-fpm.conf
24 ; Default Value: none
25 pid = run/php-fpm.pid
26


147 ; will be used.
148 user = www
149 group = www
150

:set nu 可查看行数

​ 配置妥当后便可把用于管理php服务的脚本文件复制到/etc/rc.d/init.d中了。为了能够执行脚本,请记得为脚本赋予755权限。最后把php-fpm服务程序加入到开机启动项中

1
2
3
[root@server222 php-5.5.14]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@server222 php-5.5.14]# chmod 755 /etc/rc.d/init.d/php-fpm
[root@server222 php-5.5.14]# chkconfig php-fpm on

​ 由于php服务程序的配置参数直接会影响到Web服务服务的运行环境,因此,如果默认开启了一些不必要且高危的功能(如允许用户在网页中执行Linux命令),则会降低网站被入侵的难度,入侵人员甚至可以拿到整台Web服务器的管理权限。因此我们需要编辑php.ini配置文件,在305行的disable_functions参数后面追加上要禁止的功能。

1
2
3
4
[root@server222 php-5.5.14]# vim /usr/local/php/etc/php.ini

304 ; http://php.net/disable-functions
305 disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restor e,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,g etservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,po six_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_ getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_ setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

​ 这样就把php服务程序配置妥当了。最后,还需要编辑Nginx服务程序的主配置文件,把第2行的井号(#)删除,然后在后面写上负责运行Nginx服务程序的账户名称和用户组名称;在第45行的index参数后面写上网站的首页名称。

​ 最后是将第65~71行参数前的井号(#)删除来启用参数,主要是修改第69行的脚本名称路径参数,其中$document_root变量即为网站信息存储的根目录路径,若没有设置该变量,则Nginx服务程序无法找到网站信息,因此会提示“404页面未找到”的报错信息。在确认参数信息填写正确后便可重启Nginx服务与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
[root@server222 php-5.5.14]# vim /usr/local/nginx/conf/nginx.conf
1
2 user www www;
3 worker_processes 1;
4

44 root html;
45 index index.html index.htm index.php forum.php;
46 }
47

64 #
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
70 include fastcgi_params;
71 }
72
73 # deny access to .htaccess files, if Apache's document root

[root@server222 php-5.5.14]# systemctl restart nginx
[root@server222 php-5.5.14]# systemctl restart php-fpm

从65行到71行的注释需要去掉

至此,LNMP动态网站环境架构的配置实验全部结束。

搭建Discuz!论坛

​ 为了检验LNMP动态网站环境是否配置妥当,可以使用在上面部署Discuz!系统,然后查看结果。如果能够在LNMP动态网站环境中成功安装使用Discuz!论坛系统,也就意味着这套架构是可用的。Discuz! X3.2是国内最常见的社区论坛系统,在经过十多年的研发后已经成为了全球成熟度最高、覆盖率最广的论坛网站系统之一。

​ Discuz! X3.2软件包的后缀是.zip格式,因此应当使用专用的unzip命令来进行解压。解压后会在当前目录中出现一个名为upload的文件目录,这里面保存的就是Discuz!论坛的系统程序。我们把Nginx服务程序网站根目录的内容清空后,就可以把这些这个目录中的文件都复制进去了。记得把Nginx服务程序的网站根目录的所有者和所属组修改为本地的www用户,并为其赋予755权限以便于能够读、写、执行该论坛系统内的文件。

1
2
3
4
5
6
[root@server222 php-5.5.14]# cd /usr/local/src/
[root@server222 src]# unzip Discuz_X3.2_SC_GBK.zip
[root@server222 src]# rm -rf /usr/local/nginx/html/{index.html,50x.html}*
[root@server222 src]# mv upload/* /usr/local/nginx/html/
[root@server222 src]# chown -Rf www:www /usr/local/nginx/html
[root@server222 src]# chmod -Rf 755 /usr/local/nginx/html

之后在浏览器输入192.168.176.222/install

依次按照傻瓜式步骤下一步

使用admin管理员账户登录

到此结束LNMP动态网站部署,如果您觉得不懂,可私信给我,后期将写一些部署docker容器虚拟化平台

打赏
  • © 2019-2020 Li Jie
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

喜欢就打赏吧~

支付宝
微信