• centos7 编译安装新版LNMP环境


    centos7 编译安装新版LNMP环境

    环境版本如下:


    1、系统环境:Centos 7 x86_64

    2、NGINX:nginx-1.11.3.tar.gz

    3、数据库:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz

    4、PHP:php-5.6.25.tar.gz

    记得不要忘了先安装gcc gcc-c++ wget net-tools等功能哦。

    一、首先安装mariadb

    应为数据库编译需要很长时间,所以我这里下载的是已经编译好了的二进制包,下载版本为:mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz

    1、下载二进制包到/usr/local/src 目录下:

    2、将压缩包解压到/home/work/env 目录下:                                                             #根据个人习惯更改安装目录,下面不再做解释。#/home/work/env/

    [root@centos74 src]#tar zvxf mariadb-10.0.28-linux-glibc_214-x86_64.tar.gz -C /home/work/env 

    3、[root@centos74 src]# mv /home/work/env/mariadb-10.0.28-linux-x86_64/ /home/work/env/mariadb                                

    4、创建mariadb 数据初始化目录/home/work/env/mariadb/data/mysql:

    [root@centos74 src]# mkdir -p /home/work/env/mariadb/data/mysql                                                                           

    5、将mariadb 数据初始化目录所属主和组都修改为work:

    [root@centos74 src]# chown -R work:work /home/work/env/mariadb/data/mysql        

     6、进入重命名后的目录,初始化mariadb:

    root@centos74 src]# cd /home/work/env/mariadb                                                                                      
    [root@centos74 mysql]# ./scripts/mysql_install_db --datadir=/home/work/env/mariadb/data/mysql --user=work                        
    Installing MariaDB
    /MySQL system tables in '/home/work/env/mariadb/data/mysql
    ' ... 140906 2:03:19 [Note] InnoDB: Using mutexes to ref count buffer pool pages 140906 2:03:19 [Note] InnoDB: The InnoDB memory heap is disabled 140906 2:03:19 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 140906 2:03:19 [Note] InnoDB: Compressed tables use zlib 1.2.3 140906 2:03:19 [Note] InnoDB: Using Linux native AIO 140906 2:03:19 [Note] InnoDB: Using CPU crc32 instructions 140906 2:03:19 [Note] InnoDB: Initializing buffer pool, size = 128.0M ........................................................................ The latest information about MariaDB is available at http://mariadb.org/. You can find additional information about the MySQL part at: http://dev.mysql.com Support MariaDB development by buying support/new features from SkySQL Ab. You can contact us about this at sales@skysql.com. Alternatively consider joining our community based development effort: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/

    报错:WARNING: The host 'test4' could not be looked up with resolveip.
    解决办法:vim /etc/hosts 在最后一行添加192.168.1.242 test4

    报错:./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

    解决办法:yum -y install libaio-devel libaio

    7、配置文件/home/work/env/mariadb/my.cnf

    [client]
    port = 3306
    socket = /home/work/env/mariadb/data/mysql.sock
    default-character-set = utf8
    [mysqld]
    port = 3306
    tmp_table_size = 512M
    max_heap_table_size = 512M
    socket = /home/work/env/mariadb/data/mysql.sock
    character-set-server = utf8
    character-set-filesystem = utf8
    skip-external-locking
    key_buffer_size = 256M
    max_allowed_packet = 32M
    table_open_cache = 1024
    sort_buffer_size = 1M
    read_buffer_size = 1M
    read_rnd_buffer_size = 4M
    myisam_sort_buffer_size = 64M
    ft_min_word_len = 2
    thread_cache_size = 8
    query_cache_size = 16M
    thread_concurrency = 8
    max_connections = 500
    slave_net_timeout = 30
    max_connect_errors = 100000
    binlog_format = row
    expire-logs-days = 3
    #binlog-ignore-db = mysql
    server-id = 96
    log-bin = mysql-bin
    log-slave-updates
    relay-log = mysqld-relay-bin
    relay-log-purge = 1
    slow-query-log = 1
    long_query_time = 2
    innodb_file_per_table = 1
    innodb_open_files = 800
    innodb_buffer_pool_size = 6G
    innodb_additional_mem_pool_size = 16M
    innodb_log_file_size = 512M
    innodb_log_buffer_size = 8M
    innodb_flush_log_at_trx_commit = 2
    innodb_lock_wait_timeout = 50
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    default-character-set=utf8
    [myisamchk]
    key_buffer_size = 128M
    sort_buffer_size = 128M
    read_buffer = 2M
    write_buffer = 2M
    
    [mysqlhotcopy]
    interactive-timeout

    8、编辑mariadb启动脚本文件/home/work/env/mariadb/start.sh

    #!/bin/sh
    dir=`dirname $(realpath $0)`
    cmd="nohup $dir/bin/mysqld_safe --defaults-file=$dir/my.cnf --basedir=$dir --ledir=$dir/bin --datadir=$dir/data --log-error=$dir/data/mysql.err --pid-file=$dir/data/mysql.pid >> mysql.log 2>&1 & echo $!"
    echo $cmd
    nohup $dir/bin/mysqld_safe --defaults-file=$dir/my.cnf --basedir=$dir --ledir=$dir/bin --datadir=$dir/data --log-error=$dir/data/mysql.err --pid-file=$dir/data/mysql.pid >> mysql.log 2>&1 & echo $!
    
    sleep 1
    # 确保mysql已经起来后, 再退出
    while true 
    do
        pid_file="$dir/data/mysql.pid"
        if test -f "$pid_file"
        then
            PID=`cat "$pid_file"`
            if kill -0 $PID > /dev/null 2> /dev/null
            then
                if ps wwwp $PID | grep -v mysqld_safe | grep -- mysqld > /dev/null
                then
                    #mysqld 已经被启动了
                    echo "mysqld has been started!"
                    break
                fi
            fi
        fi
        echo "mysqld is still starting, waiting for it"
        sleep 2
    done

    编辑mariadb启动脚本文件/home/work/env/mariadb/stop.sh

    #!/bin/sh
    dir=`dirname $(realpath $0)`
    cmd="$dir/bin/mysqladmin -S $dir/data/mysql.sock -uroot shutdown"
    echo $cmd
    $dir/bin/mysqladmin -S $dir/data/mysql.sock -uroot shutdown

    编辑mariadb启动脚本文件/home/work/env/mariadb/connnect.sh

    bin/mysql -uroot -Sdata/mysql.sock -A

    10、将mariadb自带命令放入$PATH

    [root@localhost ~]# echo "export PATH=$PATH:/home/work/env/mariadb/bin/" >>/etc/profile
    [root@localhost ~]# source !$

    11、启动mariadb:

    chmod +x start.sh

    sh start.sh

     
    第二步,开始安装php
    这里要先声明一下,针对Nginx的php安装和针对apache的php安装是有区别的,因为Nginx中的php是以fastcgi的方式结合nginx的,可以理解为nginx代理了php的fastcgi,而apache是把php作为自己的模块来调用的。同样的,php官方下载地址: http://www.php.net/downloads.php
    1、下载php版本:php-5.6.25.tar.gz/
    2、解压php:
    [root@localhost src]# tar zxf php-5.6.25.tar.gz -C /home/work/env
    3、配置编译参数
    [root@localhost src]# cd php-5.6.25
     [root@localhost src]# yum -y install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel 
     [root@localhost php-5.6.25]# ./configure --prefix=/home/work/env/php --with-config-file-path=/home/work/env/php/etc --enable-fpm --with-fpm-user=work --with-fpm-group=work --with-mysql=mysqlnd  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gd --with-libxml-dir=/home/work/env --with-gettext

    错误:configure: error: xml2-config not found. Please check your libxml2 installation.

    解决办法:yum -y install libxml2-devel

    错误:configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/

    解决办法:yum -y install libcurl-devel

    错误:configure: error: jpeglib.h not found.

    解决办法:yum -y install libjpeg-turbo-devel

    错误:configure: error: png.h not found.

    解决办法:yum -y install libpng-devel

    错误:configure: error: freetype-config not found.
    解决办法:configure: error: mcrypt.h not found. Please reinstall libmcrypt.

    错误:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
    解决办法:yum -y install libmcrypt-devel  

    如果yum没有这个libmcrypt-devel包,那就自己编译安装libmcrypt-2.5.8.tar.gz(去下载)

    #tar -zxvf mcrypt-2.6.8.tar.gz
    #cd mcrypt-2.6.8
    #LD_LIBRARY_PATH=/usr/local/lib ./configure
    #make
    #make install

    4、安装php

    [root@localhost php-5.6.25]# make && make install

    以上每一个步骤,如果没有完全执行正确,那么下一步是无法进行的,使用 echo $? 看结果是否为 “0” , 如果不是,就是没有执行正确。

    5、修改配置文件

    cp php.ini-production /home/work/env/php/etc/php.ini
    vim /home/work/env/php/etc/php-fpm.conf

    6、把如下内容写入该文件:

    [global]
    pid = /home/work/env/php/var/run/php-fpm.pid
    error_log = /home/work/env/php/var/log/php-fpm.log
    [www]
    listen = /home/work/env/php/php-fcgi.sock                   #或 listen = 127.0.0.1:9000
    user = work
    group = work
    listen.owner = work
    listen.group = work
    pm = dynamic
    pm.max_children = 50
    pm.start_servers = 20
    pm.min_spare_servers = 5
    pm.max_spare_servers = 35
    pm.max_requests = 500
    rlimit_files = 1024

    7、保存配置文件后,检验配置是否正确的方法为:/home/work/env/php/sbin/php-fpm -t

    如果出现诸如 “test is successful” 字样,说明配置没有问题。

    8、启动php-fpm

    #!/bin/bash
    #opt.sh
    dir=`dirname $(realpath $0)`
    export LD_LIBRARY_PATH=$dir/lib
    usage()
    {
     echo "usage: sh opt.sh start|stop|restart|count"
    }
    
    OPT=$1
    if [ $# -ne 1 ]; then
     usage
     exit 1
    fi
    case $OPT in
     start|Start) echo "Starting..."
     /home/work/env/php/sbin/php-fpm --prefix=/home/work/env/php -c /home/work/env/php/etc
     echo "Done"
     ;;
     stop|Stop) echo "Stopping..."
     kill -INT `cat /home/work/env/php/var/run/php-fpm.pid`
     echo "Done"
     ;;
     restart|Restart) echo "Restarting..."
     kill -USR2 `cat /home/work/env/php/var/run/php-fpm.pid`
     echo "Done"
     ;;
     count|Count) echo "Count..."
     ps aux  | grep -c php-fpm
     echo "Done"
     ;;
     *)usage
     ;;
    esac

    9、检测是否启动:

    ps aux |grep php-fpm

    看看是不是有很多个进程(大概20多个)。

    三、安装nginx

    1、下载nginx软件包:nginx-1.11.3.tar.gz

    2、解压nginx

    tar zxvf nginx-1.11.3.tar.gz

    3、配置编译参数

    cd nginx-1.11.3
    ./configure 
    --prefix=/home/work/env/nginx 
    --with-http_realip_module 
    --with-http_sub_module 
    --with-http_gzip_static_module 
    --with-http_stub_status_module 
    --with-pcre

    报错:./configure: error: the HTTP rewrite module requires the PCRE library.

    解决办法:yum -y install pcre-devel

    报错:./configure: error: the HTTP gzip module requires the zlib library.

    解决办法:yum install -y zlib-devel

    4、编译nginx

    make

    安装nginx

    make install

    8、更改nginx配置

    首先把原来的配置文件清空: 建议mv备份

    /home/work/env/nginx/conf/nginx.conf

    “>” 这个符号为重定向的意思,单独用它,可以把一个文本文档快速清空。

    vim /home/work/env/nginx/conf/nginx.conf
    user work work;
    worker_processes 2;
    error_log /home/work/env/nginx/logs/nginx_error.log crit;
    pid /home/work/env/nginx/logs/nginx.pid;
    worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /home/work/env/nginx/client_body_temp;
    proxy_temp_path /home/work/env/nginx/proxy_temp;
    fastcgi_temp_path /home/work/env/nginx/fastcgi_temp;
    fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /home/work/env/nginx/html;
    location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    } } }
    user work;
    worker_processes  8;
    
    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
        fastcgi_connect_timeout 120;
        fastcgi_send_timeout 120;
        fastcgi_read_timeout 120;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;
        tcp_nodelay        on;
        client_max_body_size 128m;
    
        gzip  on;
        gzip_min_length  1000;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_types       text/plain application/x-javascript text/css application/xml application/json;
        gzip_comp_level 2;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
    user work;
    worker_processes  8;
    
    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
        fastcgi_connect_timeout 120;
        fastcgi_send_timeout 120;
        fastcgi_read_timeout 120;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;
        tcp_nodelay        on;
        client_max_body_size 128m;
    
        gzip  on;
        gzip_min_length  1000;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_types       text/plain application/x-javascript text/css application/xml application/json;
        gzip_comp_level 2;
    
        #gzip  on;
    
        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
        #
            #}
    
            # 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 hosts/*.conf;
    }
    View Code

    9、保存配置后,先检验一下配置文件是否有错误存在:

    /home/work/env/nginx/sbin/nginx-t
    如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件:
    nginx: the configuration file /home/work/env/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /home/work/env/nginx/conf/nginx.conf test is successful
    10、启动nginx:
    /home/work/env/nginx/sbin/nginx -c /home/work/env/nginx/conf/nginx.conf
    重载: /home/work/env/nginx/sbin/nginx -s reload
    如果不能启动,请查看 “/home/work/env/nginx/logs/error.log” 文件,检查nginx是否启动:
    11、psaux |grep nginx

    看是否有进程。

    12、测试是否解析php文件

    创建测试文件:

    vim /usr/local/nginx/html/2.php
    内容如下:
    <?php
    phpinfo();
    ?>
    13、防火墙添加80端口

    firewall-cmd --permanent --zone=public --add-port=80/tcp #添加80端口

    firewall-cmd --reload #重新加载

    firewall-cmd --list-port #查看是否添加成功

    14、测试:
    [root@localhost nginx]# curl localhost/2.php
    或者使用浏览器打开http://ip/2.php
    15、重要:如果解析不了,检查日志发现连接不到php,我的php版本为5.625,比较新的版本,需要在php/etc/php-fpm.conf文件中添加
    listen.owner = work
    listen.group = work

    这两行,再重启一下服务就能使用php了

    原因是/home/work/env/php/php-fcgi.sock这个文件没有读权限

    至此,最新版的LNMP环境源码编译安装完成了

     docker容器里安装需要把端口映射到本地上:

    登陆到172.18.1.34 (服务器ip)

    机器上执行:

    ssh -C -f -N -g -L 8080:127.0.0.1:80 172.18.1.144    

    目的把144的80端口映射到 34的8080上

    /home/work/env
  • 相关阅读:
    批量修改数据后应收集统计信息
    this,this,再次讨论javascript中的this,超全面
    javascript中的闭包,超简单论述,保证小学生必懂
    有四中方法可以实现PHP的伪静态,你造吗?
    javascript 事件传播与事件冒泡,W3C事件模型
    浅谈asp.net性能
    div浮动层 兼容IE FF
    我们这些程序员
    c++学习笔记_1
    windows下hexo+github搭建个人博客
  • 原文地址:https://www.cnblogs.com/liuyansheng/p/6065296.html
Copyright © 2020-2023  润新知