使用源代码搭建LNMP动态网站

想搭建一个练习平台,之前一直使用的是LAMP环境搭建的,使用的是yum仓库。这次想尝试使用LNMP来搭建,利用源代码安装,熟悉一下Nginx。

0x00 更换yum源为阿里云源

1).备份原来的源

# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2).下载新的源到/etc/yum.repos.d/目录下

# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3)生成缓存

# yum makecache

0x01 安装编译程序源码的环境

# 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

0x02 下载搭建LNMP所需的软件包

# cd /usr/local/src

通过百度云链接下载,将软件包放到该目录下

0x03 安装CMake

1).解压tar包

# tar xzvf cmake-2.8.11.2.tar.gz   

2).进入cmake-2.8.11.2目录,并执行安装

# cd cmake-2.8.11.2/                       
# ./configure                               

3).编译源代码生成可执行文件

# make  

4).安装编译成功的可执行文件,一般目录在/usr/local/bin下

# make install                             

CMake是Linux系统下常用的编译工具,因为使用源码包安装编译时需要用到,所以先安装,通常源码安装四个步骤

1).下载及解压源码包文件
2).编译源码包代码
3).生成二进制安装程序
4).运行二进制的服务程序安装包

0x04 安装MYSQL数据库

在系统中创建一个名为sqladmin的用户,专门用于负责运行MySQL数据库。并把该账户的Bash终端设置成nologin解释器,提高系统的安全性。

回到/usr/local/src目录下

1).添加本地用户sqladmin,设置shell为nologin

# useradd mysql -s /sbin/nologin

2).创建目录用于保存MySQL数据库有关文件

# mkdir -p /usr/local/mysql/var

3).将该目录的所有者和所属组身份修改为sqladmin

# chown -Rf mysql:mysql /usr/local/mysql

4).解压MySQL源代码tar包

# tar xzvf mysql-5.6.19.tar.gz

5).编译数据库

# cd mysql-5.6.19/
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc

各参数意义:
-DCMAKE_INSTALL_PREFIX    用于定义数据库服务程序的保存目录
-DMYSQL_DATADIR           用于定义真实数据库文件的目录
-DSYSCONFDIR              定义MySQL数据库配置文件的保存目录

6).编译源代码生成二可执行文件

# make

7).安装可执行程序

# make install

8).删除/etc目录中的mysql的默认配置文件

# rm -rf /etc/my.cnf

9).进入数据库服务程序的保存目录

在scripts内找到一个名为mysql_install_db的脚本程序,并执行这个脚本程序

# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=sqladmin --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

10).新生成的MySQL数据库配置文件链接到/etc目录中

# ln -s my.cnf /etc/my.cnf

11).程序目录中的开机程序文件复制到/etc/rc.d/init.d目录中

#  cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

12).修改数据库脚本权限

# chmod 755 /etc/rc.d/init.d/mysqld

13).修改MySQL数据库的脚本文件

# vi /etc/rc.d/init.d/mysqld 

命令模式输入”:set nu”显示所有行数,填入信息

 46 basedir=/usr/local/mysql
 47 datadir=/usr/local/mysql/var
#保存退出

14).启动数据库

# service mysqld start

启动数据库出现问题:

Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/var/localhost.localdomain.pid).

解决过程:

刚开始我以为是没有localhost.localdomain.pid,所以我建了这个文件,然后发现当我想启动mysql时,新建的.pid文件就消失了
卧榻,网上Google了半天各种办法都试过了也没解决,回头看到一个localhost.localdomain.err文件,打开看到一行关键信息
119563 [ERROR] Fatal error: Can't change to run as user 'mysql' ;  Please check that the user exists!
原来是没有mysql这个用户,赶紧把之前建的sqladmin用户删除了,新建了mysql用户,并重修修改了mysql文件的所有者和权限
并且把前面的步骤内容更改了一下,但是问题记录了下来,localhost.localdomain.err文件路径:/usr/local/mysql/var/localhost.localdomain.err

解决问题的步骤:

# userdel sqladmin(之前新建的sqlamdin用户)
# useradd mysql -s /sbin/nologin
# chown -Rf mysql:mysql /usr/local/mysql
# service mysqld start
#然后数据库启动成功,可以进行下一步安装

15.将MySQL服务设置开机启动

# chkconfig mysqld on

16.编辑/etc/profile文件将命令所保存的目录永久性地定义到PATH变量

mysql数据库自带了许多命令,但是Bash终端的PATH变量并不包含这些命令所在的目录,所以需要自己添加,这样当物理设备在下一次重启时就会永久生效了。使用source命令加载一下/ect/profile文件,此时新的PATH变量也可以立即生效了。

# vi /etc/profile

使用”:set nu”命令查看所有行数,在47行添加

export PATH=$PATH:/usr/local/mysql/bin

# source /etc/profile

17.初始化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: 输入密码
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...

0x05 配置Nginx服务

安装Nginx所需的依赖包

1.安装pcre包

软件包pcre是Nginx服务程序用于实现伪静态功能必不可少的依赖包

1).进入/usr/local/src目录,解压tar包

# tar xzvf pcre-8.35.tar.gz 

2).进入pcre目录,编译

# cd pcre-8.35
# ./configure --prefix=/usr/local/pcre

3).生成可执行文件

# make

4).执行

# make install
2.安装openssl包

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

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

配置openssl的配置文件,将命令目录添加到PATH环境变量中去,使用source命令让它立即生效

# vi /etc/profile

# source /etc/profile
3.安装zlib包

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服务程序的账号

# cd /usr/local/src
# useradd www -s /sbin/nologin

安装Nginx服务器

# 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

参数意义:

  • –prefix参数用于定义服务程序稍后安装到的位置
  • –user与–group参数用于指定执行Nginx服务程序的用户名和用户组
  • 在使用参数调用openssl、zlib、pcre软件包时,写出软件源码包的解压路径,而不是程序的安装路径

将Nginx设置到开机启动项中

# vi /etc/rc.d/init.d/nginx

配置脚本文件(因为没有nginx这个文件,直接copy就好)

#!/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权限,然后使用chkconfig命令添加服务到开机启动项

# chmod 755 /etc/rc.d/init.d/nginx
# /etc/rc.d/init.d/nginx restart    //以绝对路径执行该脚本
状态:
Reloading systemd:                                         [  确定  ]
Restarting nginx (via systemctl):                          [  确定  ]
在nginx-1.6.0目录下
root@localhost nginx-1.6.0]# chkconfig nginx on

查看是否安装成功

# elinks 服务器ip

0x06 配置PHP服务

php具有开源、免费、快捷、跨平台性强、效率高等优良特性,是目前Web开发领域最常用的语言之一,安装php比安装Nginx还要复杂,得先安装很多依赖包,所以用源码包安装LNMP环境特别耗费时间

安装依赖包

1.安装yasm包

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

# cd /usr/local/src
# tar zxvf yasm-1.2.0.tar.gz
# cd yasm-1.2.0
# ./configure
# make
# make install
2.安装libmcrypt包

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

# cd /usr/local/src
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make
# make install
3.安装libvpx包

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

# 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
4.安装tiff包

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

# tar zxvf tiff-4.0.3.tar.gz
# cd tiff-4.0.3
# ./configure --prefix=/usr/local/tiff --enable-shared
# make
# make install
5.安装libpng包

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

# 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
6.安装freetype包

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

# 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
7.安装jpeg包

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

# cd /usr/local/src
# tar zxvf jpegsrc.v9a.tar.gz
# cd jpeg-9a
# ./configure --prefix=/usr/local/jpeg --enable-shared
# mkae
# make install
8.安装libgd包

libgd源码包是用于提供图形处理的服务程序,编译时要写入jpeg、libpng、freetype、tiff、libvpx等服务程序在系统中的安装路径

# cd ..
# tar zxvf libgd-2.1.0.tar.gz
# ./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
9.安装t1lib包

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

# 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/

安装php服务

在开始编译php源码包之前,先定义一个名为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配置文件

# 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-fpm.conf文件

分别将第148和149行的user与group参数分别修改为www账户和用户组名称

# vi /usr/local/php/etc/php-fpm.conf

把php-fpm服务程序加入到开机启动项

为了能够执行脚本,为脚本赋予755权限。

# 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参数后面追加上要禁止的功能

# vi /usr/local/php/etc/php.ini

0x07 修改Nginx配置文件

# vi /usr/local/nginx/conf/nginx.conf

把第2行的注释删除,然后在后面写上负责运行Nginx服务程序的账户名称和用户组名称

在第45行的index参数后面写上网站的首页名称

将第65~71行参数前的注释删除来启用参数,主要是修改第69行的脚本名称路径参数,其中$document_root变量即为网站信息存储的根目录路径,若没有设置该变量,则Nginx服务程序无法找到网站信息,因此会提示“404页面未找到”的报错信息

在确认参数信息填写正确后便可重启Nginx服务与php-fpm服务

0x08 重启服务

# systemctl restart nginx
# systemctl restart php-fpm

到了这一步LNMP环境就搭建完成了

0x09 测试

为了测试LNMP环境搭建是否妥当,部署Discuz!系统

1).解压dz

# cd /usr/local/src/
# unzip Discuz_X3.2_SC_GBK.zip

2).将文件放在服务器目录

解压后会在当前目录中出现一个名为upload的文件目录,这里面保存的就是Discuz!论坛的系统程序。把Nginx服务程序网站根目录的内容清空后,就可以把这些这个目录中的文件都复制进去

# # rm -rf /usr/local/nginx/html/{index.html,50x.html}*
# mv upload/* /usr/local/nginx/html/

3).修改属主和权限

把Nginx服务程序的网站根目录的所有者和所属组修改为本地的www用户,并赋予755权限以便于能够读、写、执行该论坛系统内的文件

# chown -Rf www:www /usr/local/nginx/html
# chmod -Rf 755 /usr/local/nginx/html

4).在浏览器输入http://服务器ip/install/,开始安装

5).确保当前状态都是可写,然后进行下一步

6).选择“全新安装Discuz! X(含UCenter Server)”,后面填写要求的信息

7).安装成功,说明

既然环境没有问题.接下就是搭建练习平台了。

-------------本文结束感谢您的阅读-------------

本文标题:使用源代码搭建LNMP动态网站

文章作者:Peithon

发布时间:2018年05月28日 - 17:05

最后更新:2018年10月13日 - 08:10

原始链接:https://peithon.github.io/2018/05/28/lnmp-setup/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。