编译安装LNMP2

作者: Miracle001 | 来源:发表于2018-10-29 11:05 被阅读0次
    单机运行LNMP
    centos 1708
    
    cd /usr/local/src/
    
    yum -y install gcc gcc-c++ openssl-devel cyrus-sasl-md5
    yum -y install automake autocon libtool make glibc
    
    autocon显示没有可用包
    
    下载wget
    yum -y install wget
    wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz
    tar xzvf pcre-8.41.tar.gz
    
    下载zlib
    wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
    tar xzvf zlib-1.2.11.tar.gz
    
    下载openssl
    wget https://www.openssl.org/source/openssl-1.1.0b.tar.gz
    tar -zxvf openssl-1.1.0b.tar.gz
    
    为nginx添加用户及组
    groupadd -r www
    useradd -r -g www www
    
    

    编译安装nginx

    
    下载nginx
    wget http://nginx.org/download/nginx-1.14.0.tar.gz
    tar xzf nginx-1.14.0.tar.gz
    
    cd nginx-1.14.0
    
    ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_mp4_module  --with-http_stub_status_module --with-select_module --with-poll_module --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log  --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.1.0b
    完成--如下图1
    
    make && make install
    完成--如下图2
    
    1
    2
    设置nginx配置文件
    cd /usr/local/nginx/
    
    mkdir -pv conf/vhosts
    mkdir logs/{access,error}
    mkdir -p /data/webapps
    
    vim /usr/local/nginx/nginx.conf
    user  www www;
    worker_processes  4;
    
    error_log  /usr/local/nginx/logs/error.log crit;
    
    pid        /usr/local/nginx/logs/nginx.pid;
    
    worker_rlimit_nofile 65535;
    
    events {
        use epoll;
        worker_connections  65535;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        access_log  /usr/local/nginx/logs/access.log;
    
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 8m;
    
        sendfile        on;
        tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        gzip  on;
        gzip_min_length     1k;
        gzip_buffers        4    16k;
        gzip_http_version   1.0;
        gzip_comp_level     2;
        gzip_types          text/plain  application/x-javascript  text/css  application/xml;
        gzip_vary           on;
    
    
        tcp_nodelay         on;
    
    
        fastcgi_connect_timeout     300;
        fastcgi_send_timeout     300;
        fastcgi_read_timeout     300;
        fastcgi_buffer_size      64k;
        fastcgi_buffers          4   64k;
        fastcgi_busy_buffers_size     128k;
        fastcgi_temp_file_write_size  128k;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        include   /usr/local/nginx/conf/vhosts/*.conf;
        include   /usr/local/nginx/conf/proxy/*.conf;
    }
    
    
    引用了/usr/local/nginx/conf/vhosts/目录下所有后缀名是.conf的配置文件,
    现在进入该目录编写一个默认配置文件default.conf,其内容是
    
    vim /usr/local/nginx/conf/vhosts/default.conf 
    server {
        listen       80;
        server_name  localhost;
        index  index.html;
        root   /data/webapps;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
               expires  30d;
        }
    
        location ~ .*\.(js|css)?$ {
               expires  1h;
    
        }
    
    
        #伪静态规则
        include     /usr/local/nginx/conf/rewrite/default.conf;
        access_log  /usr/local/nginx/logs/access/default.log;
        error_log   /usr/local/nginx/logs/error/default.log;
    }
    
    
    其中引入了一个/usr/local/nginx/conf/rewrite/default.conf文件,
    这个文件是用来编写rewrite规则的配置文件,用来实现伪静态,其内容是
    
    mkdir /usr/local/nginx/conf/rewrite
    
    vim /usr/local/nginx/conf/rewrite/default.conf 
    rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
    rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last;
    rewrite ^(.*)/data/(.*)\.(html|php)$ 404.html last;
    rewrite ^(.*)/attachment/(.*)\.(html|php)$ 404.html last;
    rewrite ^(.*)/html/(.*)\.(html|php)$ 404.html last;
    
    
    启动完成
    /usr/local/nginx/sbin/nginx
    ps aux |grep nginx
    ps -e |grep nginx
    
    
    加入系统环境变量
    vim /etc/profile
    在文件最末尾加上如下代码
    export NGINX_HOME=/usr/local/nginx
    export PATH=$PATH:$NGINX_HOME/sbin
    
    . /etc/profile  重读配置文件  或者 source  /etc/profile
    
    echo $PATH
    命令查看环境变量中是否已经加入了相关的路径
    
    
    3
    加入系统服务
    
    在/etc/init.d/目录下创建一个nginx文件
    
    vim /etc/init.d/nginx    文件内容如下
    
    
    #!/bin/bash
    # chkconfig: - 85 15
    PATH=/usr/local/nginx
    DESC="nginx daemon"
    NAME=nginx
    DAEMON=$PATH/sbin/$NAME
    CONFIGFILE=$PATH/$NAME.conf
    PIDFILE=$PATH/logs/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME
    set -e
    [ -x "$DAEMON" ] || exit 0
    do_start() {
    $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
    }
    do_stop() {
    $DAEMON -s stop || echo -n "nginx not running"
    }
    do_reload() {
    $DAEMON -s reload || echo -n "nginx can't reload"
    }
    case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    do_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    do_stop
    echo "."
    ;;
    reload|graceful)
    echo -n "Reloading $DESC configuration..."
    do_reload
    echo "."
    ;;
    restart)
    echo -n "Restarting $DESC: $NAME"
    do_stop
    do_start
    echo "."
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" 
    exit 3
    ;;
    esac
    exit 0
    
    给其赋予可执行的权限,具体命令如下
    
    chmod +x /etc/init.d/nginx
    
    首先要确认之前打开的nginx进程已被杀死,
    ps aux|grep nginx;kill 28150
    
    
    systemctl enable nginx  或者  /sbin/chkconfig nginx on  设置nginx开机自启动
    
    /bin/systemctl start nginx
    ps aux|grep nginx  启动成功
    
    其他命令:不用执行
    /bin/systemctl stop nginx
    /bin/systemctl reload nginx
    
    

    编译安装PHP

    cd /usr/local/src/
    
    下载地址:http://php.net/releases/
    包名:php-7.1.17.tar.gz
    解压:tar xzvf php-7.1.17.tar.gz
    
    安装依赖包
    yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel gcc openssl-devel libmcrypt-devel
    
    cd php-7.1.17
    
    编译:
    
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-zlib-dir --with-mhash --with-mcrypt --with-openssl-dir --with-jpeg-dir --enable-gd-jis-conv --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-maintainer-zts --enable-zip
    
    执行完成,如下图1
    
    make
    make install 
    编译成功,如下图2
    
    
    1
    2
    设置php配置文件
    将官方提供的配置模板拷贝到配置参数中所指定的目录中
    
    cp php.ini-production /usr/local/php/etc/php.ini
    然后可以根据自己服务器的需求,对php.ini文件的内容进行修改
    
    
    设置php-fpm配置文件,依次执行以下命令
    
    cd /usr/local/php/etc/
    cp php-fpm.conf.default php-fpm.conf
    vim php-fpm.conf  内容进行如下修改
    pid = /usr/local/php/var/run/php-fpm.pid
    
    cd /usr/local/php/etc/php-fpm.d/
    
    cp www.conf.default www.conf
    vim www.conf  内容进行如下修改
    user = www
    group = www
    listen = 127.0.0.1:9000
    pm.max_children = 100
    pm.start_servers = 20
    pm.min_spare_servers = 5
    pm.max_spare_servers = 35
    其他配置可根据自己的情况进行相应修改
    
    
    启动php-fpm
    完成以上配置后,即可启动php-fpm,执行以下命令
    /usr/local/php/sbin/php-fpm 
    
    ps -eF|grep php-fpm
    命令查看进程,若能够看到相关进程,则证明启动成功。查询进程时,可以看到进程是以www用户身份执行的
    
    
    加入系统环境变量
    vim /etc/profile 在文件最末尾加上如下代码
    export PHP_HOME=/usr/local/php
    export PATH=$PATH:$PHP_HOME/bin:$PATH_HOME/sbin
    
    重新加载配置文件
    source /etc/profile
    或者 . /etc/profile
    
    echo $PATH  查看环境变量中是否已经加入了相关的路径
    
    
    加入系统变量
    加入系统服务
    vim /etc/init.d/php-fpm
    文件内容如下
    
    #!/bin/bash
    # php-fpm startup script for the php-fpm 
    # php-fpm version:7.1.17
    # chkconfig: - 85 15
    # description: php-fpm
    # processname: php-fpm
    # pidfile: /usr/local/php/var/run/php-fpm.pid
    # config: /usr/local/php/etc/php-fpm.conf
    
    php_command=/usr/local/php/sbin/php-fom
    php_config=/usr/local/php/etc/php-fpm.conf
    php_pid=/usr/local/php/var/run/php-fpm.pid
    RETVAL=0
    prog="php-fpm"
    
    #start function
    php_fpm_start() {
        /usr/local/php/sbin/php-fpm
    }
    
    start(){
        if [ -e $php_pid  ]
        then
        echo "php-fpm already start..."
        exit 1
        fi
        php_fpm_start
    }
    
    stop(){
        if [ -e $php_pid ]
        then
        parent_pid=`cat $php_pid`
        all_pid=`ps -ef | grep php-fpm | awk '{if('$parent_pid' == $3){print $2}}'`
        for pid in $all_pid
        do
                kill $pid
            done
            kill $parent_pid
        fi
        exit 1
    }
    
    restart(){
        stop
        start
    }
    
    # See how we were called.
    case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    restart)
            stop
            start
            ;;
    status)
            status $prog
            RETVAL=$?
            ;;
    *)
            echo $"Usage: $prog {start|stop|restart|status}"
            exit 1
    esac
    exit $RETVAL
    
    其中
    php_command表示php-fpm可执行脚本的路径,
    php_config表示php-fpm的配置文件路径,
    php_pid表示php-fpm进程文件的路径
    
    
    设置php-fpm开机自启动
    经过上面的操作,php-fpm已被添加为系统服务,所以可以将其设置为开机自启动,
    这样就可以避免每次服务器重启之后需要手动开启php-fpm的操作,具体命令如下
    
    chkconfig --add php-fpm
    chkconfig php-fpm on
    或者
    systemctl enable php-fpm
    
    

    配置Nginx+PHP

    配置Nginx+PHP
    
    本文中对Nginx进行了多域名配置,
    每个域名只需要在/usr/local/nginx/conf/vhosts目录下有一个自己的配置文件即可,
    所以重点就在这个配置文件里,
    现在假定我们需要查看当前服务器的php信息,因为本文没有涉及到域名解析及配置的相关信息,
    所以这里就以IP访问为例进行配置讲解,
    首先,需要在我们的web代码根路径/data/webapps下创建一个info.php文件,执行以下命令
    
    vim /data/webapps/info.php
    <?php
    phpinfo();
    ?>
    
    
    对Nginx的配置文件进行修改
    前文已经在/usr/local/nginx/conf/vhosts目录下创建了default.conf配置文件,
    现在对这个文件的内容进行修改,修改后的内容如下
    
    vim /usr/local/nginx/conf/vhosts/default.conf
    server {
        listen       80;
        server_name  localhost;
        index  index.php index.htm index.html;
        root   /data/webapps;
    
        location ~ .*\.(php|php5)?$ {
            #fastcgi_pass unix:/tmp/php-cgi.sock;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
           expires  30d;
        }
    
        location ~ .*\.(js|css)?$ {
           expires  1h;
    
        }
    
    
        # 配置页面静态化
        include  /usr/local/nginx/conf/rewrite/default.conf;
        access_log  /usr/local/nginx/logs/access/default.log;
        error_log   /usr/local/nginx/logs/error/default.log;
    }
    
    与之前的配置文件相比,主要是index配置项后,多了关于php的配置内容,
    以及location相关配置中,多了对于php的内容,保存以上配置文件。
    
    重新加载配置文件
    nginx -t
    nginx -s reload
    
    通过浏览器访问:http://服务器的IP/info.php
    http://192.168.10.88/info.php
    有时候,界面显示"file not "需要等1-2分钟才会刷新出来
    上文中php代码的运行效果,具体如下图所示 
    
    php代码运行结果
    至此,即完成了Nginx和PHP的配置。上述的配置只是最简单的配置;
    若项目不是运行在web根目录下的,那么配置文件也需要对应的进行修改;
    若项目是采用CodeIgniter或Laravel等其他框架技术开发的,那么也需要对配置文件进行相应修改;
    这部分内容大家可自行百度,有很多相关的博客。
    

    安装MySQL

    tar xf  mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar 
    
    yum remove mariadb-libs -y
    
    1
    先后顺序:common-->libs-->client-->server-->devel
    
    可以一起安装
    yum -y install mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm mysql-community-devel-5.7.24-1.el7.x86_64.rpm 
    
    可以全部使用yum安装
    rpm -ivh mysql-community-common-5.7.24-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-5.7.24-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-5.7.24-1.el7.x86_64.rpm
    
    2
    rpm -ivh mysql-community-server-5.7.24-1.el7.x86_64.rpm
    报错,有依赖关系
    
    3
    yum -y install mysql-community-server-5.7.24-1.el7.x86_64.rpm
    yum 安装解决依赖关系
    
    4
    rpm -ivh mysql-community-devel-5.7.24-1.el7.x86_64.rpm
    
    5
    systemctl start mysqld
    systemctl status mysqld
    ss -ntl
    
    
    6
    find / -name mysql
    
    /etc/logrotate.d/mysql
    /etc/selinux/targeted/active/modules/100/mysql
    /var/lib/mysql
    /var/lib/mysql/mysql
    /usr/bin/mysql
    /usr/lib64/mysql
    /usr/share/mysql
    /usr/include/mysql
    /usr/include/mysql/mysql
    
    
    cat /var/log/mysqld.log |grep password
    
    7
    mysql -uroot -p
    set password = password('Root4rfv4rfv!');
    
    ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    密码过于简单,必须包含--字母大小写+数字+符号
    
    8
    show databases;
    
    8
    另外一台MySQL库也是同样这样安装
    
    
    主从配置
    
    master
    vi /etc/my.cnf
    [mysqld]
    server-id = 1
    log-bin = /var/lib/mysql/master-log
    skip_name_resolve = on
    
    systemctl restart mysqld
    
    mysql -uroot -p
    show master status\G
    grant replication client,replication slave on *.* to 'repl'@'192.168.%.%' identified by 'Repl4rfv4rfv!';
    show master status\G
    show binlog events in 'master-log.000001'\G
    
    
    9
    slave
    vi /etc/my.cnf
    [mysqld]
    server-id = 2
    relay-log = /var/lib/mysql/relay-log
    skip_name_resolve = on
    
    systemctl restart mysqld
    
    mysql -uroot -p
    show databases;
    change master to master_host='192.168.25.101',master_user='repl',master_password='Repl4rfv4rfv!',master_log_file='master-log.000001',master_log_pos=467;
    
    10
    show slave status\G
    START SLAVE;
    show slave status\G
    
    
    11
    master
    
    create database mydb;
    use mydb
    create table t1 (id int,name char(50));
    insert into t1 values (1,'fgq');
    show databases;
    
    12
    slave
    
    show databases;
    show tables from mydb;
    
    13
    master  配置文件
    vi /etc/my.cnf
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    
    symbolic-links=0
    
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    
    ####
    gtid_mode = on
    enforce_gtid_consistency = on
    master_info_repository = table
    relay_log_info_repository = table
    read_only = on
    ####
    
    server-id = 88
    port        = 3306
    skip-external-locking
    
    log-bin=mysql-bin
    binlog_format=mixed
    
    
    master  配置文件
    主目录是/var/lib/mysql
    如果主目录是/usr/local/mysql/var/,
    那么把配置文件里的/var/lib/mysql--->/usr/local/mysql/var
    
    vi /etc/my.cnf.d/server.cnf
    [client]
    #password   = your_password
    port        = 3306
    socket      = /tmp/mysql.sock
    
    [mysqld]
    server-id = 88
    port        = 3306
    socket      = /tmp/mysql.sock
    #datadir = /usr/local/mysql/var
    datadir = /var/lib/mysql
    skip-external-locking
    #key_buffer_size = 128M
    key_buffer_size = 384M
    max_allowed_packet = 16M
    table_open_cache = 512
    sort_buffer_size = 16M
    net_buffer_length = 8K
    read_buffer_size = 32M
    read_rnd_buffer_size = 16M
    myisam_sort_buffer_size = 32M
    thread_cache_size = 64
    query_cache_size = 16M
    tmp_table_size = 32M
    performance_schema_max_table_instances = 500
    
    explicit_defaults_for_timestamp = true
    #skip-networking
    max_connections = 500
    max_connect_errors = 100
    open_files_limit = 65535
    
    log-bin=mysql-bin
    binlog_format=mixed
    #server-id   = 1
    expire_logs_days = 10
    early-plugin-load = ""
    
    ####
    gtid_mode = on
    enforce_gtid_consistency = on
    master_info_repository = table
    relay_log_info_repository = table
    read_only = on
    ####
    #loose-innodb-trx=0
    #loose-innodb-locks=0
    #loose-innodb-lock-waits=0
    #loose-innodb-cmp=0
    #loose-innodb-cmp-per-index=0
    #loose-innodb-cmp-per-index-reset=0
    #loose-innodb-cmp-reset=0
    #loose-innodb-cmpmem=0
    #loose-innodb-cmpmem-reset=0
    #loose-innodb-buffer-page=0
    #loose-innodb-buffer-page-lru=0
    #loose-innodb-buffer-pool-stats=0
    #loose-innodb-metrics=0
    #loose-innodb-ft-default-stopword=0
    #loose-innodb-ft-inserted=0
    #loose-innodb-ft-deleted=0
    #loose-innodb-ft-being-deleted=0
    #loose-innodb-ft-config=0
    #loose-innodb-ft-index-cache=0
    #loose-innodb-ft-index-table=0
    #loose-innodb-sys-tables=0
    #loose-innodb-sys-tablestats=0
    #loose-innodb-sys-indexes=0
    #loose-innodb-sys-columns=0
    #loose-innodb-sys-fields=0
    #loose-innodb-sys-foreign=0
    #loose-innodb-sys-foreign-cols=0
    
    default_storage_engine = InnoDB
    innodb_file_per_table = 1
    innodb_data_home_dir = /var/lib/mysql
    innodb_data_file_path = ibdata1:10M:autoextend
    innodb_log_group_home_dir = /var/lib/mysql
    innodb_buffer_pool_size = 512M
    innodb_log_file_size = 128M
    innodb_log_buffer_size = 8M
    innodb_flush_log_at_trx_commit = 1
    innodb_lock_wait_timeout = 50
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    
    [myisamchk]
    key_buffer_size = 128M
    sort_buffer_size = 2M
    read_buffer = 2M
    write_buffer = 2M
    
    [mysqlhotcopy]
    interactive-timeout
    [mysqld]
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    
    

    相关文章

      网友评论

        本文标题:编译安装LNMP2

        本文链接:https://www.haomeiwen.com/subject/ohvrzftx.html