美文网首页
Centos7下搭建LNMP环境

Centos7下搭建LNMP环境

作者: Zclee | 来源:发表于2019-08-09 16:07 被阅读0次

    一、环境配置及准备工作

    操作系统: centos 7
    mysql 5.7.20
    PHP 7.2
    nginx 12.2
    
    

    准备好源码下载目录

    mkdir /usr/local/src
    cd /usr/local/src
    

    安装必要的库和工具

    # yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap lsof
    

    二、安装

    Mysql

    路径列表:

    安装目录:/usr/local/mysql
    配置文件:/etc/my.cnf
    数据目录:/data/data/mysql
    
    打开Mysql官网下载页:

    https://dev.mysql.com/downloads/mysql/

    找到你要下载的版本如:==mysql-5.7.20-linux-glibc2.12-x86_64.tar==

    下载二进制软件包:
    wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar
    
    解压至安装目录
    tar -xvf mysql-5.7.20-linux-glibc2.12-x86_64.tar -C /usr/local/
    cd /usr/local
    mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
    
    增加mysql用户及用户组
    groupadd mysql
    useradd -s /sbin/nologin -M -g mysql mysql
    cd /usr/local
    chown -R mysql:mysql mysql #修改目录权限
    
    • -s表示指定用户所用的shell,此处为/sbin/nologin,表示不登录。
    • -M表示不创建用户主目录。
    • -g表示指定用户的组名为mysql。
    • 最后的mysql表示用户名。

    准备数据目录

    mkdir /data/data/mysql
    chown -R mysql:mysql /data/data/mysql
    
    执行初始化命令
    ./bin/mysqld --user=mysql [--basedir=/usr/local/mysql] [--datadir=/data/data/mysql] --initialize
    

    安装完成,出现如下信息,将随机生成的==登录密码==记录下来,忘记记录则可以通过日志找回:

    > grep "password" /var/log/mysqld.log
    2016-08-10T15:03:02.210317Z 1 [Note] A temporary password is generated for root@localhost: AYB(&-3Cz-rW
    

    若初始化命令不指定basedir和datadir,也可以在配置文件/etc/my.cnf里面配置。

    my.cnf配置及优化

    参考文章:Mysql5.7优化配置.md

    精简的配置如下:

    [client]
    port    = 3306
    socket   = /var/run/mysqld/mysqld.sock
    
    [mysqld_safe]
    pid-file  = /var/run/mysqld/mysqld.pid
    socket   = /var/run/mysqld/mysqld.sock
    nice    = 0
    
    [mysql]
    default-character-set=utf8
    
    [mysqld]
    user    = mysql
    pid-file  = /var/run/mysqld/mysqld.pid
    socket   = /var/run/mysqld/mysqld.sock
    port    = 3306
    basedir   = /usr/local/mysql
    datadir   = /data/data/mysql
    tmpdir   = /tmp
    lc-messages-dir = /usr/local/mysql/share
    explicit_defaults_for_timestamp
    
    default-storage-engine=INNODB
    character_set_server=utf8
    
    log-bin=mysql-bin
    server-id=1
    binlog-do-db=test
    binlog-ignore-db=mysql
    sync_binlog=1
    binlog_checksum=none
    binlog_format=mixed
    
    log-error = /var/log/mysql/error.log
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    

    当然,mysql5.7是不需要配置文件也能直接运行的。

    开启Mysql服务
    cp support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    

    开机自动运行

    chkconfig mysqld on
    
    修改初始化密码
    mysql -u root -p
    password: 
    
    mysql> update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
    mysql> flush privileges;
    mysql> quit;
    
    出现错误信息
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    

    解决方法:

    mysql > alter user 'root'@'localhost' identified by '123456';
    mysql > flush privileges;
    
    重启 mysql
    systemctl restart mysql.service
    

    PHP 7

    路径列表:
    安装目录:/usr/local/php
    配置文件: /usr/local/php/etc/php.ini
    php-fpm配置文件:/usr/local/php/etc/php-fpm.conf
    
    下载源码包:

    官网下载地址:

    http://php.net/downloads.php

    cd /usr/local/src
    wget http://cn2.php.net/distributions/php-7.2.0.tar.gz
    tar -xvf php-7.2.0.tar.gz
    
    编译前准备

    安装openssl

    在使用服务器时我会开启并使用SSL,所以编译安装php时会在环境中加入SSL模块,因为openssl1.0.1版本会有心脏滴血漏洞,所以这里需要更换OpenSSL版本

    # 下载openssl1.0.2版本:
    wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz
    tar -zxvf openssl-1.0.2-latest.tar.gz
    
    # 编译安装openssl-1.0.2
    cd  openssl-1.0.2j
    ./config shared zlib
    make depend
    make && make install
    
    # 修改历史的OpenSSL文件设置备份:
    mv /usr/bin/openssl /usr/bin/openssl.old
    mv /usr/include/openssl /usr/include/openssl.old
    
    # 设置软连接使其使用新的OpenSSL版本 刚刚安装的OpenSSL默认安装在/usr/local/ssl下:
    ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
    ln -s /usr/local/ssl/include/openssl /usr/include/openssl
    
    # 更新动态链接库数据:
    echo "/usr/local/ssl/lib/" >> /etc/ld.so.conf
    export LD_LIBRARY_PATH=/usr/local/ssl/lib/
    ldconfig -v
    
    # 查看openssl版本
    > openssl version
    OpenSSL 1.0.2j  26 Sep 2016
    
    # 执行命令查看openssl依赖库版本是否为1.0.2j:
    strings /usr/local/ssl/lib/libssl.so | grep OpenSSL
    

    安装 libmcrypt

    wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
    tar -zxvf libmcrypt-2.5.8.tar.gz
    cd libmcrypt-2.5.8/
    ./configure --prefix=/usr/local
    make && make install
    

    修改curl的安装,使其支持openssl:

    PS.如果不支持openssl,CURL无法访问https,在php中同样也无法使用CURL访问https:

    > curl -V
    curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
    Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz
    

    很明显可以看到是NSS模式,不支持openssl,接下来重新安装curl:

    去官网查看curl最新版本已经是7.51.0,并且支持http2,所以建议重新编译CURL并且使之支持http2,为了让 curl 支持 HTTP2 我们需要安装 nghttp2(http2 的 C 语言库),nghttp2的地址:nghttp2,git仓库:nghttp2的git仓库,使用source方式安装nghttp2:

    git clone https://github.com/nghttp2/nghttp2.git
    cd nghttp2
    autoreconf -i
    automake
    autoconf
    ./configure
    make && make install
    

    安装CURL7.51.0:

    wget https://curl.haxx.se/download/curl-7.51.0.tar.gz
    tar -zxvf curl-7.51.0.tar.gz
    cd curl-7.51.0/
    ./configure --prefix=/usr/local/curl --without-nss --with-ssl=/usr/local/ssl --with-nghttp2=/usr/local --with-gssapi --with-libmetalink --with-libssh2 --enable-tls-srp --enable-sspi
    make && make install
    echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
    ldconfig
    mv /usr/bin/curl /usr/bin/curl.old
    ln -s /usr/local/curl/bin/curl /usr/bin/
    

    再查看 curl -V

    > curl -V
    curl 7.51.0 (x86_64-pc-linux-gnu) libcurl/7.51.0 OpenSSL/1.0.2j zlib/1.2.7 libssh2/1.4.3 nghttp2/1.18.0-DEV
    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
    Features: IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets Metalink
    
    编译PHP

    内存1G以下请在结尾加上:–disable-fileinfo,

    cd /usr/local/php-7.2.0
    ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --enable-rpath --enable-bcmath --enable-calendar --enable-debug --enable-exif --enable-fileinfo --enable-filter --enable-fpm --enable-ftp --enable-gd-jis-conv --enable-gd-native-ttf --enable-hash --enable-inline-optimization --enable-json --enable-libxml --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-mysqlnd --enable-opcache --enable-opcache-file --enable-pcntl --enable-pdo --enable-session --enable-shared --enable-shmop --enable-simplexml --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-xml --enable-zip --enable-ctype --with-bz2 --with-curl=/usr/local/curl --with-fpm-user=www --with-fpm-group=www --with-freetype-dir=/usr/include/freetype2/freetype --with-gd --with-gettext --with-gmp --with-iconv --with-iconv-dir=/usr --with-jpeg-dir=/usr --with-mcrypt=/usr/local --with-mhash --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=mysqlnd --with-openssl=/usr/local/ssl --with-pdo-mysql=mysqlnd --with-pear --with-png-dir=/usr --with-xmlrpc --with-zlib --with-libxml-dir=/usr 
    make && make install
    make test
    
    

    安装完成之后将php的程序引用至/usr/bin中:

    ln -fs /usr/local/php/bin/* /usr/bin/
    ln -fs /usr/local/php/sbin/* /usr/sbin/
    

    查看版本:

    > php -v
    PHP 7.1.0 (cli) (built: Dec  2 2016 19:07:57) ( ZTS DEBUG )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
    

    现在php7已经安装完成,开始设置PHP7的配置文件php.ini,设定php.ini:

    cp php.ini-production /usr/local/php/etc/php.ini
    ln -fs /usr/local/php/etc/php.ini /etc/php.ini
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    ln -fs /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
    

    更改配置,使php7支持opcache:

    vim /usr/local/php/etc/php.ini
    
    # 找到[opcache], 加入代码
    zend_extension=opcache.so
    opcache.enable=1
    opcache.enable_cli=1
    opcache.file_cache="/tmp/opcache" # 注意目录是否存在
    opcache.revalidate_freq=60
    opcache.validate_timestamps=1
    opcache.max_accelerated_files=1000
    opcache.memory_consumption=512
    opcache.interned_strings_buffer=16
    opcache.fast_shutdown=1
    
    > php -v
    PHP 7.1.0 (cli) (built: Dec  2 2016 19:07:57) ( ZTS DEBUG )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
        with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies
    

    修改fpm配置:

    vim /usr/local/php/conf/php-fpm.conf
    
    # 修改
    pid = run/php-fpm.pid
    error_log = log/php-fpm.log
    
    启动PHP
    
    # 启动php-fpm:
    php-fpm -D
    
    # 停止php-fpm的命令如下:
    kill -INT `cat /usr/local/php7/var/run/php-fpm.pid`
    
    # 重启php-fpm的命令:
    kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`
    
    # 设置php-fpm开机启动:
    echo -e 'php-fpm -D\n' >> /etc/rc.local
    
    # PS.为避免rc.local中设定的程序无法自动启动,执行如下命令:
    
    sudo chmod +x /etc/rc.d/rc.local
    

    Nginx

    增加 www
    groupadd www
    useradd -g www -s /sbin/nologin -M www
    
    路径列表:
    安装目录:/usr/local/nginx
    配置目录: /usr/local/nginx/conf
    
    准备工作:

    在编译nginx的时候需要pcre、openssl、zlib的源码,所以首先要下载这三个扩展对应的源码包,openssl在安装php时已经下载了,就不需要重新下载了,下载之前首先查看对应版本:

    yum info pcre zlib
    

    得到的结果是pcre的版本是==8.32==,zlib对应的版本是==1.2.7==,下载各自的源码:

    wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.32/pcre-8.32.tar.gz
    wget http://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.7/zlib-1.2.7.tar.gz
    

    解压,但不用安装:

    tar -xvf pcre-8.32.tar.gz
    tar -xvf zlib-1.2.7.tar.gz
    
    安装必要的库及工具

    安装libunwind

    wget http://download.savannah.gnu.org/releases/libunwind/libunwind.1.tar.gz
    tar zxvf libunwind-1.1.tar.gz
    cd libunwind-1.1
    CFLAGS=-fPIC ./configure
    make CFLAGS=-fPIC
    make CFLAGS=-fPIC install
    

    安装gperftools

    wget https://github.com/gperftools/gperftools/releases/download/gpftools-2.5/gperftools-2.5.tar.gz
    tar zxvf gperftools-2.5.tar.gz
    cd gperftools-2.5/
    ./configure
    make && make install
    
    安装Nginx

    官方网站下载:

    http://nginx.org/en/download.html

    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar -xvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    ./configure --prefix=/usr/local/nginx --user=www --group=www --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-libatomic --http-client-body-temp-path=/tmp/nginx_client --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --with-http_addition_module  --with-mail --with-mail_ssl_module --with-ipv6 --with-pcre=/usr/local/src/pcre-8.32 --with-zlib=/usr/local/src/zlib-1.2.7 --with-openssl=/usr/local/src/openssl-1.0.2j --with-google_perftools_module
    make && make install
    

    PS.需要注意的是,这里的--with-pcre、--with-zlib、--with-openssl以及ngx_pagespeed模块都是源码路径,而不是编译安装之后的路径。

    三、配置环境变量

    > vim /etc/profile
    
    # 在底部加入以下内容:
    PHP_HOME=/usr/local/php
    NGINX_HOME=/usr/local/nginx
    MYSQL_HOME=/usr/local/mysql
    export PATH=$PATH:$PHP_HOME/bin/:$PHP_HOME/sbin:$NGINX_HOME/sbin:$MYSQL_HOME/bin
    
    > qw
    > source /etc/profile # 使配置立即生效
    

    四、防火墙配置

    # nginx
    #为public域开放tcp协议的80端口
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    #为public域添加http服务
    firewall-cmd --zone=public --add-service=http --permanent
    
    # mysql
    #为public域开放tcp协议的3306端口
    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    #为public域添加mysql服务
    firewall-cmd --zone=public --add-service=mysql --permanent
    
    #重启firewall服务
    firewall-cmd --reload
    

    相关文章

      网友评论

          本文标题:Centos7下搭建LNMP环境

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