使用LNMP架构部署动态网站环境

学习总结所用《 Linux就该这么学 》
原文在我搭建的个人博客

LNMP动态软件架构

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

​ 本实验将采用源码包方式安装服务程序,然后使用Discuz! X3.2版本论坛系统验证架构环境 。

源码包安装的优势:

源码包的可移植性非常好,几乎可以在任何Linux系统中安装使用,而RPM软件包是针对特定系统和架构编写的指令集,必须严格地符合执行环境才能顺利安装(即只会去“生硬地”安装服务程序)。

使用源码包安装服务程序时会有一个编译过程,因此可以更好地适应安装主机的系统环境,运行效率和优化程度都会强于使用RPM软件包安装的服务程序。也就是说,可以将采用源码包安装服务程序的方式看作是针对系统的“量体裁衣”。

配置Yum软件仓库

把光盘设备中的系统镜像挂载到/media/cdrom目录

# mkdir -p /media/cdrom
# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only

创建Yum仓库的配置文件

# vim /etc/yum.repos.d/rhel7.repo
[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0

安装编译程序源码的环境,需具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序

# 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
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
......
Complete!

将源码包发送到指定目录,rz后选择所需要的源码包即可发送到所在目录,若没有可先执行yum install -y lrzsz命令安装(LNMP所需软件源码包与Discuz!软件包 提取码:1234

# cd /usr/local/src
# rz

源码包安装Linux系统中一款常用的编译工具 CMake

# cd /usr/local/src
# tar xzvf cmake-2.8.11.2.tar.gz
# cd cmake-2.8.11.2/
# ./configure , make , make install

配置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数据库服务程序

# cd /usr/local/src
# tar xzvf mysql-5.6.19.tar.gz
# cd mysql-5.6.19/
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc;make;make install;make celan
参数说明
- DCMAKE_INSTALL_PREFIX用于定义数据库服务程序的保存目录
- DMYSQL_DATADIR用于定义真实数据库文件的目录
- DSYSCONFDIR定义MySQL数据库配置文件的保存目录

为使MySQL数据库程序正常运转,先删除/etc目录中的默认配置文件,然后执行在MySQL数据库程序的保存目录scripts下名为mysql_install_db的脚本程序

# rm -rf /etc/my.cnf
# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
命令/参数说明
./scripts/mysql_install_db生成系统数据库文件、新的MySQL服务配置文件
–user指定MySQL服务的对应账号名称
–basedir指定MySQL服务程序的保存目录
–datadir指定MySQL真实数据库的文件保存目录

将系统新生成的MySQL数据库配置文件链接到/etc目录中

将程序目录中的开机程序文件复制到/etc/rc.d/init.d目录中,以便通过service命令来管理MySQL数据库服务程序

将数据库脚本文件的权限修改成755以便于让用户有执行该脚本的权限

# cd /usr/local/mysql
# ln my.cnf /etc
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld

编辑MySQL数据库脚本文件 ,basedir与datadir参数分别修改为MySQL数据库程序的保存目录和真实数据库的文件内容

# vim /etc/rc.d/init.d/mysqld
......
basedir=/usr/local/mysql
datadir=/usr/local/mysql/var
......

启动mysqld数据库服务,加入开机启动项

# service mysqld start
Starting MySQL... SUCCESS! 
# chkconfig mysqld on

手动方式将MySQL数据库服务程序需调用到的一些程序文件和函数库文件链接过来 ( 为兼容32位和64位平台 )

# mkdir /var/lib/mysql
# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
# ln -s /usr/local/mysql/include/mysql /usr/include/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...

**注意:**源码包安装时突然中断,需先执行命令make clean清理源码包临时文件 ,然后再 make;make install

配置Nginx服务

安装pcre软件包,用于提供Perl语言兼容的正则表达式库,Nginx服务程序用于实现伪静态功能必不可少的依赖包

# cd /usr/local/src
# tar xzvf pcre-8.35.tar.gz 
# cd pcre-8.35
# ./configure --prefix=/usr/local/pcre
# make;make install;make clean

安装openssl软件包,用于提供网站加密证书服务的程序文件

# cd /usr/local/src
# tar xzvf openssl-1.0.1h.tar.gz
# cd openssl-1.0.1h
# ./config --prefix=/usr/local/openssl;make;make install

添加/usr/local/openssl/bin到PATH环境变量,并写入配置文件 , source /etc/profile命令使环境变量立即生效

# vim /etc/profile
......
done
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
unset i
unset -f pathmunge
# source /etc/profile

安装zlib软件包,用于提供压缩功能的函数库文件

# cd /usr/local/src
# tar xzvf zlib-1.2.8.tar.gz 
# cd zlib-1.2.8
# ./configure --prefix=/usr/local/zlib; make ; make install

创建一个用于执行Nginx服务程序的账户

# useradd www -s /sbin/nologin

安装Nginx服务程序

# cd /usr/local/src
# tar xzvf nginx-1.6.0.tar.gz 
# cd 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
参数说明
–user指定执行Nginx服务程序的用户名
–group指定执行Nginx服务程序的用户组
–with-openssl调用openssl软件包,指的是软件源码包的解压路径

启动Nginx服务程序( 在/etc/rc.d/init.d目录中创建启动脚本文件 )

# 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.
[ "$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服务程序 ,加入到开机启动项

# chmod 755 /etc/rc.d/init.d/nginx 
# /etc/rc.d/init.d/nginx restart
Restarting nginx (via systemctl):                          [  OK  ]
# chkconfig nginx on

浏览器访问服务器的IP地址看到 **Welcome to nginx!**标题的网页

配置PHP服务

安装汇编器yasm

# cd /usr/local/src
# tar xvzf yasm-1.2.0.tar.gz
# cd yasm-1.2.0/
# ./configure ; make ; make install

安装用于加密算法的扩展库程序 libmcrypt-2.5.8.tar.gz

# cd /usr/local/src
# tar xvzf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure ; make ; make install

安装用于提供视频编码器的服务程序 libvpx-v1.3.0**.tar.bz2** 源码包,解压的参数为 xjvf

# cd /usr/local/src
# 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-4.0.3.tar.gz 源码包

# cd /usr/local/src
# tar zxvf tiff-4.0.3.tar.gz
# cd tiff-4.0.3
# ./configure --prefix=/usr/local/tiff --enable-shared;make;make install

安装用于提供png图片格式支持函数库的服务程序 libpng-1.6.12.tar.gz 源码包

# cd /usr/local/src
# tar zxvf libpng-1.6.12.tar.gz
# cd libpng-1.6.12
# ./configure --prefix=/usr/local/libpng --enable-shared;make;make install

安装用于用于提供字体支持引擎的服务程序 freetype-2.5.3.tar.gz 源码包

# cd /usr/local/src
# tar zxvf freetype-2.5.3.tar.gz
# cd freetype-2.5.3
# ./configure --prefix=/usr/local/freetype --enable-shared;make;make install

安装用于提供jpeg图片格式支持函数库的服务程序 jpegsrc.v9a.tar.gz 源码包

# cd /usr/local/src
# tar zxvf jpegsrc.v9a.tar.gz
# cd jpeg-9a
# ./configure --prefix=/usr/local/jpeg --enable-shared;make;make install

安装用于提供图形处理的服务程序 libgd-2.1.0.tar.gz 源码包

# cd ..
# tar zxvf libgd-2.1.0.tar.gz
# cd 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-5.1.2.tar.gz 源码包

将/usr/lib64目录中的函数文件链接到/usr/lib目录中,以便系统能够顺利调取到函数文件 (兼容32位系统)

# cd ..
# tar zxvf t1lib-5.1.2.tar.gz
# cd t1lib-5.1.2
# ./configure --prefix=/usr/local/t1lib --enable-shared;make;make install
# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so 
# cp -frp /usr/lib64/libXpm.so* /usr/lib/

定义一个名为LD_LIBRARY_PATH的全局环境变量,帮助系统找到指定的动态链接库文件——编译php服务源码包的必须元素之一

编译php服务源码包时,除了定义要安装到的目录以外,还需要依次定义配置php服务程序配置文件的保存目录、MySQL数据库服务程序所在目录、MySQL数据库服务程序配置文件所在目录,以及libpng、jpeg、freetype、libvpx、zlib、t1lib等服务程序的安装目录路径,并通过参数启动php服务程序的诸多默认功能

# cd ..
# tar -zvxf php-5.5.14.tar.gz
# cd php-5.5.14
# export LD_LIBRARY_PATH=/usr/local/libgd/lib
# ./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服务程序目录中相应的配置文件复制过来

# rm -rf /etc/php.ini 
# ln -s /usr/local/php/etc/php.ini /etc/php.ini
# cp php.ini-production /usr/local/php/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf

编辑 php 主配置文件 php-fpm.conf ( :set nu 查看文件的行号)

第25行:启用该配置文件中的pid文件保存目录
第148行:user参数修改为www账户
第149行:group参数修改为www用户组名称

 # vim /usr/local/php/etc/php-fp
 ......
 25 pid = run/php-fpm.pid
 ......
148 user = www
149 group = www
 ......

将用于管理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.ini配置文件,在305行的disable_functions参数后面追加上要禁止的功能

解释:

php服务程序的配置参数直接会影响到Web服务服务的运行环境,如果默认开启一些不必要且高危的功能(如允许用户在网页中执行Linux命令),则会降低网站被入侵的难度,入侵者甚至可拿到整台Web服务器的管理权限

# vim /usr/local/php/etc/php.ini
......
305 disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_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
......

编辑Nginx服务程序的主配置文件,在确认参数信息填写正确后便可重启Nginx服务与php-fpm服务

第2行: 负责运行Nginx服务程序的账户名称和用户组名称;
第45行:index参数后面是网站的首页名称
第65~71行:修改第69行的脚本名称路径参数,$document_root变量即为网站信息存储的根目录路径,若没有设置该变量,则Nginx服务程序无法找到网站信息,会提示“404页面未找到”的报错信息

# vim /usr/local/nginx/conf/nginx.conf
  1 
  2        user  www www;
   ......
 43         location / {
 44             root   html;
 45             index  index.html index.htm index.php forum.php;
 46         }
   ......
 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         }
 
# systemctl restart nginx
# systemctl restart php-fpm

搭建Discuz论坛

unzip命令解压Discuz_X3.2_SC_GBK.zip
将Nginx服务程序网站根目录的内容清空
复制 Discuz!论坛的系统程序即upload目录下的文件 到Nginx服务程序网站根目录
将Nginx服务程序的网站根目录的所有者和所属组修改为本地的www用户,赋予755权限以便于能够读、写、执行该论坛系统内的文件

# cd /usr/local/src/
# unzip Discuz_X3.2_SC_GBK.zip
# rm -rf /usr/local/nginx/html/*
# mv upload/* /usr/local/nginx/html/
# chown -Rf www:www /usr/local/nginx/html
# chmod -Rf 755 /usr/local/nginx/html
  1. 浏览器访问服务器IP地址,接受Discuz!安装向导的许可协议

在这里插入图片描述

  1. 检查Discuz! X3.2论坛系统的安装环境及目录权限 (必须确保状态都是可写!)

在这里插入图片描述

  1. 选择“全新安装Discuz! X(含UCenter Server)”

    在这里插入图片描述

  2. 填写服务器的数据库信息与论坛系统管理员信息

在这里插入图片描述
5. 等待Discuz! X3.2论坛系统安装完毕

在这里插入图片描述

在这里插入图片描述

  1. 成功安装Discuz! X3.2论坛系统,欢迎界面

    在这里插入图片描述

至此,整个LNMP架构搭建完成,同时也验证了其可用性。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值