1. 程式人生 > >Linux基礎學習-LNMP環境搭建

Linux基礎學習-LNMP環境搭建

LNMP環境搭建

LNMP動態網站部署架構是一套由Linux+Nginx+Mysql+PHP元件的動態網站系統解決方案,具有免費、高效、擴充套件性強且資源消耗低等特性.

LNMP動態網站架構

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

 cd /usr/local/src

 wget https://www.linuxprobe
.com/Software/cmake-2.8.11.2.tar.gz wget https://www.linuxprobe.com/Software/Discuz_X3.2_SC_GBK.zip wget https://www.linuxprobe.com/Software/freetype-2.5.3.tar.gz wget https://www.linuxprobe.com/Software/jpegsrc.v9a.tar.gz wget https://www.linuxprobe.com/Software/libgd-2.1.0.tar.gz wget https://www.linuxprobe.com/Software/libmcrypt-2.5
.8.tar.gz wget https://www.linuxprobe.com/Software/libpng-1.6.12.tar.gz wget https://www.linuxprobe.com/Software/libvpx-v1.3.0.tar.bz2 wget https://www.linuxprobe.com/Software/mysql-5.6.19.tar.gz wget https://www.linuxprobe.com/Software/nginx-1.6.0.tar.gz wget https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
wget https://www.linuxprobe.com/Software/php-5.5.14.tar.gz wget https://www.linuxprobe.com/Software/pcre-8.35.tar.gz wget https://www.linuxprobe.com/Software/t1lib-5.1.2.tar.gz wget https://www.linuxprobe.com/Software/tiff-4.0.3.tar.gz wget https://www.linuxprobe.com/Software/yasm-1.2.0.tar.gz wget https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
[[email protected] src]# ls
cmake-2.8.11.2          libgd-2.1.0.tar.gz      nginx-1.6.0.tar.gz     tiff-4.0.3.tar.gz
cmake-2.8.11.2.tar.gz   libmcrypt-2.5.8.tar.gz  openssl-1.0.1h.tar.gz  yasm-1.2.0.tar.gz
Discuz_X3.2_SC_GBK.zip  libpng-1.6.12.tar.gz    pcre-8.35.tar.gz       zlib-1.2.8.tar.gz
freetype-2.5.3.tar.gz   libvpx-v1.3.0.tar.bz2   php-5.5.14.tar.gz
jpegsrc.v9a.tar.gz      mysql-5.6.19.tar.gz     t1lib-5.1.2.tar.gz

[[email protected] cmake-2.8.11.2]# tar -xzvf cmake-2.8.11.2.tar.gz 
[[email protected] cmake-2.8.11.2]# cd cmake-2.8.11.2/
[[email protected] cmake-2.8.11.2]# ./configure 
[[email protected] cmake-2.8.11.2]# make && make install
[[email protected] cmake-2.8.11.2]# echo $?
0

配置MySQL服務

引數 解釋
-DCMAKE_INSTALL_PREFIX 引數用於定義資料庫服務程式的儲存目錄
-DMYSQL_DATADIR 引數用於定義真實資料庫檔案的目錄
-DSYSCONFDIR 引數定義MySQL資料庫配置檔案的儲存目錄
–user 引數指定MySQL服務的對應賬號名稱
–basedir 引數指定MySQL服務程式的儲存目錄
–datadir 引數指定MySQL真實資料庫的檔案儲存目錄
[root@mail src]# tar -xzvf mysql-5.6.19.tar.gz 
[root@mail src]# cd mysql-5.6.19/
[root@mail mysql-5.6.19]# 
[root@mail mysql-5.6.19]# useradd mysql -s /sbin/nologin -M 
[root@mail mysql-5.6.19]# mkdir -p /usr/local/mysql/var
[root@mail mysql-5.6.19]# chown -Rf mysql:mysql /usr/local/mysql

[root@mail mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
[root@mail mysql-5.6.19]# make && make install
[root@mail mysql-5.6.19]# echo $?
0

[root@mail mysql-5.6.19]# rm -rf /etc/my.cnf

[root@mail mysql-5.6.19]# cd /usr/local/mysql/
[root@mail mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

[root@mail mysql]# ln -s my.cnf /etc/my.cnf
[root@mail mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@mail mysql]# vim /etc/rc.d/init.d/mysqld
 46 basedir=/usr/local/mysql
 47 datadir=/usr/local/mysql/var

[root@mail mysql]# service mysqld start
Starting MySQL............ SUCCESS! 
[root@mail mysql]# chkconfig mysqld on

[root@mail mysql]# vim /etc/profile
 74 export PATH=$PATH:/usr/local/mysql/bin
[root@mail mysql]# source /etc/profile

[root@mail mysql]# mkdir /var/lib/mysql
[root@mail mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@mail mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@mail mysql]# ln -s /usr/local/mysql/include/mysql
/usr/include/mysql

MySQL資料庫初始化

[[email protected] 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...

配置Nginx服務

//用於提供Perl語言相容的正則表示式庫的軟體包pcre

[root@mail src]# tar -xzvf pcre-8.35.tar.gz 
[root@mail src]# cd pcre-8.35/
[root@mail pcre-8.35]# ./configure --prefix=/usr/local/pcre
[root@mail pcre-8.35]# make && make install
//openssl軟體包用於提供網站加密證書服務的程式檔案

[root@mail src]# tar -xzvf openssl-1.0.1h.tar.gz 
[root@mail nginx-1.6.0]# cd openssl-1.0.1h/
[root@mail openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
[root@mail openssl-1.0.1h]# make && make install

[root@mail openssl-1.0.1h]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
[root@mail openssl-1.0.1h]# source /etc/profile
//zlib軟體包用於提供壓縮功能的函式庫檔案

[root@mail src]# tar xzvf zlib-1.2.8.tar.gz 
[root@mail src]# cd zlib-1.2.8/
[root@mail zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
[root@mail zlib-1.2.8]# make && make install
[root@mail src]# useradd www -s /sbin/nologin -M


[root@mail src]# tar -xzvf nginx-1.6.0.tar.gz 
[root@mail src]# cd nginx-1.6.0/
[root@mail 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
[root@mail nginx-1.6.0]# make && make install

[root@mail nginx-1.6.0]# vim /etc/rc.d/init.d/nginx

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

啟動Nginx服務



[root@mail nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx 

[root@mail nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
Restarting nginx (via systemctl):                          [  確定  ]
[root@mail nginx-1.6.0]# chkconfig nginx on

配置PHP服務

//yasm原始碼包是一款常見的開源彙編器

[root@mail src]# tar -zxvf yasm-1.2.0.tar.gz 
[root@mail src]# cd yasm-1.2.0/
[root@mail yasm-1.2.0]# ./configure
[root@mail yasm-1.2.0]# make && make install
//libmcrypt原始碼包用於加密演算法的擴充套件庫程式

[root@mail src]# tar -xzvf libmcrypt-2.5.8.tar.gz 
[root@mail src]# cd libmcrypt-2.5.8/
[root@mail libmcrypt-2.5.8]# ./configure 
[root@mail libmcrypt-2.5.8]# make && make install
//libvpx原始碼包用於提供視訊編碼器的服務程式
[root@mail src]# tar -xjvf libvpx-v1.3.0.tar.bz2
[root@mail src]# cd libvpx-v1.3.0/
[root@mail libvpx-v1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
[root@mail libvpx-v1.3.0]# make && make install 
//tiff原始碼包是用於提供標籤影象檔案格式的服務程式

[root@mail src]# tar -xzvf tiff-4.0.3.tar.gz 
[root@mail src]# cd tiff-4.0.3/
[root@mail tiff-4.0.3]# ./configure --prefix=/usr/local/tiff --enable-shared
[root@mail tiff-4.0.3]# make && make install 
//libpng原始碼包用於提供png圖片格式支援函式庫的服務程式

[root@mail src]# tar zxvf libpng-1.6.12.tar.gz 
[root@mail src]# cd libpng-1.6.12/
[root@mail libpng-1.6.12]# ./configure --prefix=/usr/local/libpng --enable-shared
[root@mail libpng-1.6.12]# make && make install
//freetype原始碼包用於提供字型支援引擎的服務程式

[root@mail src]# tar -xzvf freetype-2.5.3.tar.gz 
[root@mail src]# cd freetype-2.5.3/
[root@mail freetype-2.5.3]# ./configure --prefix=/usr/local/freetype -enable-shared
[root@mail freetype-2.5.3]# make && make install 
//jpeg原始碼包用於提供jpeg圖片格式支援函式庫的服務程式

[root@mail src]# tar -zxvf jpegsrc.v9a.tar.gz 
[root@mail src]# cd jpeg-9a/
[root@mail jpeg-9a]# ./configure --prefix=/usr/local/jpeg --enable-shared
[root@mail jpeg-9a]# make && make install
//libgd原始碼包用於提供圖形處理的服務程式

[[email protected] src]# tar -xzvf 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
[[email protected] libgd-2.1.0]# make && make install
//t1lib原始碼包用於提供圖片生成函式庫的服務程式
//編譯t1lib-5.1.2的時候make出錯需要安裝latex

//網上解決方法未驗證
yum -y install texlive-latex
yum -y install texlive-metapost
yum -y install texlive-collection-fontsrecommended 

[root@mail src]# tar zxvf t1lib-5.1.2.tar.gz 
[root@mail src]# cd t1lib-5.1.2/
[root@mail t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared

Configure finished!
        Do:  'make'               to compile the t1lib-package.
        Do:  'make without_doc'   to compile if you do not have LaTeX.
        Do:  'make install'       to install the t1lib-package.

[root@mail t1lib-5.1.2]# make without_doc && make install

[root@mail t1lib-5.1.2]# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so
[root@mail t1lib-5.1.2]# cp -frp /usr/lib64/libXpm.so* /usr/lib/
[[email protected] src]# tar -zxvf 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=/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

[[email protected] php-5.5.14]# make && make install

在php原始碼包程式安裝完成後,需要刪除當前預設的配置檔案,然後將php服務程式目錄中相應的配置檔案複製過來

[root@mail php-5.5.14]# rm -rf /etc/php.ini
[root@mail php-5.5.14]# cp php.ini-production /usr/local/php/etc/php.ini
[root@mail php-5.5.14]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[root@mail php-5.5.14]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@mail php-5.5.14]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
[[email protected] php-5.5.14]# vim /usr/local/php/etc/php-fpm.conf
 25 pid = run/php-fpm.pid

148 user = nobody
149 group = nobody

//修改
148 user = www
149 group = www

[root@mail php-5.5.14]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@mail php-5.5.14]# chmod 755 /etc/rc.d/init.d/php-fpm
[root@mail php-5.5.14]# chkconfig php-fpm on
[root@mail php-5.5.14]# vim /usr/local/php/etc/php.ini
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
[root@mail php-5.5.14]# vim /usr/local/nginx/conf/nginx.conf
  2 user  www www;

 43         location / {
 44             root   html;
 45             index  index.html index.htm index.php;
 46         }


 65         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 66         #   
 67         location ~ .*\.php(\/.*)*$ {
 68             root           html;
 69             fastcgi_pass   127.0.0.1:9000;  
 70             fastcgi_index  index.php;
 71             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 72             include        fastcgi_params;
 73         }


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

至此LNMP動態網站環境架構的配置全部完成.

搭建Discuz!論壇

[root@mail src]# unzip Discuz_X3.2_SC_GBK.zip 
[root@mail src]# rm -rf /usr/local/nginx/html/{index.html,50x.html}*
[root@mail src]# mv upload/* /usr/local/nginx/html/
[root@mail src]# chown -Rf www:www /usr/local/nginx/html
[root@mail src]# chmod -Rf 755 /usr/local/nginx/html