LNMP搭建
配置本地yum源
安装软件包
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目录中统软件包
源码包文件已经放到百度云盘里面,下载下来传到Linux系统里面/usr/local/src目录即可
链接:https://pan.baidu.com/s/1X-eh_SRBbpXcVuxcu62VTA
提取码:ox0w
安装编译工具
cmake是Linux系统中一款常用的编译工具。
配置MySQL服务
在使用Yum软件仓库安装服务程序时,系统会自动根据RPM软件包中的指令集完整软件配置等工作。但是一旦选择使用源码包的方式来安装,这一切就需要自己来完成了。针对MySQL数据库来讲,我们需要在系统中创建一个名为mysql的用户,专门用于负责运行MySQL数据库。请记得要把这类账户的Bash终端设置成nologin解释器,避免黑客通过该用户登录到服务器中,从而提高系统安全性。
useradd mysql -s /sbin/nologin
创建一个用于保存MySQL数据库程序和数据库文件的目录,并把该目录的所有者和所属组身份修改为mysql。其中,/usr/local/mysql是用于保存MySQL数据库服务程序的目录,/usr/local/mysql/var则是用于保存真实数据库文件的目录。
mkdir -p /usr/local/mysql/var
chown -Rf mysql:mysql /usr/local/mysql
接下来解压、编译、安装MySQL数据库服务程序。在编译数据库时使用的是cmake命令
-DCMAKE_INSTALL_PREFIX参数用于定义数据库服务程序的保存目录
-DSYSCONFDIR则是定义MySQL数据库配置文件的保存目录
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服务配置文件
rm -rf /etc/my.cnf
[[email protected] mysql-5.6.19]# cd /usr/local/mysql
[[email protected] 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以便于让用户有执行该脚本的权限
[[email protected] mysql]# ln -s my.cnf /etc/my.cnf
[[email protected] mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[[email protected] mysql]# chmod 755 /etc/rc.d/iniit.d/mysql
编辑刚复制的MySQL数据库脚本文件,把第46、47行的basedir与datadir参数分别修改为MySQL数据库程序的保存目录和真实数据库的文件内容。
vim /etc/rc.d/init.d/mysqld
45
46 basedir=/usr/local/mysql
47 datadir=/usr/local/mysql/var
48
配置好脚本文件后便可以用service命令启动mysqld数据库服务了。mysqld是MySQL数据库程序的服务名称,注意不要写错。顺带再使用chkconfig命令把mysqld服务程序加入到开机启动项中
[email protected] mysql]# service mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql]# chkconfig mysqld on
MySQL数据库程序自带了许多命令,但是Bash终端的PATH变量并不会包含这些命令所存放的目录,因此我们也无法顺利地对MySQL数据库进行初始化,也就不能使用MySQL数据库自带的命令了。想要把命令所保存的目录永久性地定义到PATH变量中,需要编辑/etc/profile文件并写入追加的命令目录,这样当物理设备在下一次重启时就会永久生效了。如果不想通过重启设备的方式来生效,也可以使用source命令加载一下vim /etc/profile
73 done
74 export PATH=$PATH:/usr/local/mysql/bin
75 unset i
76 unset -f pathmunge
[[email protected] mysql]# source /etc/profile
MySQL数据库服务程序还会调用到一些程序文件和函数库文件。由于当前是通过源码包方式安装MySQL数据库,因此现在也必须以手动方式把这些文件链接过来。
mkdir /var/lib/mysql
[[email protected] mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[[email protected] mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[[email protected] mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
现在,MySQL数据库服务程序已经启动,调用的各个函数文件已经就位,PATH环境变量中也加入了MySQL数据库命令的所在目录。接下来准备对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的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法
tar xzvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre
make && make install
nginx的各种模块中需要使用gzip压缩
tar xzvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
安全套接字层密码库:
tar xzvf openssl-1.0.1h.tar.gz
./config --prefix=/usr/local/openssl
openssl软件包安装后默认会在/usr/local/openssl/bin目录中提供很多的可用命令,我们需要像前面的操作那样,将这个目录添加到PATH环境变量中,并写入到配置文件中,最后执行source命令以便让新的PATH环境变量内容可以立即生效
vim /etc/profile
73 done
74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
75 unset i
76 unset -f pathmunge
source /etc/profile
下载nginx包并解压(到/usr/local/src目录中)
tar -zxvf nginx-1.6.1tar.gz
编译安装(到/data/server目录中)
./configure --prefix=/data/server
make && make install
创建一个用于执行Nginx服务程序的账户。账户名称可以自定义,但一定别忘记,因为在后续需要用到
cd …
useradd www -s /sbin/nologin
在使用命令编译Nginx服务程序时,需要设置特别多的参数
–prefix参数用于定义服务程序稍后安装到的位置
cd nginx-1.6.0/
tar xzvf nginx-1.6.0.tar.gz
cd nginx-1.6.0/
./configure --prefix=/data/server/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
要想启动Nginx服务程序以及将其加入到开机启动项中,也需要有脚本文件。可惜的是,在安装完Nginx软件包之后默认并没有为用户提供脚本文件 下面东西复制进去
vim /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.
[ “KaTeX parse error: Expected 'EOF', got '&' at position 22: …KING" = "no" ] &̲& exit 0
nginx=…(basename KaTeX parse error: Expected 'EOF', got '&' at position 87: …config/nginx ] &̲& . /etc/syscon…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 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 ?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc ?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc ?
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服务程序添加至开机启动项中
chmod 755 /etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx restart
Reloading systemd: [ 确定 ]
Restarting nginx (via systemctl): [ 确定 ]
chkconfig nginx
Nginx服务程序在启动后就可以在浏览器中输入服务器的IP地址来查看到默认网页了。相较于Apache服务程序的红色默认页面,Nginx服务程序的默认页面显得更加简洁。
配置PHP服务
解决PHP的程序包和其他软件的依赖关系。
需要先安装部署将近十个用于搭建网站页面的软件程序包,然后才能正式安装PHP程序
yasm源码包是一款常见的开源汇编器
cd …
[[email protected] src]# tar zxvf yasm-1.2.0.tar.gz
[[email protected] src]# cd yasm-1.2.0
[[email protected] yasm-1.2.0]# ./configure
make && make install
libmcrypt源码包是用于加密算法的扩展库程序
cd …
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
libvpx源码包是用于提供视频编码器的服务程序
cd …
tar xjvf libvpx-v1.3.0.tar.bz2
cd libvpx-v1.3.0
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
make && make install
tiff源码包是用于提供标签图像文件格式的服务程序
[[email protected] libvpx-v1.3.0]# cd …
[[email protected] src]# tar zxvf tiff-4.0.3.tar.gz
[[email protected] src]# cd tiff-4.0.3
[[email protected] tiff-4.0.3]# ./configure --prefix=/usr/local/tiff --enable-shared
make && make install
png源码包是用于提供png图片格式支持函数库的服务程序
[email protected] tiff-4.0.3]# cd …
[[email protected] src]# tar zxvf libpng-1.6.12.tar.gz
[[email protected] src]# cd libpng-1.6.12
[[email protected] libpng-1.6.12]# ./configure --prefix=/usr/local/libpng --enable-shared
make && make install
freetype源码包是用于提供字体支持引擎的服务程序
[[email protected] libpng-1.6.12]# cd …
[[email protected] src]# tar zxvf freetype-2.5.3.tar.gz
[[email protected] src]# cd freetype-2.5.3
[[email protected] freetype-2.5.3]# ./configure --prefix=/usr/local/freetype --enable-shared
make && make install
jpeg源码包是用于提供jpeg图片格式支持函数库的服务程序
[[email protected] freetype-2.5.3]# cd …
[[email protected] src]# tar zxvf jpegsrc.v9a.tar.gz
[[email protected] src]# cd jpeg-9a
[[email protected] jpeg-9a]# ./configure --prefix=/usr/local/jpeg --enable-shared make && make install
libgd源码包是用于提供图形处理的服务程序
[[email protected] jpeg-9a]# cd …
[[email protected] src]# tar zxvf libgd-2.1.0.tar.gz
[[email protected] src]# cd libgd-2.1.0
[[email protected] 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源码包是用于提供图片生成函数库的服务程序
[[email protected] libgd-2.1.0]# cd …
[[email protected] src]# tar zxvf t1lib-5.1.2.tar.gz
[[email protected] src]# cd t1lib-5.1.2
[[email protected] t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared
make && make install
[[email protected] t1lib-5.1.2]# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so
[[email protected] 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服务程序的诸多默认功能
cd …
[[email protected] src]# tar -zvxf php-5.5.14.tar.gz
[[email protected] src]# cd php-5.5.14
[[email protected] php-5.5.14]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
[[email protected] php-5.5.14]# ./configure --prefix=/data/server/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
在php源码包程序安装完成后,需要删除当前默认的配置文件,然后将php服务程序目录中相应的配置文件复制过来
rm -rf /etc/php.ini
[[email protected] php-5.5.14]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[[email protected] php-5.5.14]# cp php.ini-production /usr/local/php/etc/php.ini
[[email protected] php-5.5.14]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] 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账户和用户组名称
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
配置妥当后便可把用于管理php服务的脚本文件复制到/etc/rc.d/init.d中了。为了能够执行脚本,请记得为脚本赋予755权限。最后把php-fpm服务程序加入到开机启动项中
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod 755 /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
由于php服务程序的配置参数直接会影响到Web服务服务的运行环境,因此,如果默认开启了一些不必要且高危的功能(如允许用户在网页中执行Linux命令),则会降低网站被入侵的难度,入侵人员甚至可以拿到整台Web服务器的管理权限。因此我们需要编辑php.ini配置文件,在305行的disable_functions参数后面追加上要禁止的功能。
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服务。
[[email protected] 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 fastcgi_script_name;
70 include fastcgi_params;
71 }
72
73 # deny access to .htaccess files, if Apache’s document root
[[email protected] php-5.5.14]# systemctl restart nginx
[[email protected] php-5.5.14]# systemctl restart php-fqm
至此,LNMP动态网站环境架构的配置实验全部结束
搭建Discuz!论坛
为了检验LNMP动态网站环境是否配置妥当,可以使用在上面部署Discuz!系统,然后查看结果。如果能够在LNMP动态网站环境中成功安装使用Discuz!论坛系统,也就意味着这套架构是可用的。Discuz! X3.2是国内最常见的社区论坛系统,在经过十多年的研发后已经成为了全球成熟度最高、覆盖率最广的论坛网站系统之一。
Discuz! X3.2软件包的后缀是.zip格式,因此应当使用专用的unzip命令来进行解压。解压后会在当前目录中出现一个名为upload的文件目录,这里面保存的就是Discuz!论坛的系统程序。我们把Nginx服务程序网站根目录的内容清空后,就可以把这些这个目录中的文件都复制进去了。记得把Nginx服务程序的网站根目录的所有者和所属组修改为本地的www用户,并为其赋予755权限以便于能够读、写、执行该论坛系统内的文件。
[[email protected] php-5.5.14]# cd /usr/local/src/
[[email protected] src]# unzip Discuz_X3.2_SC_GBK.zip
[[email protected] src]# rm -rf /usr/local/nginx/html/{index.html,50x.html}*
[[email protected] src]# mv upload/* /usr/local/nginx/html/
[[email protected] src]# chown -Rf www:www /usr/local/nginx/html
[[email protected] src]# chmod -Rf 755 /usr/local/nginx/html
傻瓜式步骤
将<?php
phpinfo();
?>写人nginx网页服务配置里面
vi /usr/local/nginx/html/nima.php
Nginx服务程序在启动后就可以在浏览器中输入服务器的IP地址来查看