LNMP环境部署(源码编译)

文章目录

    • 1. Nginx
      • 1.1 准备工作
      • 1.2 安装
    • 2. mysql
      • 2.1 准备工作
      • 2.2 安装
      • 2.3 安装后配置
      • 2.4 进行mysql安全配置
      • 2.5 验证登录mysql
    • 3. php
      • 3.1 准备工作
      • 3.2 安装
      • 3.3 安装后配置
      • 3.4 验证php

  • Linux环境(我用的是vmware workstation centos7.6)
  • nginx安装目录:/root/vslnmp/nginx.1.16,记得不要在root目录下面进行安装目录的创建,因为这个问题,在安装中出现了权限问题,排查很麻烦。
  • php安装目录:/usr/local/lnmp/php/7.0.0
  • mysql安装目录:/usr/local/lnmp/mysql.5.7

1. Nginx


1.1 准备工作

  • nginx源码包(点击官网下载)
  • 上传至linux服务器
[root@localhost src]# ls
nginx-1.16.1.tar.gz
  • 安装编译所需工具
[root@localhost src]# yum install gcc gcc-c++ zlib-devel openssl openssl-devel

1.2 安装

# 创建nginx安装目录
[root@localhost ~]# mkdir /root/vslnmp/nginx.1.16
# 解压缩nginx源码包
[root@localhost src]# tar -zxvf nginx-1.16.1.tar.gz
# 进入解压后的目录
[root@localhost nginx-1.16.1]# pwd
/usr/local/src/nginx-1.16.1
[root@localhost nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
# 编译前工具安装
[root@localhost nginx-1.16.1]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel openssl openssl-devel -y
# 进行编译
[root@localhost nginx-1.16.1]# ./configure --prefix=/root/vslnmp/nginx.1.16 --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre --with-http_gzip_static_module --with-http_dav_module   --with-http_addition_module  --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
# 编译成功后执行安装命令
[root@localhost nginx-1.16.1]# make && make install
# 添加nginx用户
[root@localhost nginx-1.16.1]# useradd -s /sbin/nologin -M nginx
[root@localhost ~]# ln  -s  /root/vslnmp/nginx.1.16/sbin/nginx  /usr/local/sbin/
# 编写服务启动脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
PROG="/root/vslnmp/nginx.1.16/sbin/nginx"
PIDF="/root/vslnmp/nginx.1.16/logs/nginx.pid"
case "$1" in
        start)
        $PROG
        ;;
        stop)
        kill -3 $(cat $PIDF)
        ;;
        restart)
        $0 stop &> /dev/null
        if [ $? -ne 0 ] ; then continue ; fi
        $0 start
        ;;
        reload)
        kill -1 $(cat $PIDF)
        ;;
        *)
        echo "Userage: $0 { start | stop | restart | reload }"
        exit 1
esac
exit 0
# 启动nginx
[root@localhost ~]# /etc/init.d/nginx start
# 查看是否有nginx进程
[root@localhost ~]# ps -ef|grep nginx
root      13165      1  0 14:26 ?        00:00:00 nginx: master process /root/vslnmp/nginx.1.16/sbin/nginx
root      13166  13165  0 14:26 ?        00:00:00 nginx: worker process
root      13221   7355  0 15:05 pts/1    00:00:00 grep --color=auto nginx

访问nginx页面:http://ip
LNMP环境部署(源码编译)_第1张图片
至此,nginx安装完成

2. mysql


2.1 准备工作

  • mysql源码包点击下载
  • boost安装包下载LNMP环境部署(源码编译)_第2张图片
  • 上传至linux服务器
[root@localhost src]# ll
total 5351148
-rw-r--r--   1 root root    83709983 Sep 16 16:48 boost_1_59_0.tar.gz
-rw-r--r--   1 root root    51433090 Sep 16 15:42 mysql-5.7.17.tar.gz
  • 安装编译所需工具
[root@localhost src]# yum install -y gcc gcc-c++ automake autoconf make cmake libtool bison bison-devel ncurses ncurses-devel libaio-devel

2.2 安装

  • 编译前准备
# 解压缩 boost与mysql源码包
[root@localhost src]# tar -zxvf mysql-5.7.17.tar.gz
[root@localhost src]# tar -zxvf boost_1_59_0.tar.gz
# 添加mysql用户
[root@localhost src]# useradd -r -g mysql -s /sbin/nologin mysql
# 创建mysql安装目录、数据目录以及boost目录
[root@localhost src]# mkdir /root/vslnmp/mysql.5.7
[root@localhost src]# mkdir /root/vslnmp/mysql.5.7/data
[root@localhost src]# mkdir /usr/local/boost
  • 编译安装
# 进入mysql解压目录,进行cmake
[root@localhost src]# cmake -DCMAKE_INSTALL_PREFIX=/root/vslnmp/mysql.5.7 -DMYSQL_UNIX_ADDR=/root/vslnmp/mysql.5.7/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/root/vslnmp/mysql.5.7/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DWITH_BOOST=/usr/local/boost/boost_1_59_0

见如下图表示cmake成功…
LNMP环境部署(源码编译)_第3张图片

[root@localhost src]# make && make install
这个时间比较长,耐心等待吧!

见如下图表示安装成功…
LNMP环境部署(源码编译)_第4张图片

2.3 安装后配置

# 复制mysql配置文件
[root@localhost src]# cp /root/vslnmp/mysql.5.7/support-files/my-default.cnf /etc/my.cnf
# 编辑配置文件(配置以下四项)
 18 basedir = /root/vslnmp/mysql.5.7
 19 datadir = /root/vslnmp/mysql.5.7/data
 20 port = 3306
 22 socket = /root/vslnmp/mysql.5.7/data/mysql.sock
# 复制服务启动文件
[root@localhost support-files]#  cp /root/vslnmp/mysql.5.7/support-files/mysql.server /etc/init.d/mysqld
# 在/etc/profile里面配置环境变量
export PATH=/root/vslnmp/nginx.1.16/sbin:/root/vslnmp/mysql.5.7/bin:$PATH
# 加载生效
[root@localhost support-files]# source /etc/profile
# mysql初始化(做这步前一定要检查权限,非常重要)
[root@localhost mysql.5.7]# mysqld --initialize --user=mysql
2019-09-18T09:01:24.031661Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-18T09:01:24.032638Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2019-09-18T09:01:24.032644Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2019-09-18T09:01:25.790481Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-09-18T09:01:26.087820Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-09-18T09:01:26.184995Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e52e705c-d9f2-11e9-961c-000c29146456.
2019-09-18T09:01:26.187012Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-09-18T09:01:26.190672Z 1 [Note] A temporary password is generated for root@localhost: EMdopCNw3J_a(这个密码很重要,初始化完mysql,第一次进行安全配置时,需要输入此密码)
#初始化后查看data目录下面存在数据
[root@localhost mysql.5.7]# ll
total 40
drwxr-xr-x  2 mysql mysql  4096 Sep 18 10:00 bin
-rwxr-xr-x  1 mysql mysql 17987 Nov 28  2016 COPYING
drwxr-x---  5 mysql mysql   147 Sep 18 17:01 data
drwxr-xr-x  2 mysql mysql    55 Sep 18 02:37 docs
drwxr-xr-x  3 mysql mysql  4096 Sep 18 02:37 include
drwxr-xr-x  4 mysql mysql   191 Sep 18 02:37 lib
drwxr-xr-x  4 mysql mysql    30 Sep 18 02:37 man
drwxr-xr-x 10 mysql mysql  4096 Sep 18 02:38 mysql-test
-rwxr-xr-x  1 mysql mysql  2478 Nov 28  2016 README
drwxr-xr-x 28 mysql mysql  4096 Sep 18 02:38 share
drwxr-xr-x  2 mysql mysql   112 Sep 18 02:38 support-files
[root@localhost mysql.5.7]# pwd
/usr/local/lnmp/mysql.5.7
# 启动mysql
[root@localhost bin]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/lnmp/mysql.5.7/data/localhost.localdomain.err'.
.. SUCCESS!

总结:源码编译安装mysql时,把安装目录选择放在/usr/local下面,千万不要放在root目录下,否则就会导致各种权限问题,我之前就是把安装目录建在了root目录下,出现了权限问题,排查了好久才解决。

  • 问题记录(初始化中有遇到过两个问题,记录一下)
# 问题1
[root@localhost mysql.5.7]# mysqld --initialize --user=mysql
2019-09-17T01:19:44.831266Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-17T01:19:44.831389Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2019-09-17T01:19:44.831395Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2019-09-17T01:19:44.833571Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2019-09-17T01:19:44.833620Z 0 [ERROR] Aborting
# 原因:mysql在initialize时会往data目录里面写入数据,如果此时data目录里面存在数据,就会导致initialize失败。
# 解决:删除data目录或者清空data目录中数据,  rm -fr data/*

# 问题2:
[root@localhost mysql.5.7]# mysqld --initialize --user=mysql
2019-09-17T01:08:35.237468Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-17T01:08:35.237580Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2019-09-17T01:08:35.237586Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2019-09-17T01:08:35.467799Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-09-17T01:08:35.512924Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-09-17T01:08:35.574688Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ac911be2-d8e7-11e9-8861-000c29146456.
2019-09-17T01:08:35.575790Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-09-17T01:08:35.577024Z 1 [Note] A temporary password is generated for root@localhost: LoYx.r6q6+hh
2019-09-17T01:08:35.579864Z 1 [ERROR] 1  Can't create/write to file '/root/vslnmp/mysql.5.7/data/mysql/db.MYI' (Errcode: 13 - Permission denied)
2019-09-17T01:08:35.579995Z 0 [ERROR] Aborting
2019-09-17T01:08:35.682919Z 0 [ERROR] InnoDB: Cannot open '/root/vslnmp/mysql.5.7/data/ib_buffer_pool.incomplete' for writing: Permission denied
# 原因:权限问题
# 解决:从倒数后三行可以看出,mysqld脚本无法写入文件到data目录中,\
我想到了修改data目录权限以及属主、属组(chmod -R 755 data ;chown -R mysql.mysql data),\
并且修改了mysqld程序的权限,\
但是还是于事无补。困扰了好久,\
后面想到,是不是因为在root目录下进行安装,\
会使得mysql用户权限不足,不能进行数据写入。\
于是推倒重来,在/usr/local下面创建安装目录进行编译安装,果然成功了,所以源码安装编译任何软件时,都不要在root下进行安装目录的创建。
  • mysql源码编译安装后目录文件权限
[root@localhost lnmp]# ll mysql.5.7/    ---- >  mysql安装目录
total 40
drwxr-xr-x  2 mysql mysql  4096 Sep 18 10:00 bin
-rwxr-xr-x  1 mysql mysql 17987 Nov 28  2016 COPYING
drwxr-x---  5 mysql mysql   147 Sep 18 17:01 data
drwxr-xr-x  2 mysql mysql    55 Sep 18 02:37 docs
drwxr-xr-x  3 mysql mysql  4096 Sep 18 02:37 include
drwxr-xr-x  4 mysql mysql   191 Sep 18 02:37 lib
drwxr-xr-x  4 mysql mysql    30 Sep 18 02:37 man
drwxr-xr-x 10 mysql mysql  4096 Sep 18 02:38 mysql-test
-rwxr-xr-x  1 mysql mysql  2478 Nov 28  2016 README
drwxr-xr-x 28 mysql mysql  4096 Sep 18 02:38 share
drwxr-xr-x  2 mysql mysql   112 Sep 18 02:38 support-files
[root@localhost mysql.5.7]# ll /etc/my.cnf    ---- > mysql主配置文件
-rwxr-xr-x 1 mysql mysql 1193 Sep 18 10:02 /etc/my.cnf

2.4 进行mysql安全配置

# 进入bin目录下,执行如下操作
[root@localhost bin]# ./mysql_secure_installation 
...
会有很多选择,比如是否修改登录密码,移除匿名用户,设置root远程登录等等,根据自己需求来进行选择。也可一路回车
....

2.5 验证登录mysql

[root@localhost bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.17 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# 在上步,安全配置中,如果设置了修改密码的话,会导致这部进到数据库中无法操作的情况出现,此时使用 ALTER USER USER() IDENTIFIED BY 'sunwei123456';修改密码即可。
# 附上博友修改密码大全:https://www.cnblogs.com/reid21/p/9314376.html

至此,mysql源码编译安装mysql完成

3. php


3.1 准备工作

  • php源码包点击下载
  • 安装编译所需工具
[root@localhost bin]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel curl gdbm-devel  libXpm-devel libX11-devel  gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c curl curl-devel
# 安装编译libmcrypt和re2c
下载地址: 
[libmcrypt](ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz )
[re2c](http://sourceforge.net/projects/re2c/files/0.15.3/re2c-0.15.3.tar.gz)
# 上面两个软件,下载后解压,直接编译安装即可
tar -zxvf  libmcrypt-2.5.7.tar.gz ; cd libmcrypt-2.5.7 && ./configure && make && make install
tar -zxvf  re2c-0.15.3.tar.gz ; cd re2c-0.15.3 && ./configure && make && make install

3.2 安装

  • 预编译配置php
[root@localhost ~]# tar -zxvf php-7.3.6.tar.gz
./configure \
--prefix=/usr/local/lnmp/php/7.0.0 \
--with-config-file-path=/usr/local/lnmp/php/7.0.0/etc \
--with-config-file-scan-dir=/usr/local/lnmp/php/7.0.0/etc/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-soap \
--with-openssl \
--with-openssl-dir \
--with-pcre-regex \
--with-zlib \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--with-pcre-dir \
--enable-ftp \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-mbstring \
--with-onig \
--enable-pdo \
--with-pdo-mysql \
--with-zlib-dir \
--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 \
--with-mysqli \
--without-pear
# 预编译过程中,报错
报错1. configure: error: cURL version 7.10.5 or later is required to compile php with cURL support
解决:yum -y install curl-devel
报错2:error: Please reinstall the libzip distribution
解决:源码安装libzip-1.2.0,下载地址:https://nih.at/libzip/libzip-1.2.0.tar.gz;tar解压后./configure && make && make install
报错3:configure: WARNING: unrecognized options: --with-mcrypt, --with-libmbfl
解决:在编译安装时,提示unrecognized options: –with-mcrypt, –enable-gd-native-ttf 表示php7.2不支持这两个选项,把上面两个编译选项删除就可以了。
# 重新编译,见到如下输出则表示预编译成功
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
# 安装
[root@localhost ~]# make && make install
报错如下:
In file included from /root/php-7.3.6/ext/zip/php_zip.h:31:0,
                 from /root/php-7.3.6/ext/zip/php_zip.c:36:
/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory
 #include 
                     ^
compilation terminated.
make: *** [ext/zip/php_zip.lo] Error 1
解决:cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
# 重新make && make install,见到如下输出即成功
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/lnmp/php/7.0.0/lib/php/extensions/no-debug-non-zts-20180731/
Installing PHP CLI binary:        /usr/local/lnmp/php/7.0.0/bin/
Installing PHP CLI man page:      /usr/local/lnmp/php/7.0.0/php/man/man1/
Installing PHP FPM binary:        /usr/local/lnmp/php/7.0.0/sbin/
Installing PHP FPM defconfig:     /usr/local/lnmp/php/7.0.0/etc/
Installing PHP FPM man page:      /usr/local/lnmp/php/7.0.0/php/man/man8/
Installing PHP FPM status page:   /usr/local/lnmp/php/7.0.0/php/php/fpm/
Installing phpdbg binary:         /usr/local/lnmp/php/7.0.0/bin/
Installing phpdbg man page:       /usr/local/lnmp/php/7.0.0/php/man/man1/
Installing PHP CGI binary:        /usr/local/lnmp/php/7.0.0/bin/
Installing PHP CGI man page:      /usr/local/lnmp/php/7.0.0/php/man/man1/
Installing build environment:     /usr/local/lnmp/php/7.0.0/lib/php/build/
Installing header files:          /usr/local/lnmp/php/7.0.0/include/php/
Installing helper programs:       /usr/local/lnmp/php/7.0.0/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/lnmp/php/7.0.0/php/man/man1/
  page: phpize.1
  page: php-config.1
/root/php-7.3.6/build/shtool install -c ext/phar/phar.phar /usr/local/lnmp/php/7.0.0/bin
ln -s -f phar.phar /usr/local/lnmp/php/7.0.0/bin/phar
Installing PDO headers:           /usr/local/lnmp/php/7.0.0/include/php/ext/pdo/

3.3 安装后配置

  • 创建php配置文件
# 进入php安装目录
[root@localhost ~]# cp php.ini-production /usr/local/lnmp/php/7.0.0/etc/php.ini
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost php-fpm.d]# cp www.conf.default www.conf
# 进入php解压目录
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost fpm]# chmod 755 /etc/init.d/php-fpm
  • 配置php.ini
# 进入php安装目录,编辑php.ini文件
[root@localhost fpm]# vim /usr/local/lnmp/php/7.0.0/etc/php.ini
# 设置PHP的opcache和memcache扩展库
zend_extension=opcache.so
extension=memcache.so
# 设置PHP的扩展库路径
extension_dir = "/usr/local/lnmp/php/7.0.0/lib/php/extensions/no-debug-non-zts-20180731/"
# 避免PHP信息暴露在http头中
expose_php = Off
# 避免暴露php调用mysql的错误信息
display_errors = Off
# 开启PHP错误日志(路径在php-fpm.conf中配置)
log_errors = On
# 设置PHP的时区
date.timezone = PRC
# 开启opcache(1733行左右)
opcache.enable=1
# 设置PHP脚本允许访问的目录
# open_basedir = /root/vslnmp/nginx.1.16/html
  • 配置php-fpm
# php-fpm.conf 进程服务主配置文件
[root@localhost extensions]# grep '^[^;]' /usr/local/lnmp/php/7.0.0/etc/php-fpm.conf
[global]
pid = /usr/local/lnmp/php/7.0.0/var/run/php-fpm.pid
# 设置错误日志的路径
error_log = /usr/local/lnmp/php/7.0.0/var/log/php-fpm/error.log
# 设置主进程打开的最大文件数
rlimit_files = 102400
# 引入www.conf文件中的配置
include=/usr/local/lnmp/php/7.0.0/etc/php-fpm.d/*.conf
-------------------
# www.conf 进程服务扩展配置文件
[root@localhost php-fpm.d]# grep '^[^;]' www.conf
[www]
# 设置php监听方式
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
#关用户和用户组
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
# 设置工作进程数(根据实际情况设置)
pm.max_children = 50
# 这里需要注意,pm.start_servers 不能小于 pm.min_spare_servers
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
# 开启慢日志
slowlog = log/$pool.log.slow
request_slowlog_timeout = 10s
request_terminate_timeout = 30
  • php-fpm 开启
# php-fpm启动,位于/usr/local/lnmp/php/7.0.0/sbin目录下
[root@localhost sbin]# ./php-fpm 
# 观察环回地址上9000端口已经开始监听
[root@localhost sbin]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      22284/php-fpm: mast 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12207/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6756/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      16263/mysqld        
tcp6       0      0 :::22                   :::*                    LISTEN      6756/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           7112/dhclient  
# php-fpm 关闭
[root@localhost sbin]# kill -INT `cat /usr/local/lnmp/php/7.0.0/var/run/php-fpm.pid`
# php-fpm重启
[root@localhost sbin]# /etc/init.d/php-fpm restart
  • 整合nginx与php
# 修改nginx.conf配置文件,如下所示
location ~ \.php$ {
            root           html;
            #fastcgi_pass   172.20.117.15:9000;
            fastcgi_pass   127.0.0.1:9000;
            #fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
#修改配置文件后,重启nginx,nginx -s reload也可以
/etc/init.d/nginx restart

3.4 验证php

  • 单独验证php
# 写入测试文件
[root@localhost html]# cat index.php 

[root@localhost html]# pwd
/root/vslnmp/nginx.1.16/html
# 访问测试:http://172.20.117.15/index.php 如下图所示

LNMP环境部署(源码编译)_第5张图片

  • php连接数据库验证
# php连接mysql数据库,需要在php.ini文件中配置mysql数据库socket文件路径
mysqli.default_socket = /usr/local/lnmp/mysql.5.7/mysql.sock
# 编写php连接数据库小程序,如下:
connect_error) {
die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
?>

如下图,则显示php连接mysql成功。
LNMP环境部署(源码编译)_第6张图片
至此,php源码编译安装完成。

  • 问题以及解决:
    1 服务进程以及端口都正常,配置文件也正常,php页面一直访问不到,更改一下root目录的权限,立马ok,是因为我的nginx是在root目录下编译安装的,坑。以后千万不要在root目录下面进行服务的编译安装。
    2 Php-fpm启动后没有查询到9000端口的问题,我是按照这边博文进行配置的,https://www.ddkiss.com/archives/150.html

  • 遗留问题:php-fpm方式启动起来了,页面也能够正常访问,但是php-cgi方式不行,php-cgi启动命令如下,spawn-fcgi 需要下载安装:
    [root@localhost ~]# spawn-fcgi -a 172.20.117.15 -p 9000 -C 5 -f /usr/local/lnmp/php/7.0.0/bin/php-cgi

  • 知识点:
    nginx与php-fpm通信的两种方式: tcp socket 和 unix socket。
    unix socket 是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。这种方式需要再nginx配置文件中填写php-fpm的pid文件位置,效率要比tcp socket高。
    tcp socket的优点是可以跨服务器,当nginx和php-fpm不在同一台机器上时,只能使用这种方式。

你可能感兴趣的:(lnmp)