当前位置: 首页 > 科技观察

【博文推荐】搭建一个完全分离的LNMP平台的简单案例

时间:2023-03-11 22:00:25 科技观察

写在前面:如果这篇文章有幸被朋友看到,发现不对的地方,望批评指正。如果有什么不明白的地方,我愿意一起讨论。博文地址:http://muluhe.blog.51cto.com/9152490/1564120案例拓扑图安装配置nginx服务器在编译安装nginx时,需要安装开发包组“开发工具”和“服务器平台开发”》,还需要专门安装pcre-devel包。#yum-ygroupinstall"DevelopmentTools"#yum-ygroupinstall"ServerPlatformDevelopment"#yum-yinstallpcre-devel首先添加nginx用户组和nginx用户#groupadd-rnginx#useradd-gnginx-rnginx创建编译安装是所需要的目录vk-p#m/var/tmp/nginx/client编译安装nginx#tarxfnginx-1.4.7.ta??r.gz#cdnginx-1.4.7#./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/客户端/\--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为nginx提供SysVinit脚本#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"]&&exit0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename$nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[-f/etc/sysconfig/nginx]&&./etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs(){#makerequireddirectoriesuser=`nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-`options=`$nginx-V2>&1|grep'configurearguments:'`foroptin$options;doif[`echo$opt|grep'.*-临时路径'`];thenvalue=`echo$opt|cut-d"="-f2`if[!-d"$value"];then#echo"creating"$valuemkdir-p$value&&chown-R$user$valuefifidone}start(){[-x$nginx]||exit5[-f$NGINX_CONF_FILE]||exit6make_dirsecho-n$"Starting$prog:"daemon$nginx-c$NGINX_CONF_FILEretval=$?echo[$retval-eq0]&&touch$lockfilereturn$retval}stop(){echo-n$"Stopping$prog:"killproc$prog-QUITretval=$?echo[$retval-eq0]&&rm-f$lockfilereturn$retval}restart(){配置测试||return$?停止sleep1开始}reload(){configtest||return$?echo-n$"Reloading$prog:"killproc$nginx-HUPRETVAL=$?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"instart)rh_status_q&&exit0$1;;停止)rh_status_q||exit0$1;;重启|配置测试)$1;;重新加载)rh_status_q||exit7$1;;力量-重新加载)force_reload;;状态)rh_status;;condrestart|try-restart)rh_status_q||exit0;;*)echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit2esac授予此脚本执行权限#chmod+x/etc/rc.d/init.d/nginx将nginx服务添加到服务管理列表中,让它开机自动启动#chkconfig--addnginx#chkconfignginxon编辑配置文件/etc/nginx/nginx.conf,在server中添加如下内容部分位置~\.php${fastcgi_pass10.170.2.90:9000;fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME/var/www/html$fastcgi_script_name;includefastcgi_params;}启动nginx服务#vim/etc/init.d/nginxstart测试nginx是否正常,在浏览器输入10.170.2.80,可以得到如下页面InstallPHPservercompileandinstallphp#tarxfphp-5.4.26.tar.bz2#cdphp-5.4.26#./configure--prefix=/usr/local/php5--with-mysql=mysqlnd--with-pdo-mysql=mysqlnd--with-mysqli=mysqlnd--with-openssl--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--enable-套接字--enable-fpm--with-mcrypt--with-config-file-path=/etc--with-config-file-scan-dir=/etc/php.d--with-bz2#make&&makeinstall提供php的配置文件#cpphp.ini-production/etc/php.ini配置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#cp/usr/local/php5/etc/php-fpm.conf.default/usr/local/php5/etc/php-fpm.conf编辑配置文件/usr/local/php5/etc/php-fpm.conf,修改如下选项对应的值pm.max_children=50pm.start_servers=5pm.min_spare_servers=2pm.max_spare_servers=8pid=/usr/local/php5/var/run/php-fpm.pid在/var/www/下面提供测试页面index.php,内容为

hello,nginx

启动php-fpm服务#/etc/init.d/php-fpmstarttestnginxserver是否可以和php服务器建立通信,在浏览器中输入10.170.2.80/index.php,可以得到下面页面显示Failure...,因为后端数据库没有相应配置安装MariaDB服务器编译安装mariadb-5.5.36#tarxfmariadb-5.5.36-linux-x86_64.tar.gz-C/usr/local#cd/usr/local/#ln-svmariadb-5.5.36-linux-x86_64/mysql#mkdir-pv/mysql/data#groupadd-rmysql#useradd-gmysql-s/sbin/nologin-M-d/mysql/data-rmysql#chown-Rmysql:mysql/mysql#chown-Rmysql:mysql/mysql/data提供数据库的配置文件:#cdmysql#mkdir/etc/mysql#chown-Rroot.mysql./*#cpsupport-files/my-large.cnf/etc/mysql/my.cnf修改文件/etc/mysql/my.cnf的内容,在thread_concurrency=8行下添加一行:datadir=/mysql/data为数据库提供一个SysV启动脚本,设置为开机启动:#cpsupport-files/mysql.server/etc/init.d/mysqld#chkconfig--addmysqld#chkconfigmysqldon初始化数据库并启动数据库:#echo"exportPATH=/usr/local/mysql/bin:$PATH">/etc/profile.d/mysql.sh#source/etc/profile.d/mysql.sh#echo"/usr/local/mysql/lib">/etc/ld.so.conf.d/mysql.conf#ldconfig#ln-sv/usr/local/mysql/include//usr/include/mysql#scripts/mysql_install_db--user=mysql--datadir=/mysql/data/#/etc/init.d/mysqldstart创建数据库并授权:MariaDB[(none)]>CREATEDATABASEtestdb;MariaDB[(none)]>GRANTALLONtestdb.*TOdscuser@'10.170.2.%'IDENTIFIEDBY'******';MariaDB[(无)]>FLUSHPRIVILEGES;整体测试LNMP平台在浏览器中输入10.170.2.80/index.php,可以得到如下页面这次可以看到页面显示的Success...信息