LNMP分离部署

Posted bixiaoyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP分离部署相关的知识,希望对你有一定的参考价值。

 

环境:

nginx+php:192.168.2.144

mysql:192.168.2.151

【Nginx】

yum install -y pcre-devel openssl-deve popt-devel

tar zxvf nginx-1.11.2.tar.gz -C /usr/src/
cd /usr/src/
d /usr/src/nginx-1.11.2/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
 make
 make install
 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx -t

[[email protected] ~]# cat /etc/init.d/nginx
#!/bin/bash
PRIF="/usr/local/nginx/sbin/nginx"
PROG="/usr/local/nginx/logs/nginx.pid"
case $1 in 
start)
    [[ `netstat -anput | grep nginx | wc -l` -eq 0 ]]
    $PRIF
    ([ `echo $?` -eq 0 ] && echo "This Start Secusses!") ||(echo "This Nginx Running,Please reload or restart")
;;
stop)
    [[ `netstat -anput | grep nginx | wc -l` > 0 ]]
     kill -s QUIT $(cat $PROG)
    ([ `echo $?` -eq 0 ] && echo "Nginx stop successfully!") || (echo "Failed stop nginx!")
;;
reload)
     if [[ `netstat -anput | grep nginx | wc -l` > 0 ]];then
     kill -s HUP $(cat $PROG)
  else
     $PRIF
     kill -s HUP $(cat $PROG)
     ([ `echo $?` -eq 0 ] && echo "This is Nginx reload Secussed!")|| (echo "This Nginx Reload failure!")
   fi
;;
restart)
    if [[ `netstat -anput | grep nginx | wc -l` == 0 ]];then
     echo "This Nginx Running..."
     $PRIF     
    ([ `echo $?` -eq 0 ] && echo "Nginx start successfully!")
 else
     echo Stopping Nginx...
    kill -s QUIT $(cat $PROG)
    $PRIF
    ([ `echo $?` -eq 0 ] && echo "This is Nginx Restart Secussed!") || (echo "This Nginx Restart Faulure!")
   fi
;;
*)
   echo -e "33[41;36m Unages: $0 [start|stop|reload|restart]33[0m"
   exit 1
;;
esac

【Mysql】

[[email protected] ~]# yum install -y cmake ncurses-devel 

[[email protected] ~]# tar zxvf mysql-5.5.38.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/mysql-5.5.38/
[[email protected] mysql-5.5.38]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utff8_general_ci -DWITH_EXTRA_CHARSETS=all

[[email protected] mysql-5.5.38]# make

[[email protected] mysql-5.5.38]# make install

[[email protected] mysql-5.5.38]# useradd -M -s /sbin/nologin mysql
[[email protected] mysql-5.5.38]# chown -R mysql.mysql /usr/local/mysql/
[[email protected] mysql-5.5.38]# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cf
[[email protected] mysql-5.5.38]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

[[email protected] mysql-5.5.38]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.5.38]# echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[[email protected] mysql-5.5.38]# source /etc/profile

 [[email protected] mysql-5.5.38]# chkconfig --add mysqld

[[email protected] mysql-5.5.38]# systemctl  mysqld start 

[php]

[[email protected] ~]# yum install -y libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel

[[email protected] ~]# tar -jxvf php-5.6.8.tar.bz2 -C /usr/src/

[[email protected] php-5.6.8]# ./configure --prefix=/usr/local/php --with-mysql --with-pdo-mysql --with-mysqli --with-openssl --enable-fpm --enable-sockts --enable-mbstring --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr/ --with-config-file-path=/etc/ --with-config-file-scan-dir=/etc/php.d --with-curl

[[email protected] php-5.6.8]# make 

[[email protected] php-5.6.8]# make install

[[email protected] php-5.6.8]# cp php.ini-production /etc/php.ini
[[email protected] php-5.6.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-5.6.8]# chmod +x /etc/init.d/php-fpm
[[email protected] php-5.6.8]# chkconfig --add php-fpm
[[email protected] php-5.6.8]# chkconfig php-fpm on
[[email protected] php-5.6.8]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[[email protected] etc]# sed -i ‘235s#pm.max_children = 5#pm.max_children = 150#;s#pm.start_servers = 2#pm.start_servers = 8#;s#pm.min_spare_servers = 1#pm.min_spare_servers = 8#;s#pm.max_spare_servers = 3#pm.max_spare_servers = 10#g‘ /usr/local/php/etc/php-fpm.conf

配置php-fpm的 Pid文件路径

[[email protected] etc]# sed -i ‘s#;pid = run/php-fpm.pid#pid = /usr/local/php/var/run/php-fpm.pid#g‘ /usr/local/php/etc/php-fpm.conf

在php-fpm配置文件配置用户名为nginx,用nginx用户来启用php-fpm程序,而监听地址为本地IP用来调用

[[email protected] etc]# sed -i ‘s#user = nobody#user = nginx#;s#group = nobody#group = nginx#;s#listen = 127.0.0.1:9000#listen = 192.168.2.144:9000#g‘ /usr/local/php/etc/php-fpm.conf

【整合Nginx与PHP】

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /www/webapp/;
            index index.php index.html index.htm;
        }
           location ~ .php$ {
             root           /www/webapp;
               fastcgi_pass   192.168.2.144:9000;
               fastcgi_index  index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
               include        fastcgi_params;
           }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

[[email protected] etc]# mkdir /www/webapp -p 

[[email protected] etc]# useradd -M -s /sbin/nologin  nginx 

[[email protected] etc]# systemctl start php-fpm

netstat -anput | grep php-fpm
tcp 0 0 192.168.2.144:9000 0.0.0.0:* LISTEN 126357/php-fpm: mas

[[email protected] ~]# cat /www/webapp/index.php
<?php
phpinfo();
?>

技术分享图片

Ps:如果PHP单独在一个服务独立运行的话。可以用nfs共享,指定php-fpm的本地IP进行挂载

1 mkdir /www
2 chown -R nginx:nginx /www
3 vim /etc/exports
4 ---------------------------------------------
5 /www    192.168.2.0/24(rw,no_root_squash)
6 ---------------------------------------------
7 service nfs start

8 mount -t nfs 192.168.2.144:/www /www
【Discuz论坛】

[[email protected] ~]# unzip Discuz_X3.1_SC_UTF8.zip -d /www/webapp

[[email protected] ~]# cd /www/webapp
[[email protected] webapp]# mv upload/* .

[[email protected] webapp]# chmod 777 -R data/ config/ uc_client/ uc_server/ 

【数据库授权】

[[email protected] mysql-5.5.38]# mysql -u root -p123.com

mysql> create database Discuzdb default charset=utf8;

mysql> grant all on Discuzdb.* to [email protected]‘mysql> grant all on Discuzdb.* to [email protected]‘192.168.2.144‘ identified by ‘123456‘;

mysql> flush privileges;

技术分享图片

技术分享图片

【Mysql数据库错误问题汇总】

错误1:预编译报错:

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed

技术分享图片

错误2:解决方法:

make clean    #清楚上次编译生成的object文件以及二进制文件
rm -rf CMakeCache.txt  
rm -rf /etc/my.cnf

make[2]: *** [sql/CMakeFiles/sql.dir/mysqld.cc.o] 错误 1
make[1]: *** [sql/CMakeFiles/sql.dir/all] 错误 2

技术分享图片

 
































以上是关于LNMP分离部署的主要内容,如果未能解决你的问题,请参考以下文章

分离部署LNMP搭建WORDPRESS详细步骤

LNMP分离式部署

LNMP分离部署

docker分离部署lnmp

HAProxy+Varnish+LNMP实现高可用负载均衡动静分离集群部署

Docke容器分离部署LNMP