可搭建分布式的lnmp源码文档

mysql部分

CentOS7.5源码安装mysql5.7.23

mysql5.7以上需要boost_1_59_0
cd /root/lnmp
#下载boost_1_59_0 
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# 解压
tar -zxvf boost_1_59_0.tar.gz 
安装cmake( is used as the build framework on all platforms)
yum install cmake -y

#####安装GCC

yum -y install gcc gcc-c++ kernel-devel
安装ncurses-devel
yum install -y ncurses-devel
安装mysql
cd /root/lnmp
# 下载
wget ftp://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz
# 解压
tar -zxvf mysql-5.7.23.tar.gz
# 进入目录
cd mysql-5.7.23
# 安装
cmake -DWITH_BOOST=/root/lnmp/boost_1_59_0
make && make install
将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld 
#将mysqld服务设置为开机启动
chkconfig mysqld on
#编辑服务脚本
vim /etc/rc.d/init.d/mysqld 
#MySQL程序安装路径
basedir=/usr/local/mysql 
#MySQl数据库存放目录
datadir=/var/lib/mysql
#修改完配置后创建数据库目录
mkdir /var/lib/mysql
添加数据库
#添加mysql组
groupadd mysql
#创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
useradd -g mysql mysql -s /sbin/nologin 
#设置MySQL数据库目录权限
chown -R mysql:mysql /var/lib/mysql
#添加数据库
/usr/local/mysql/bin/mysqld --user=mysql --initialize
将mysql等(/usr/local/mysql/bin 目录下的脚本)加入环境变量
vim /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/cmake/bin:/usr/local/mysql/bin
#使配置立即生效
source /etc/profile 
修改etc/my.cnf大致如下
[client]
socket=/var/lib/mysql/mysql.sock
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
启动mysql服务
#启动
service mysqld start
#重启
service mysqld restart
#关闭
service mysqld stop
启动服务后设置root密码
/usr/local/mysql/bin/mysql_secure_installation

nginx部分

centOS7下源码安装nginx1.15.5

在用户目录下新建lnmp目录并下载nginx源码
cd /root/lnmp
#下载nginx
wget http://nginx.org/download/nginx-1.15.5.tar.gz
注意若没有安装gcc请先安装gcc编译工具
yum -y install gcc gcc-c++ kernel-devel
同时注意安装前要安装nginx必要扩展
#安装正则 pcre扩展
yum -y install pcre-devel.x86_64
#安装openssl
yum install -y openssl-devel.x86_64
安装nginx
# 解压nginx
tar -zxvf nginx-1.15.5.tar.gz 
# 进入目录
cd nginx-1.15.5/
# 安装
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module && make && make install
增加nginx执行用户
#添加www组
groupadd www
#创建用户www并加入到www组,不允许www用户直接登录系统
useradd -g www www -s /sbin/nologin
# 修改配置
vim /usr/local/nginx/conf/nginx.conf
user  www;
!!!注意nginx的执行用户应尽量与PHP的执行用户一致(避免权限问题)
启动nginx
/usr/local/nginx/nginx
修改nginx配置,并新增server
vim /usr/local/nginx/nginx.conf
# 将整个server模块注释并在之后新增包含vhost/*.conf 
#效果如下 (整块代码在http模块内)
   # server {
   #     listen       80;
   #     server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
    #    error_page   500 502 503 504  /50x.html;
    #    location = /50x.html {
    #        root   html;
    #    }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
   # }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
include vhost/*.conf;
# wq!保存退出
#新增vhost目录并新建localhost.conf
mkdir vhost
cd  vhost
vim localhost.conf
# 增加代码
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/wwwroot;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /data/wwwroot;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
# wq! 保存退出

修改完配置重启nginx
ps aux | grep nginx
#将nginx进程杀死
kill -9 1111 2222
#开启nginx
/usr/local/nginx/nginx

# 或者
/usr/local/nginx/nginx -s stop|quit|reopen|reload
将nginx加入服务
# 创建文件并写入shell脚本代码
vim /etc/rc.d/init.d/nginx
chmod +x /etc/rc.d/init.d/nginx
chkconfig nginx on
shell脚本代码如下
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

PATH=/usr/local/nginx:/usr/local/nginx:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi


case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

PHP部分

centOS7.5源码安装PHP7.0.32

yum安装epel-release
yum install -y epel-release
安装扩展
# 安装扩展
yum install -y gcc gcc-c++ kernel-devel libxml2-devel libxslt-devel libmcrypt-devel libicu-devel libcurl-devel openssl-devel gd-devel

安装

cd /root/lnmp
#下载php7源码
wget http://cn2.php.net/get/php-7.0.32.tar.gz/from/this/mirror
#解压 php7
mv mirror php-7.0.32.tar.gz
tar zxvf php-7.0.32.tar.gz
#进入目录
cd /root/lnmp/php-7.0.32
# 安装php
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --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 --with-curl --enable-mbregex --enable-mbstring --enable-intl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl
make && make install

配置服务、启动、配置等

# 进入解压后的源码目录
cd /root/lnmp/php-7.0.32
# 将源码包里的配置复制到安装目录的etc下
cp php.ini-production /usr/local/php/etc/php.ini
# 将源码包里的php-fpm的服务文件复制到init.d目录下(创建php-fpm服务)
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# 增加执行权限
chmod +x /etc/rc.d/init.d/php-fpm
#将服务php-fpm 设置为开机启动
chkconfig php-fpm on
#将php-fpm执行脚本复制到/usr/local/php/bin目录下
cp sapi/fpm/php-fpm /usr/local/php/bin/
复制php-fpm的配置文件
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
创建启动php-fpm的用户
groupadd www 
useradd -g www www -s /sbin/nologin
加入服务后使用service对php-fpm进行服务管理
# 启动php-fpm
service php-fpm start
# 关闭php-fpm
service php-fpm stop
# 重启 php-fpm
service php-fpm restart