美文网首页
CentOS 7 搭建lnmp

CentOS 7 搭建lnmp

作者: 洗耳恭听_kai | 来源:发表于2020-04-28 22:17 被阅读0次

    引言:之前使用的是lnmp.org的一键搭建环境,现在自己手动搭建一个Lnmp环境来。搭建过程中遇到不少问题,在网上搜索“centos7搭建lnmp”,会有很多相关文章(额我现在这篇也是了,哈哈),但如果完全按照其中一篇文章从头到尾的复制粘贴的话,不说很有可能搭建不成功,出很多问题,也得不到学习的效果。那我是搜到其中一篇逻辑还行的文章,按照步骤来敲,然后遇到问题会单独拎出问题去寻找解决方法,好了,废话不多说,这里啰嗦了几句,以下是我记录的搭建过程

    一、准备工作

    1、首先搭建好centos环境后,查看ip:ifconfig,获取到虚拟机的ip,用xshell等软件来连接(原系统里操作实在是在难受了)
    2、安装相关工具:yum(系统自带了)

    yum -y install vim
    yum -y install net-tools
    yum -y install gcc-c++
    

    二、安装nginx

    • 安装pcre-devel
      yum -y install pcre-devel

    • 安装gzip
      yum -y install zlib-devel

    • 安装openssl
      yum -y install openssl-devel

    • 下载nginx
      nginx官方下载地址
      http://nginx.org/en/download.html

      获取目前最新稳定版
      wget http://nginx.org/download/nginx-1.16.1.tar.gz

    • 解压nginx压缩包
      tar zxvf nginx-1.16.1.tar.gz

    • 进入目录
      cd nginx-1.16.1

    • 进行编译安装
      ./configure --prefix=/usr/local/nginx

    • 然后
      make && make install
      会安装到/usr/local/nginx

    • 启动nginx
      /usr/local/nginx/sbin/nginx

    • 查看nginx是否启动
      ps -ef|grep nginx

      查看nginx是否启动

    三、安装mysql

    一、安装YUM Repo

    1、由于CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。

    下载命令:(注意下载位置)

    wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

    2、然后进行repo的安装:

    rpm -ivh mysql57-community-release-el7-9.noarch.rpm

    执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo mysql-community-source.repo

    二、使用yum命令即可完成安装

    注意:必须进入到 /etc/yum.repos.d/目录后再执行以下脚本

    1、安装命令:

    yum install mysql-server

    2、启动msyql:

    systemctl start mysqld

    3、获取安装时的临时密码(在第一次登录时就是用这个密码):

    grep 'temporary password' /var/log/mysqld.log

    mysql -uroot -p临时密码


    临时密码

    这里有个问题,就是临时密码里可能会包含/或者分号;这样的,就会在输入密码的时候报错,/这样的可以在第二行输入密码就可以解决,mysql -uroot -p 然后回车,再输入密码,关于分号我到现在还没找到解决方法,待后续

    4、倘若没有获取临时密码,则

    4.1、删除原来安装过的mysql残留的数据

    rm -rf /var/lib/mysql

    4.2.再启动mysql

    systemctl start mysqld

    修改设置密码强度:

    • 首先需要设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW 即可,
      输入设值语句 “ set global validate_password_policy=LOW; ” 进行设值,

    • 当前密码长度为 8 ,如果不介意的话就不用修改了,按照通用的来讲,设置为 6 位的密码,设置 validate_password_length 的全局参数为 6 即可,
      输入设值语句 “ set global validate_password_length=6; ” 进行设值,

    • 现在可以为 mysql 设置简单密码了,只要满足六位的长度即可,
      输入修改语句 “ ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ” 可以看到修改成功,表示密码策略修改成功了。

    注:在默认密码的长度最小值为 4 ,由 大/小写字母各一个 + 阿拉伯数字一个 + 特殊字符一个,
    只要设置密码的长度小于 3 ,都将自动设值为 4 ,

    关于 mysql 密码策略相关参数

    1)、validate_password_length 固定密码的总长度;

    2)、validate_password_dictionary_file 指定密码验证的文件路径;

    3)、validate_password_mixed_case_count 整个密码中至少要包含大/小写字母的总个数;

    4)、validate_password_number_count 整个密码中至少要包含阿拉伯数字的个数;

    5)、validate_password_policy 指定密码的强度验证等级,默认为 MEDIUM;

    关于 validate_password_policy 的取值:
    LOW:只验证长度;
    1/MEDIUM:验证长度、数字、大小写、特殊字符;
    2/STRONG:验证长度、数字、大小写、特殊字符、字典文件;

    6)、validate_password_special_char_count 整个密码中至少要包含特殊字符的个数;

    四、安装PHP

    • 获取PHP

    可自行选择php版本
    http://php.net/

    这里用的7.1.9
    wget -O php-7.1.9.tar.gz http://am1.php.net/get/php-7.1.9.tar.gz/from/this/mirror

    • 解压
      tar zxvf php-7.1.9.tar.gz

    • 进入目录
      cd php-7.1.9

    • 安装编译时需要的依赖包
      yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libxslt libxslt-devel

    • php 安装配置

    注:(下面这个是原先的,正确的在后面,为记录遇到的问题,把过程说一下)
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-xmlrpc --with-openssl --with-mcrypt --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache

    遇到的问题如下:

    1、configure: error: Please reinstall the BZip2 distribution

    解决:yum -y install bzip2 bzip2-devel

    2、configure: error: Unable to locate gmp.h

    解决:yum -y install gmp-devel

    3、configure: error: mcrypt.h not found. Please reinstall libmcrypt.

    方法一:
    yum install -y epel-release
    yum install -y libmcrypt-devel
    两个不能一起安装,因为CentOs6默认的yum源没有 libmcrypt-devel这个包,只能借助epel的yum源,所以先安装epel,再安装
    libmcrypt。

    4、configure: error: Please reinstall readline - I cannot find readline.h

    解决:yum -y install readline-devel

    5、configure: error: system libzip must be upgraded to version >= 0.11

    解决:
    yum -y remove libzip-devel
    1 、wget https://libzip.org/download/libzip-1.3.2.tar.gz
    2 、tar xvf libzip-1.3.2.tar.gz
    3 、cd libzip-1.3.2
    4 、./configure
    5 、make && make install
    详见:https://www.cnblogs.com/itbsl/p/10208926.html

    6、configure: error: off_t undefined; check your library configuration

    解决:
    vim /etc/ld.so.conf
    添加如下几行
    /usr/local/lib64
    /usr/local/lib
    /usr/lib
    /usr/lib64
    保存退出
    :wq
    ldconfig -v # 使之生效

    7、configure: WARNING: unrecognized options: --with-mcrypt, --enable-gd-native-ttf, --with-libmbfl

    php7不支持这几个,去掉就行了
    所以,正确的配置:
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-xmlrpc --with-openssl --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache

    检查配置成功
    • 编译安装
      make && make install
    编译安装成功
    • 配置php环境变量
      vim /etc/profile
      在末尾加上
      PATH=$PATH:/usr/local/php/bin
      export PATH
      保存后使之立即生效
      source /etc/profile

    • 查看PHP版本
      php -v

      查看php版本

    五、配置nginx支持php-fpm

    php-fpm相关配置
    php的默认安装位置在 /usr/local/php

    对php-fpm运行用户进行设置

    • 首先创建web用户
      用户 组都设置为www-data,可以自己定(实际上在配置时已经设为www-data了)
      groupadd www-data
      useradd -g www-data www-data

    • 复制一份php-fpm配置文件
      cd /usr/local/php/etc
      cp php-fpm.conf.default php-fpm.conf

    • 打开 php-fpm.d目录,复制默认配置文件并打开修改
      cd php-fpm.d
      cp www.conf.default www.conf
      vim www.conf
      将user和group都改成www-data

      image.png
    • 保存退出,重启php-fpm服务
      /usr/local/php/sbin/php-fpm

    • php-fpm默认使用9000端口,使用如下命令可查看是否成功启动
      netstat -lnt | grep 9000

      启动php-fpm

    nginx相关配置

    • 打开nginx配置文件
      vim /usr/local/nginx/conf/nginx.conf
      修改用户组
      1、第一行注释去掉 nobody改为www-data
      2、添加index.php,使nginx默认使用index.php为入口页
      添加Index.php
      3、配置php-fpm模块
    nginx.conf

    注释去掉,将/scripts改为$document_root

    nginx.conf
    • 以上都修改后保存退出
    • 先检测相关配置是否正确
      /usr/local/nginx/sbin/nginx -t
      测试nginx连接
    • 显示正确,重启nginx
      /usr/local/nginx/sbin/nginx -s reload

    • 切到nginx下的html目录(默认代码目录)
      新建index.php

    <?php
    echo 111;
    ?>
    
    • chown www-data /usr/local/nginx/html/ -R #设置目录所有者
      chmod 700 /usr/local/nginx/html/ -R #设置目录权限

    • 关闭并禁用防火墙 (https://blog.csdn.net/Tacks/article/details/83624194)
      systemctl stop firewalld.service
      systemctl disable firewalld.service

    • 浏览器ip访问


      image.png
    • linux中打印:curl http:127.0.0.1/index.php


      image.png

    收工!

    参考:
    https://blog.csdn.net/u011781521/article/details/75675019
    https://blog.csdn.net/huangjinao/article/details/102676468
    https://blog.csdn.net/wohiusdashi/article/details/89358071
    https://www.cnblogs.com/linchuanye/p/10799067.html
    https://blog.csdn.net/aerchi/article/details/83858180
    https://blog.csdn.net/Tacks/article/details/83624194
    https://www.cnblogs.com/ANCAN-RAY/p/8342472.html

    后续

    在检查php编译配置时,一些问题的解决方法

    错误:configure: error: libevent >= 1.4.11 could not be found 
    
    解决:yum -y install libevent libevent-devel 
    
    
    
    错误:configure: error: Please reinstall the mysql distributio 
    
    解决:yum -y install mysql-devel 
    
    
    
    错误:make: *** [sapi/fpm/php-fpm] error 1 
    
    解决:用make ZEND_EXTRA_LIBS='-liconv'编译 
    
    
    
    错误:configure: error: XML configuration could not be found 
    
    解决:yum -y install libxml2 libxml2-devel 
    
    
    
    错误:configure: error: No curses/termcap library found 
    
    解决:yum -y install ncurses ncurses-devel 
    
    
    
    错误:configure: error: xml2-config not found 
    
    解决:yum -y install libxml2 libxml2-devel 
    
    
    
    错误:configure: error: Cannot find OpenSSL's <evp.h> 
    
    解决:yum install openssl openssl-devel 
    
    
    
    错误:configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/ 
    
    解决:yum install curl curl-devel 
    
    
    
    错误:configure: error: Cannot find ldap.h 
    
    解决:yum install openldap openldap-devel 
    
    
    
    错误:configure: error: libjpeg.(a|so) not found 
    
    解决:yum install libjpeglibjpeg -devel 
    
    
    
    错误:configure: error: libpng.(a|so) not found. 
    
    解决:yum install libpnglibpng –devel 
    
    
    
    错误:onfigure: error: freetype.h not found. 
    
    解决:yum install freetype-devel 
    
    
    
    错误:configure: error: cannot find output from lex; giving up 
    
    解决:yum -y install flex 
    
    
    
    错误:configure: error: mod_deflate has been requested but can not be built due to prerequisite failures 
    
    解决:yum -y install zlib-devel openssl-devel 
    
    
    
    错误:Configure: error: Unable to locate gmp.h 
    
    解决:yum install gmp-devel 
    
    
    
    错误:Configure: error: Cannot find MySQL header files under /usr. 
    
    Note that the MySQL client library is not bundled anymore! 
    
    解决:yum install mysql-devel 
    
    更多的补充内容:
    
    安装php: ./configure 
    configure: error: XML configuration could not be found 
    
    yum -y install libxml2 libxml2-devel 
    
    Cannot find OpenSSL's <evp.h> 
    yum install openssl openssl-devel 
    
    1) Configure: error: xml2-config not found. Please check your libxml2 installation. 
    #yum install libxml2 libxml2-devel (For RedHat & Fedora) 
    # aptitude install libxml2-dev (For Ubuntu) 
    
    2) Checking for pkg-config… /usr/bin/pkg-config 
    configure: error: Cannot find OpenSSL's <evp.h> 
    #yum install openssl openssl-devel 
    
    3) Configure: error: Please reinstall the BZip2 distribution 
    # yum install bzip2 bzip2-devel 
    
    4) Configure: error: Please reinstall the libcurl distribution - 
    easy.h should be in <curl-dir>/include/curl/ 
    # yum install curl curl-devel (For RedHat & Fedora) 
    # install libcurl4-gnutls-dev (For Ubuntu) 
    
    5) Configure: error: libjpeg.(also) not found. 
    # yum install libjpeg libjpeg-devel 
    
    6) Configure: error: libpng.(also) not found. 
    # yum install libpng libpng-devel 
    
    7) Configure: error: freetype.h not found. 
    #yum install freetype-devel 
    
    8) Configure: error: Unable to locate gmp.h 
    # yum install gmp-devel 
    
    9) Configure: error: Cannot find MySQL header files under /usr. 
    Note that the MySQL client library is not bundled anymore! 
    # yum install mysql-devel (For RedHat & Fedora) 
    # apt-get install libmysql++-dev (For Ubuntu) 
    
    10) Configure: error: Please reinstall the ncurses distribution 
    # yum install ncurses ncurses-devel 
    
    11) Checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h' not found! 
    # yum install unixODBC-devel 
    
    12) Configure: error: Cannot find pspell 
    # yum install pspell-devel 
    
    13) configure: error: mcrypt.h not found. Please reinstall libmcrypt. 
    # yum install libmcrypt libmcrypt-devel (For RedHat & Fedora) 
    # apt-get install libmcrypt-dev 
    
    14) Configure: error: snmp.h not found. Check your SNMP installation. 
    # yum install net-snmp net-snmp-devel 
    
    15) 
    /usr/bin/ld: cannot find -lltdl 
    collect2: ld returned 1 exit status 
    make: *** [sapi/cgi/php-cgi] Error 1 
    # yum install libtool-ltdl.x86_64 libtool-ltdl-devel.x86_64 
    
    16) 
    为php编译xcache模块的时候,需要运行phpize 
    得到了一个错误 
    #/usr/local/php/bin/phpize 
    Configuring for: 
    PHP Api Version: 20041225 
    Zend Module Api No: 20060613 
    Zend Extension Api No: 220060519 
    Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF 
    environment variable is set correctly and then rerun this script. 
    通过安装 autoconf 可以解决 
    centos下执行 yum install autoconf 即可 
    Ubuntu下执行 apt-get install autoconf 即可 
    17) 
    # /usr/local/php/bin/phpize 
    Cannot find config.m4. 
    Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module 
    修改方法: 
    [root@centos lnmp]# cd php-5.2.14ext/ 
    [root@centos ext]# ./ext_skel --extname=my_module 
    Creating directory my_module 
    Creating basic files: config.m4 config.w32 .cvsignore my_module.c php_my_module.h CREDITS EXPERIMENTAL tests/001.phpt my_module.php [done]. 
    To use your new extension, you will have to execute the following steps: 
    1. $ cd .. 
    2. $ vi ext/my_module/config.m4 
    3. $ ./buildconf 
    4. $ ./configure --[with|enable]-my_module 
    5. $ make 
    6. $ ./php -f ext/my_module/my_module.php 
    7. $ vi ext/my_module/my_module.c 
    8. $ make 
    Repeat steps 3-6 until you are satisfied with ext/my_module/config.m4 and 
    step 6 confirms that your module is compiled into PHP. Then, start writing 
    code and repeat the last two steps as often as necessary. 
    [root@centos ext]# cd my_module/ 
    [root@centos my_module]# vim config.m4 
    根据你自己的选择将 
    dnl PHP_ARG_WITH(my_module, for my_module support, 
    dnl Make sure that the comment is aligned: 
    dnl [ --with-my_module Include my_module support]) 
    修改成 
    PHP_ARG_WITH(my_module, for my_module support, 
    Make sure that the comment is aligned: 
    [ --with-my_module Include my_module support]) 
    或者将 
    dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support, 
    dnl Make sure that the comment is aligned: 
    dnl [ --enable-my_module Enable my_module support]) 
    修改成 
    PHP_ARG_ENABLE(my_module, whether to enable my_module support, 
    Make sure that the comment is aligned: 
    [ --enable-my_module Enable my_module support]) 
    [root@centos my_module]# vim my_module.c 
      将文件其中的下列代码进行修改 
    /* Every user visible function must have an entry in my_module_functions[]. 
    */ 
    function_entry my_module_functions[] = { 
        PHP_FE(say_hello,    NULL) /* ?添加着一行代码 */ 
        PHP_FE(confirm_my_module_compiled,   NULL) /* For testing, remove later. */ 
        {NULL, NULL, NULL}   /* Must be the last line in my_module_functions[] */ 
    }; 
      在文件的最后添加下列代码 
    PHP_FUNCTION(say_hello) 
    { 
        zend_printf("hello sdomain!"); 
    } 
    再修改:php_sdomain.h 
    vi php_sdomain.h 
    在PHP_FUNCTION(confirm_my_module_compiled ); /* For testing, remove later. */ 这行的下面添加一行: 
    PHP_FUNCTION(say_hello); /* For testing, remove later. */ 
      保存文件退出 
      然后我们就可以在这个目录下使用上面的命令了 
      /usr/local/php/bin/phpize 
      执行以后会看到下面的 
      [root@ns sdomain]# /usr/local/php/bin/phpize 
      Configuring for: 
      PHP Api Version:     20020918 
      Zend Module Api No:   20020429 
      Zend Extension Api No:  20050606 
      [root@ns sdomain]# 
      然后执行./configure --with-php-config=/usr/local/php/bin/php-config 
      然后执行make 
      make install 
    然后他会把对应的so文件生成放到PHP安装目录下面的一个文件夹,并提示在在什么地方,然后再把里面的SO文件拷到你存放SO文件的地方 
      即你在php.ini里面的extension_dir所指定的位置 
      最后一步是你在php.ini文件中打开这个扩展 
      extension=sdomain.so 
      然后 
      重新起动apache 
    

    相关文章

      网友评论

          本文标题:CentOS 7 搭建lnmp

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