搭建环境:Centos 6.8,Nginx 1.9.14,MysqL 5.6.23,PHP 5.6.17
一、目的:LNMP如今已成为主流的web服务器搭建方案,公司内部的ECstore,Discuz,计划用来替代nagios+check_mk监控平台的Zabbix。均基于LNMP。本次单机LNMP搭建为后续全面了解电商集群做准备,顺便记录搭建过程,作为以后搭建环境的参考。
二、LNMP安装环境准备:
关闭防火墙及selinux
serviceiptablesstop chkconfigiptablesoff setenforce0 vi/etc/sysconfig/selinux SELINUX=disabled#enforcing改为disabled
依赖包安装
yum-yinstallgccgcc-c++autoconflibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibxml2libxml2-develzlibzlib-develglibcglibc-develglib2glib2-develbzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-devellibidnlibidn-developensslopenssl-developenldapopenldap-developenldap-clientsopenldap-serversmakelibtool*gittreebisonpcre-develperlgdgd-devel
tarxflibiconv-1.14.tar.gz cdlibiconv-1.14/ ./configure--prefix=/usr/local make&&makeinstall
安装libmcrypt (加密算法扩展库,支持DES,3DES,RIJNDAEL,Twofish,IDEA,GOST,CAST-256,ARCFOUR,SERPENT,SAFER+等算法)
tarxflibmcrypt-2.5.8.tar.gz cdlibmcrypt-2.5.8 ./configure make&&makeinstall cdlibltdl/ ./configure--enable-ltdl-install#加载动态库 make&&makeinstall
安装mhash(Mhash是基于离散数学原理的不可逆向的PHP加密方式扩展库,其在默认情况下不开启。 mhash的可以用于创建校验数值,消息摘要,消息认证码,以及无需原文的关键信息保存)
tarxfmhash-0.9.9.9.tar.bz2 cdmhash-0.9.9.9 ./configure make&&makeinstall
安装mcript(mcrypt 是 PHP 里面重要的加密支持扩展库,Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。)
cd/usr/local/src tarzxvfmcrypt-2.6.8.tar.gz cdmcrypt-2.6.8/ exportLD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH#暂时生效 ./configure make&&makeinstall
ln-s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la ln-s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so ln-s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4 ln-s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8 ln-s/usr/local/lib/libmhash.a/usr/lib/libmhash.a ln-s/usr/local/lib/libmhash.la/usr/lib/libmhash.la ln-s/usr/local/lib/libmhash.so/usr/lib/libmhash.so ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2 ln-s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1 ln-s/usr/local/bin/libmcrypt-config/usr/bin/libmcrypt-config
vim/etc/ld.so.conf /usr/local/lib/ ldconfig
安装cmake (MysqL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具)
tarxfcmake-3.4.1.tar.gz cdcmake-3.4.1 ./bootstrap make&&makeinstall
注:一起解压所有文件可采用:find /usr/local/src/*.tar.gz -exec tar xf {} \;
三、MysqL编译安装
groupadd-rMysqL useradd-r-gMysqLMysqL
新建MysqL所需目录
mkdir-p/usr/local/MysqL mkdir-p/data/MysqLdb
编译安装
tarxfMysqL-5.6.23.tar.gz cdMysqL-5.6.23 cmake-DCMAKE_INSTALL_PREFIX=/usr/local/MysqL-DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci-DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1-DWITH_BLACKHOLE_STORAGE_ENGINE=1-DMysqL_DATADIR=/data/MysqLdb-DMysqL_TCP_PORT=3306-DENABLE_DOWNLOADS=1-DSYSCONFDIR=/etc-DWITH_SSL=system-DWITH_ZLIB=system-DWITH_LIBWRAP=0 make&&makeinstall
cd/usr/local/MysqL chown-RMysqL:MysqL. cd/data/MysqLdb chown-RMysqL:MysqL.
cd/usr/local/MysqL ./scripts/MysqL_install_db--user=MysqL--datadir=/data/MysqLdb
编译修改/etc/my.cnf
[MysqL] #CLIENT# port=3306 socket=/data/MysqLdb/MysqL.sock [MysqLd] #GENERAL# user=MysqL default-storage-engine=InnoDB socket=/data/MysqLdb/MysqL.sock pid-file=/data/MysqLdb/MysqL.pid #MyISAM# key-buffer-size=32M myisam-recover=FORCE,BACKUP #SAFETY# max-allowed-packet=16M max-connect-errors=1000000 #DATASTORAGE# datadir=/data/MysqLdb/ #BINARYLOGGING# log-bin=/data/MysqLdb/MysqL-bin expire-logs-days=14 sync-binlog=1 #REPLICATION# skip-slave-start=1 relay-log=/data/MysqLdb/relay-bin slave-net-timeout=60 #CACHESANDLIMITS# tmp-table-size=32M max-heap-table-size=32M query-cache-type=0 query-cache-size=0 max-connections=500 thread-cache-size=50 open-files-limit=65535 table-definition-cache=4096 table-open-cache=4096 #INNODB# innodb-flush-method=O_DIRECT innodb-log-files-in-group=2 innodb-log-file-size=64M innodb-flush-log-at-trx-commit=1 innodb-file-per-table=1 innodb-buffer-pool-size=592M #LOGGING# log-error=/data/MysqLdb/MysqL-error.log log-queries-not-using-indexes=1 slow-query-log=1 slow-query-log-file=/data/MysqLdb/MysqL-slow.log
cpsupport-files/MysqL.server/etc/init.d/MysqLd vim/etc/profile.d/MysqL.sh PATH=/usr/local/MysqL/bin:/usr/local/MysqL/lib:$PATH exportPATH source/etc/profile.d/MysqL.sh
serviceMysqLdstart chkconfigMysqLdon
dropuser''@localhost; dropuser''@hostname; updateMysqL.usersetpassword=password('*******'); flushprivileges;
四、Nginx编译安装
groupadd-rNginx useradd-gNginx-rNginx
创建所需要目录
mkdir-pv/var/tmp/Nginx/client
编译安装Nginx
tarxfNginx-1.9.14.tar.gz cdNginx-1.9.14 ./configure--prefix=/usr/local/Nginx--sbin-path=/usr/local/Nginx/sbin/Nginx--conf-path=/etc/Nginx/Nginx.conf--error-log-path=/var/log/Nginx/error.log--http-log-path=/var/log/Nginx/access.log--pid-path=/var/run/Nginx/Nginx.pid--lock-path=/var/lock/Nginx.lock--user=Nginx--group=Nginx--with-http_ssl_module--with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module--http-client-body-temp-path=/var/tmp/Nginx/client/--http-proxy-temp-path=/var/tmp/Nginx/proxy/--http-fastcgi-temp-path=/var/tmp/Nginx/fcgi/--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi--http-scgi-temp-path=/var/tmp/Nginx/scgi--with-pcre make&&makeinstall
编辑启动脚本
vim/etc/rc.d/init.d/Nginx #!/bin/sh # #Nginx-thisscriptstartsandstopstheNginxdaemon # #chkconfig:-8515 #description:NginxisanHTTP(S)server,HTTP(S)reverse\ #proxyandIMAP/POP3proxyserver #processname:Nginx #config:/etc/Nginx/Nginx.conf #config:/etc/sysconfig/Nginx #pidfile:/var/run/Nginx.pid #Sourcefunctionlibrary. ./etc/rc.d/init.d/functions #Sourcenetworkingconfiguration. ./etc/sysconfig/network #Checkthatnetworkingisup. ["$NETWORKING"="no"]&&exit0 Nginx="/usr/local/Nginx/sbin/Nginx" prog=$(basename$Nginx) Nginx_CONF_FILE="/etc/Nginx/Nginx.conf" [-f/etc/sysconfig/Nginx]&&./etc/sysconfig/Nginx lockfile=/var/lock/subsys/Nginx make_dirs(){ #makerequireddirectories user=`Nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-` options=`$Nginx-V2>&1|grep'configurearguments:'` foroptin$options;do if[`echo$opt|grep'.*-temp-path'`];then value=`echo$opt|cut-d"="-f2` if[!-d"$value"];then #echo"creating"$value mkdir-p$value&&chown-R$user$value fi fi done } start(){ [-x$Nginx]||exit5 [-f$Nginx_CONF_FILE]||exit6 make_dirs echo-n$"Starting$prog:" daemon$Nginx-c$Nginx_CONF_FILE retval=$? echo [$retval-eq0]&&touch$lockfile return$retval } stop(){ echo-n$"Stopping$prog:" killproc$prog-QUIT retval=$? echo [$retval-eq0]&&rm-f$lockfile return$retval } restart(){ configtest||return$? stop sleep1 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/null2>&1 } case"$1"in start) rh_status_q&&exit0 $1 ;; stop) rh_status_q||exit0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q||exit7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q||exit0 ;; *) echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit2 esac
设置开机启动并启动服务
chmod+x/etc/rc.d/init.d/Nginx chkconfig--addNginx chkconfigNginxon serviceNginxstart
五、PHP编译安装
cp-frp/usr/lib64/libldap*/usr/lib/ echo/usr/local/MysqL/lib>>/etc/ld.so.conf.d/MysqL-x86_64.conf ldconfig-v
编译安装PHP
tarxfPHP-5.6.17.tar.gz cdPHP-5.6.17 ./configure--prefix=/usr/local/PHP--with-MysqL=/usr/local/MysqL--with-MysqLi=/usr/local/MysqL/bin/MysqL_config--with-iconv-dir=/usr/local--with-openssl--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--disable-rpath--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--enable-mbregex--enable-mbstring--with-gd--enable-gd-native-ttf--with-mhash--enable-pcntl--enable-sockets--with-mcrypt--with-ldap--with-ldap-sasl--with-xmlrpc--enable-zip--enable-soap--with-bz2--with-config-file-path=/etc--enable-fpm--with-config-file-scan-dir=/etc/PHP.d--enable-maintainer-zts makeZEND_EXTRA_LIBS='-liconv' makeinstall
cpPHP.ini-production/etc/PHP.ini
cp/usr/local/PHP/etc/PHP-fpm.conf.default/usr/local/PHP/etc/PHP-fpm.conf
设置PHP-fpm启动脚本并开机启动
cpsapi/fpm/init.d.PHP-fpm/etc/rc.d/init.d/PHP-fpm chmod+x/etc/rc.d/init.d/PHP-fpm chkconfig--addPHP-fpm chkconfigPHP-fpmon servicePHP-fpmstart
六、web功能基本实现
vim/etc/Nginx/Nginx.conf #location~\.PHP${ #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.PHP; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #} #修改为 location~\.PHP${ fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.PHP; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; }
vim/usr/local/Nginx/html/test.PHP <?PHP $link=MysqL_connect('127.0.0.1','root','you_passwd'); if($link) echo"It'sOK,Frank"; else echo"Failed,Frank"; MysqL_close; PHPinfo(); ?>
Nginx重载
serviceNginxreload
访问http://ip/test.php,LNMP测试成功。
七、后记
从去年底开始,LAMP,LNMP断断续续搭建过多次,搭建时磕磕碰碰,完成后就会忘记,估计是基础不牢的原因。虽然本次搭建中间也有些许不顺,但决定写博文之后,还是有点小压力,前前后后验证了两遍,还是可以保证可行性的。本次Nginx,PHP的配置优化及插件均未涉及,后续博文中将陆续补上。