LNMP架构——php+nginx+mysql源码编译搭建lnmp环境

矫情吗;* 2023-07-05 14:47 197阅读 0赞

本次实验将搭建lnmp架构,详细讲解PHP、Nginx、Mysql的源码编译过程。

文章目录

    • 一、实验环境
    • 二、搭建lnmp架构
        • 1、安装数据库mysql-boost
        • 2、源码编译安装php
        • 3、源码编译nginx

一、实验环境





















主机名 ip 服务
server1 172.25.1.1 lnmp架构
真机 172.25.1.250 测试(客户端)

mysql源码编译时要求虚拟机的硬盘大小不能小于20G,否则后边的编译过程可能会出问题
而且会很漫长,为了提升速度我们增加CPU核数和内存大小。
在这里插入图片描述

二、搭建lnmp架构

封装rhel6.5版本虚拟机母盘详细过程

1、安装数据库mysql-boost

在server1上:

步骤一:在网上下载mysql-boost安装包及cmake安装包
在这里插入图片描述
cmake相当于configure,用来编译

步骤二:安装cmake

  1. yum install cmake-2.8.12.2-4.el6.x86_64.rpm -y

在这里插入图片描述
步骤三:解压mysql-boost,进入解压目录并编译

  1. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \ #安装目录
  2. -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \ #数据库存放目录
  3. -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \ #Unix socket 文件路径
  4. -DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎
  5. -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎
  6. -DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
  7. -DDEFAULT_COLLATION=utf8_general_ci \ #校验字符
  8. -DEXTRA_CHARSETS=all \ #安装所有扩展字符集
  9. -DWITH_BOOST=boost/boost_1_59_0/

在这里插入图片描述

发现报错,提示需要解决依赖项,解决:

  1. yum install -y gcc gcc-c++ ncurses-devel

在这里插入图片描述
清理缓存,重新编译:

  1. make clean
  2. rm -fr CMakeCache.txt
  3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
发现如下报错:

CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
libmysqld/CMakeLists.txt:187 (INCLUDE)

解决:

  1. yum install -y bison

在这里插入图片描述
清理缓存,重新编译:

  1. make clean
  2. rm -fr CMakeCache.txt
  3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0

在这里插入图片描述
步骤四:编译安装

  1. make && make install

注意:时间会很长,不要乱按
在这里插入图片描述在这里插入图片描述
步骤五:复制启动脚本

  1. cp /root/mysql-5.7.17/support-files/mysql.server /etc/init.d/mysqld
  2. chmod +x /etc/init.d/mysqld

在这里插入图片描述
步骤六:拷贝配置文件到数据库文件/etc/my.cnf

  1. cp /etc/my.cnf /etc/my.cnf.bak
  2. cp my-default.cnf /etc/my.cnf

在这里插入图片描述
步骤七:修改/etc/my.cnf文件

  1. vim /etc/my.cnf
  2. basedir = /usr/local/lnmp/mysql
  3. datadir = /usr/local/lnmp/mysql/data
  4. socket = /usr/local/lnmp/mysql/data/mysql.sock

在这里插入图片描述
步骤七:添加用户和组,指定id为27,因为rpm默认安装时mysql用户的id为27,并非强制

  1. groupadd -g 27 mysql
  2. useradd -u 27 -g 27 mysql
  3. chown mysql.mysql /usr/local/lnmp/mysql -R

在这里插入图片描述
步骤八:将mysql命令添加到环境变量

  1. vim ~/.bash_profile
  2. PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
  3. source ~/.bash_profile

在这里插入图片描述在这里插入图片描述
测试:
输入mysql按tab键补齐
在这里插入图片描述
步骤九:初始化mysql,会生成一个临时密码,用于登录mysql(要记住此密码)

  1. mysqld --user=mysql --initialize

在这里插入图片描述
步骤十:为了安全,再将mysql目录的所有者改为root,但是 数据目录所有者必须是mysql,不然mysql用户不能写

  1. chown root /usr/local/lnmp/mysql -R
  2. chown mysql /usr/local/lnmp/mysql/data -R

在这里插入图片描述
步骤十一:启动mysqld,并安全初始化

  1. /etc/init.d/mysqld start
  2. mysql_secure_installation

安全初始化时会提示是否启用密码检测插件,直接回车不启用,否则会要求密码有大小写和特殊字符等要求,剩余全部选 y

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
步骤十二:登陆数据库测试

  1. mysql -uroot -predhat
  2. mysql> show databases;

在这里插入图片描述
可以看到数据目录下生成了套节字文件:

  1. cd /usr/local/lnmp/mysql/data/
  2. ls

在这里插入图片描述
查看服务开启端口:

  1. netstat -antuple

在这里插入图片描述

2、源码编译安装php

在server1上:

步骤一:下载php安装包并解压

  1. tar -jxvf php-5.6.35.tar.bz2

在这里插入图片描述
在这里插入图片描述
步骤二:进入解压目录,编译

  1. cd php-5.6.35
  2. ./configure --prefix=/usr/local/lnmp/php \
  3. --with-config-file-path=/usr/local/lnmp/php/etc \ #指定php.ini位置
  4. --with-openssl --with-snmp --with-gd --with-zlib --with-curl \
  5. --with-libxml-dir --with-png-dir \
  6. --with-jpeg-dir --with-freetype-dir --with-gmp \ # --with-freetype-dir 打开对freetype字体库的支持
  7. --with-gettext --with-pear --enable-mysqlnd \
  8. --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
  9. # --with-MySQL=mysql安装目录,对mysql的支持 ;--with-MySQLi=mysqli文件目录,优化支持
  10. --enable-inline-optimization --enable-soap --enable-ftp \
  11. --enable-sockets --enable-mbstring --enable-fpm \ #--enable-fpm参数即可开启PHP-FPM
  12. --with-fpm-user=nginx --with-fpm-group=nginx \
  13. # 编译的时候指定了php的用户是nginx用户,故要建立nginx用户
  14. --with-mcrypt --with-mhash

在这里插入图片描述在这里插入图片描述
出现如下报错:
configure: error: xml2-config not found. Please check your libxml2 installation.

解决:

  1. yum install -y libxml2-devel

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:

configure: error: Cannot find OpenSSL’s

解决:

  1. yum install -y openssl-devel

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: Please reinstall the libcurl distribution -
easy.h should be in /include/curl/

解决:

  1. yum install -y libcurl-devel

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: jpeglib.h not found.

解决:

  1. yum install -y libjpeg-turbo-devel-1.2.1-1.el6.x86_64

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: png.h not found.

解决:

  1. yum install -y libpng-devel-1.2.49-1.el6_2.x86_64

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: freetype-config not found.

解决:

  1. yum install freetype-devel-2.3.11-14.el6_3.1.x86_64 -y

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: Unable to locate gmp.h

解决:

  1. yum install gmp-devel-4.3.1-7.el6_2.2.x86_64 -y

在这里插入图片描述
在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决:
在网上下载libmcrypt及libmcrypt-devel安装包并安装

  1. yum install libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm -y

在这里插入图片描述
在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
出现如下报错:
configure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.

解决:

  1. yum whatprovides */net-snmp-config
  2. yum install -y net-snmp-devel-5.5-49.el6.x86_64

在这里插入图片描述
在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

在这里插入图片描述
步骤三:编译安装

  1. make && make install

在这里插入图片描述在这里插入图片描述
步骤四:编辑配置文件php-fpm.conf

  1. cd /usr/local/lnmp/php/etc
  2. cp php-fpm.conf.default php-fpm.conf
  3. vim php-fpm.conf
  4. 25 pid = run/php-fpm.pid
  5. 149 user = nginx
  6. 150 group = nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
步骤五:拷贝更改php主配置文件/usr/local/lnmp/php/etc/php.ini,修改时区

  1. cd /root/php-5.6.35
  2. cp php.ini-production /usr/local/lnmp/php/etc/php.ini
  3. vim /usr/local/lnmp/php/etc/php.ini
  4. 936 date.timezone = Asia/Shanghai

在这里插入图片描述
步骤六:拷贝php启动脚本至/etc/init.d/,并启动php-fpm,查看服务开启端口

  1. cd /root/php-5.6.35/sapi/fpm
  2. cp init.d.php-fpm /etc/init.d/php-fpm
  3. chmod +x /etc/init.d/php-fpm
  4. useradd nginx
  5. id nginx
  6. /etc/init.d/php-fpm start
  7. netstat -antuple | grep 9000

在这里插入图片描述在这里插入图片描述

3、源码编译nginx

在server1上:

步骤一:下载nginx安装包,并解压

步骤二:修改配置文件,注释掉版本号

  1. vim src/core/nginx.h
  2. 14 #define NGINX_VER "nginx/"

在这里插入图片描述
步骤三:修改gcc文件,关闭debug模式

  1. vim auto/cc/gcc
  2. 171 # debug
  3. 172 #CFLAGS="$CFLAGS -g"

在这里插入图片描述
步骤四:编译

  1. ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx

在这里插入图片描述
出现如下报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using —without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using —with-pcre= option.

解决:

  1. yum install pcre-devel -y

在这里插入图片描述
再次编译:

  1. ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx

在这里插入图片描述
步骤五:编译安装

  1. make && make install

在这里插入图片描述
步骤六: 修改配置文件,开启php模块,添加php页面

  1. vim /usr/local/lnmp/nginx/conf/nginx.conf
  2. location / {
  3. root html;
  4. index index.php index.html index.htm;
  5. }
  6. location ~ \.php$ {
  7. root html;
  8. fastcgi_pass 127.0.0.1:9000;
  9. fastcgi_index index.php;
  10. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  11. include fastcgi.conf;
  12. }

在这里插入图片描述在这里插入图片描述
步骤七:创建软链接,将nginx启动脚本链接到/usr/local/sbin/下,并检测语法规范启动nginx

  1. ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin
  2. nginx -t
  3. nginx

在这里插入图片描述

步骤八:此时,可以看到php、nginx、mysql服务全部开启

  1. netstat -antuple

在这里插入图片描述
在客户端(真机)上测试:

浏览器搜索http://172.25.1.101
在这里插入图片描述
在server1上:
在nginx默认发布目录添加 index.php

  1. vim /usr/local/lnmp/nginx/html/index.php
  2. <?php
  3. phpinfo()
  4. ?>

在这里插入图片描述
在客户端(真机)上测试:

浏览器搜索http://172.25.1.101/index.php
在这里插入图片描述
至此,lnmp搭建完成

发表评论

表情:
评论列表 (有 0 条评论,197人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LNMP环境

    经过一番折腾,终于将LNMP环境搭建完成了。本文介绍的LNMP环境是在windows的Oracle VM VirtualBox中的Centos虚拟机上搭建的,各个软件的版本为: