LNMP架构的搭建

LNMP 架构的搭建

基础架构图

LNMP架构的搭建

环境:

server5: nginx  mysql   php

//需要的安装包 (蓝色为解压后的文件)

LNMP架构的搭建

[[email protected] ~]# /etc/init.d/iptables stop        //关掉防火墙


MYSQL 源码安装

[[email protected] ~]#yum install -y gcc gcc-c++ make ncurses-devel bison
openssl-devel zlib-devel cmake           //解决依赖性
[[email protected] ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm mysql-boost-5.7.11.tar.gz
[[email protected] ~]# tar zxf mysql-boost-5.7.11.tar.gz
[[email protected] ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm mysql-5.7.11 mysql-boost-5.7.11.tar.gz
[[email protected] ~]# yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm
[[email protected] ~]# cd mysql-5.7.11/
[[email protected]]#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 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \    #安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \    #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \    #安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \    #安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \    #安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \    #安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \    #安装数据库分区
-DENABLED_LOCAL_INFILE=1 \    #允许从本地导入数据
-DWITH_READLINE=1 \    #快捷键功能
-DWITH_SSL=yes \    #支持 SSL
-DDEFAULT_CHARSET=utf8 \    #使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \    #校验字符
-DEXTRA_CHARSETS=all \    #安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \    #MySQL 监听端口


编译时可能会出现很多依赖性:
See also "/root/mysql-5.7.11/CMakeFiles/CMakeOutput.log".


[[email protected] ~]# yum install -y gcc gcc-c++
[[email protected] ~]# rm -rf CMakeCache.txt
[[email protected] ~]# yum install -y ncurses-devel
[[email protected] ~]# yum install -y bison       

//每修改一次错误,就执行清缓存操作 rm -fr CMakeCache.txt 并再次编译
//编译三部曲 cmake / ./configure...../make/make install
[[email protected] ~]# make && make install
[[email protected] mysql-5.7.11]# cd /usr/local/lnmp/
[[email protected] lnmp]# ls
mysql
[[email protected] lnmp]#cd mysql

LNMP架构的搭建
LNMP架构的搭建

[[email protected] mysql]# cd support-files/
[[email protected] support-files]# pwd
/usr/local/lnmp/mysql/support-files
[[email protected] support-files]# cp my-default.cnf /etc/my.cnfcp: overwrite `/etc/my.cnf'? Y
[[email protected] support-files]# vim /etc/my.cnf

LNMP架构的搭建

[[email protected] support-files]# file mysql.server
mysql.server: POSIX shell script text executable
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld
cp: overwrite `/etc/init.d/mysqld'? yes
[[email protected] support-files]# cd ..
[[email protected] mysql]# groupadd -g 27 mysql      //创建组
[[email protected] mysql]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s    /sbin/nologin mysql     //创建用户
[[email protected] mysql]# cd
[[email protected] ~]# vim .bash_profile          //将路径注明添加到环境变量

LNMP架构的搭建

[[email protected] ~]# source .bash_profile           //刷新环境变量
[[email protected] ~]# cd -
/usr/local/lnmp/mysql
[[email protected] mysql]# ls
bin
data include lib mysql-test share
COPYING docs keyring man README
support-files
[[email protected] mysql]# vim /etc/my.cnf

LNMP架构的搭建

[[email protected] mysql]# mysqld --initialize --user=mysql             //进行初始化

2018-10-10T05:37:45.328095Z 0 [ERROR] --initialize specified but the data
directory has files in it. Aborting.
2018-10-10T05:37:45.328132Z 0 [ERROR] Aborting
//出现这种问题,应将目录切换到 mysql 下的 data 目录,删除 data 下的所有东西并重新初始化。若没出现这种情况则步骤忽略。

[[email protected] mysql]# cd data/
[[email protected] data]# ls
auto.cnf  ibdata1   ib_logfile1   marry   mysql.sock   performance_schema test5.err 
ib_buffer_pool   ib_logfile0   ibtmp1   mysql   mysql.sock.lock   sys  test5.pid

[[email protected] data]# rm -rf *

LNMP架构的搭建

[[email protected] mysql]# cd ..
[[email protected] mysql]# mysqld --initialize --user=mysql         //再次初始化

2018-10-10T05:41:49.135259Z 0 [Warning] Gtid table is not ready to be used.
Table 'mysql.gtid_executed' cannot be opened.
2018-10-10T05:41:49.135787Z 1 [Note] A temporary password is generated for
[email protected]: e<hjlNprw7aj           //localhost: 后面的为初始密码用于后面的密码初始化

[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[[email protected] mysql]# chgrp root . -R            //更改组及用户
[[email protected] mysql]# ll

LNMP架构的搭建

[[email protected] mysql]# chown mysql data/ -R          //初始化及登陆
[[email protected] mysql]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

如果出现以下情况:

[[email protected] mysql]# /etc/init.d/mysqld start

Starting MySQL....
ERROR! The server quit without updating PID file (/usr/local/lnmp/mysql/data/test5.pid).
执行:

[[email protected] mysql]# chown -R mysql:mysql /var/data
[[email protected] mysql]# chown -R mysql:mysql /usr/local/lnmp/mysql/data
[[email protected] mysql]# chmod -R 755 /usr/local/lnmp/mysql/data
[[email protected] mysql]# /etc/init.d/mysqld restart          //重起服务,失败

ERROR! MySQL server PID file could not be found!
Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/lnmp/mysql/data/test5.pid).
//解决方法:用命令“ps -ef|grep mysqld”查看是否有 mysqld 进程,如果有使用“kill -9 进程号”杀掉,然后启动 mysql
[[email protected] mysql]# ps -ef|grep mysqld      //查看进程,杀掉 mysql 有关的进程

LNMP架构的搭建

[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql]# mysql_secure_installation    //开启服务,成功

Securing the MySQL server deployment.
Enter password for user root:              //这里输入初始密码
The existing password for the user account root has expired. Please set a new
password.
New password:
Re-enter new password:          //输入你想设置的数据库密码
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.Remove test database and access to it? (Press y|Y for Yes, any other key for
No) :
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
... skipping.
All done!

[[email protected] mysql]# mysql -p            //登陆 mysql
[[email protected] mysql]# mysql -p

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.

LNMP架构的搭建

mysql> exit
Bye

 
PHP 源码安装

[[email protected] ~]# ls

cmake-2.8.12.2-4.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
php-5.6.35.tar.bz2
gd-devel-2.0.35-11.el6.x86_64.rpm
mysql-5.7.11
re2c-0.13.5-1.el6.x86_64.rpm
libmcrypt-2.5.8-9.el6.x86_64.rpm
mysql-boost-5.7.11.tar.gz

[[email protected] ~]# tar jxf php-5.6.35.tar.bz2
[[email protected] ~]# ls    //下载依赖包
[[email protected] ~]# yum install -y libxml2-devel openssl-devel curl-devel  gd-devel-2.0.35-11.el6.x86_64.rpm  gmp-devellibmcrypt-2.5.8-9.el6.x86_64.rpm  libmcrypt-devel-2.5.8-9.el6.x86_64.rpm net-snmp-devel
[[email protected] ~]# rpm -ivh re2c-0.13.5-1.el6.x86_64.rpm

 

[[email protected] ~]# cd php-5.6.35
[[email protected]~]./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-mysql=mysqlnd --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --with-gmp --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:
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-vpx-dir=<DIR>
configure: error: jpeglib.h not found.
LNMP架构的搭建

解决:
yum -y install libjpeg-devel
再次编译
问题 2:
checking for jpeg_read_header in -ljpeg... yes
configure: error: png.h not found.解决:
yum install -y libpng-devel libpng
再次编译
问题 3:
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.

LNMP架构的搭建

解决:
[[email protected] php-5.6.35]# yum install -y freetype-devel


再次编译

LNMP架构的搭建
最后编译成功
//可能遇到的其他问题:
问题 4:
No package xml2-config available.
Error: Nothing to do
解决:
[[email protected] php-5.6.35]# rpm -qa | grep libxml2
libxml2-python-2.7.6-14.el6.x86_64
libxml2-2.7.6-14.el6.x86_64
[[email protected] php-5.6.35]# yum install libxml2 libxml2-devel -y

问题 5:
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
解决:
[[email protected] php-5.6.35]# yum install curl curl-devel -y
问题 6:
configure: error: Could not find net-snmp-config binary. Please check your
net-snmp installation.
解决:
[[email protected] php-5.6.35]# yum -y install net-snmp-devel

[[email protected] php-5.6.35]# make && make install            //编译第二步和第三步

配置部分:

[[email protected] ~]# cd /usr/local/lnmp/php/
[[email protected] php]# ls
bin etc include lib php sbin var
[[email protected] php]# cd etc/
[[email protected] etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
cp: overwrite `php-fpm.conf'? y
[[email protected] etc]# cd /root/php-5.6.35
[[email protected] etc]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[[email protected] etc]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[[email protected] etc]# vim php.ini                        //写时间

LNMP架构的搭建

[[email protected] etc]# vim php-fpm.conf         

   
LNMP架构的搭建                    //使第 25 行有效

[[email protected] etc]# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin nginx     //创建用户
[[email protected] php-5.6.35]# cd /root/php-5.6.35/sapi/fpm/
[[email protected] fpm]# file init.d.php-fpm                 //它是个脚本
init.d.php-fpm: POSIX shell script text executable
//[[email protected] fpm]# cp init.d.php-fpm /etc/init.d/php-fpm                    //将脚本文件传到默认脚本路径
[[email protected] fpm]# chmod +x /etc/init.d/php-fpm                   //给其可执行权限
[[email protected] fpm]# /etc/init.d/php-fpm start                     //执行文件
Starting php-fpm done


nginx 源码安装

需要的包
nginx-1.14.0.tar.gz
nginx-sticky-module-1.1.tar.gz

LNMP架构的搭建

[[email protected] ~]# tar zxf nginx-sticky-module-1.1.tar.gz
[[email protected] ~]# tar zxf nginx-1.14.0.tar.gz
[[email protected] ~]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE
man README src 

[[email protected] nginx-1.14.0]# vim src/core/nginx.h                       //去掉 nginx 的版本号

LNMP架构的搭建

[[email protected] nginx-1.14.0]# vim auto/cc/gcc                   //注释掉   

LNMP架构的搭建

[[email protected] nginx-1.14.0]# yum install pcre-devel -y
[[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module
--user=nginx --group=nginx --with-threads --with-file-aio                                  //同样,根据报错解决依赖性//编译成功


LNMP架构的搭建

//编译成功

[[email protected] nginx-1.14.0]# make && make install
[[email protected] ~]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# cd /usr/local/lnmp/nginx/conf/
[[email protected] conf]# vim nginx.conf                         //加入 index.php 会默认首先访问 index.php

LNMP架构的搭建

[[email protected] conf]# cd
[[email protected] ~]# vim .bash_profile

LNMP架构的搭建

[[email protected] ~]# source .bash_profile //或者 ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/          //做个软连接
[[email protected] ~]# cd /usr/local/lnmp/nginx/html/
[[email protected] html]# vim index.php

LNMP架构的搭建

[[email protected] html]# nginx -t             //检查 nginx 配置文件是否有错误
[[email protected] html]# nginx                //开启 nginx 服务


在网页进行访问 172.25.1.5:LNMP架构的搭建

nginx 和 mysql 结合并安装论坛

[[email protected] ~]# unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/lnmp/nginx/html
[[email protected] ~]# cd /usr/local/lnmp/nginx/html/
[[email protected] html]# ls
50x.html bbs index.html index.php readme utility
[[email protected] html]# mv upload bbs

LNMP架构的搭建

网页进行访问 172.25.1.5/bbs:

LNMP架构的搭建

LNMP架构的搭建

LNMP架构的搭建

[[email protected] bbs]# chmod 777 config/ data/ uc_server/ uc_client/ -R           //修改权限

LNMP架构的搭建

LNMP架构的搭建

执行下一步

LNMP架构的搭建

LNMP架构的搭建

数据库连接错误,执行:

[[email protected] bbs]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# vim php.ini
pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock
mysql.default_socket =/usr/local/lnmp/mysql/data/mysql.sock
mysqli.default_socket =/usr/local/lnmp/mysql/data/mysql.sock

//分别是

LNMP架构的搭建

LNMP架构的搭建

LNMP架构的搭建

[[email protected] etc]# /etc/init.d/php-fpm reload
[[email protected] etc]# cd /usr/local/lnmp/mysql/
[[email protected] mysql]# chmod 755 data/   

LNMP架构的搭建                    

//下一步

LNMP架构的搭建

LNMP架构的搭建

登陆论坛:

LNMP架构的搭建
登陆成功

LNMP架构的搭建

LNMP架构的搭建LNMP架构的搭建

上面提示删除 index.php
故执行删除操作:

[[email protected] mysql]# cd data
[[email protected] data]# cd /usr/local/lnmp/nginx/html/bbs/install/
[[email protected] install]# ls
data images include index.php
[[email protected] install]# rm -f index.php

LNMP架构的搭建最后,论坛发布成。