使用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架構搭建完成,同時也驗證了其可用性。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章