分布式LNMP【centos7+nginx1.24+php8+mysql8】
分布式源码部署LNMP架构
192.168.100.62 Mysql
192.168.100.101 Nginx
192.168.100.102 PHP
分别下载mysql nginx php 源码包
mysql8:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35.tar.gz
php8:https://www.php.net/distributions/php-8.2.12.tar.gz
nginx:http://nginx.org/download/nginx-1.24.0.tar.gz
部署MYSQL8
1,MYSQL8数据库源码编译安装,需cmake版本大于3.5.+,cmake3可以使用yum或者二进制包部署
2,升级GCC高版本
yum部署cmake3
#查看目前cmake版本
[root@localhost ~]# cmake --version
-bash: cmake: 未找到命令
#安装epel-release扩展源
[root@localhost ~]# yum install epel-release -y
#安装cmake3
[root@localhost ~]# yum install cmake3 -y
#将新版本cmake3软链接到cmake;
[root@localhost ~]# ln -s /usr/bin/cmake3 /usr/bin/cmake
源码部署cmake3
#cmake3源码下载
[root@localhost ~]# wget -O /usr/src/cmake-3.7.2.tar.gz https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
cmake-3.7.2.tar.gz debug kernels
#解压
[root@localhost src]# tar -xf cmake-3.7.2.tar.gz
#编译安装
[root@localhost src]# cd cmake-3.7.2
[root@localhost cmake-3.7.2]# ls
Auxiliary cmake_uninstall.cmake.in DartConfig.cmake Source
bootstrap CompileFlags.cmake doxygen.config Templates
CMakeCPack.cmake configure Help Tests
CMakeCPackOptions.cmake.in CONTRIBUTING.rst Licenses Utilities
CMakeGraphVizOptions.cmake Copyright.txt Modules
CMakeLists.txt CTestConfig.cmake Packaging
CMakeLogo.gif CTestCustom.cmake.in README.rst
[root@localhost cmake-3.7.2]# ./bootstrap && make && make install
[root@localhost cmake-3.7.2]# rm -rf /usr/bin/cmake
[root@localhost cmake-3.7.2]# ln -s /usr/local/bin/cmake /usr/bin/
[root@localhost cmake-3.7.2]# cmake --version
cmake version 3.7.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
升级GCC高版本
#升级
[root@localhost cmake-3.7.2]# yum -y install centos-release-scl
#安装依赖包和库文件
[root@localhost cmake-3.7.2]# yum -y install ncurses-devel make perl gcc autoconf automake zlib libxml2 libxml2-devel libgcrypt libtool bison devtoolset-10-gcc devtoolset-10-gcc-c++ devtoolset-10-binutils openssl openssl-devel
#启用新版Gcc
[root@localhost cmake-3.7.2]# scl enable devtoolset-10 bash
[root@localhost cmake-3.7.2]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-10/root/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-10/root/usr --mandir=/opt/rh/devtoolset-10/root/usr/share/man --infodir=/opt/rh/devtoolset-10/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-10.2.1-20210130/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.1 20210130 (Red Hat 10.2.1-11) (GCC)
开始部署mysql
#下载mysql
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35.tar.gz
#解压mysql
[root@localhost src]# tar -xf mysql-8.0.35.tar.gz
[root@localhost src]# cd mysql-8.0.35
[root@localhost mysql-8.0.35]# ls
client Doxyfile.in libmysql plugin storage
cmake doxygen_resources libservices README strings
CMakeLists.txt extra LICENSE router support-files
components include man run_doxygen.cmake testclients
config.h.cmake INSTALL mysql-test scripts unittest
configure.cmake libbinlogevents MYSQL_VERSION share utilities
Docs libbinlogstandalone mysys sql vio
Doxyfile-ignored libchangestreams packaging sql-common
#预编译(这里出现一个问题)
[root@localhost mysql-8.0.35]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
> -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc \
> -DMYSQL_USER=mysql \
> -DMYSQL_TCP_PORT=3306 \
> -DWITH_XTRADB_STORAGE_ENGINE=1 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DWITH_READLINE=1 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_EXTRA_CHARSETS=1 \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DEXTRA_CHARSETS=all \
> -DWITH_BIG_TABLES=1 \
> -DWITH_DEBUG=0 \
> -DWITH_BOOST=./boost/ \
> -DFORCE_INSOURCE_BUILD=1
-- Running cmake version 3.7.2
-- This is .el7. as found from 'rpm -qf /'
-- Looking for a devtoolset compiler
CMake Warning at CMakeLists.txt:394 (MESSAGE):
Could not find devtoolset compiler/linker in /opt/rh/devtoolset-11
CMake Warning at CMakeLists.txt:396 (MESSAGE):
You need to install the required packages:
yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils
CMake Error at CMakeLists.txt:398 (MESSAGE):
Or you can set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER explicitly.
-- Configuring incomplete, errors occurred!
#根据提示安装
[root@localhost mysql-8.0.35]# yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils -y
#在这之前提前下载boost
[root@localhost mysql-8.0.35]# wget -O /usr/local/boost/boost_1_77_0.tar.bz2 https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2
#重新预编译
[root@localhost mysql-8.0.35]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
> -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc \
> -DMYSQL_USER=mysql \
> -DMYSQL_TCP_PORT=3306 \
> -DWITH_XTRADB_STORAGE_ENGINE=1 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DWITH_READLINE=1 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_EXTRA_CHARSETS=1 \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DEXTRA_CHARSETS=all \
> -DWITH_BIG_TABLES=1 \
> -DWITH_DEBUG=0 \
> -DDOWNLOAD_BOOST=1 \
> -DWITH_BOOST=/usr/local/boost \
> -DFORCE_INSOURCE_BUILD=1
#编译
[root@localhost mysql-8.0.35]# make -j8
#安装
[root@localhost mysql-8.0.35]# make -j8 install
#创建数据目录
[root@localhost mysql-8.0.35]# mkdir -p /data/mysql
#创建数据库用户
[root@localhost mysql-8.0.35]# useradd -s /sbin/nologin mysql -M
#初始化数据库
[root@localhost mysql-8.0.35]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
2023-11-01T08:08:30.898167Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-11-01T08:08:30.898241Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.35) initializing of server in progress as process 65352
2023-11-01T08:08:30.899148Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2023-11-01T08:08:30.899154Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
2023-11-01T08:08:30.905460Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-11-01T08:08:31.097087Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-11-01T08:08:31.812079Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
#设置MYSQL为系统服务
[root@localhost mysql-8.0.35]#ln -s /usr/local/mysql/bin/* /usr/bin/
[root@localhost mysql-8.0.35]# \cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-8.0.35]# chmod +x /etc/init.d/mysqld
#创建my.cnf文件
[root@localhost mysql-8.0.35]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql/
datadir=/data/mysql/
port=3306
pid-file=/data/mysql/mysql.pid
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/data/mysql/mysql.log
#启动mysql
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#查看进程
[root@localhost mysql]# netstat -nltp |grep 3306
tcp6 0 0 :::33060 :::* LISTEN 65958/mysqld
tcp6 0 0 :::3306 :::* LISTEN 65958/mysqld
[root@localhost mysql]# ps -ef |grep mysql
root 65791 1 0 16:18 pts/0 00:00:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql/mysql.pid
mysql 65958 65791 1 16:18 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/data/mysql --plugin-dir=/usr/localmysql//lib/plugin --user=mysql --log-error=/data/mysql/mysql.log --pid-file=/data/mysql/mysql.pid --socket=/data/mysql/mysql.sock --port=3306
root 66009 27676 0 16:19 pts/0 00:00:00 grep --color=auto mysql
#登陆mysql
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 Source distribution
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
#修改密码123456
mysql> alter user user() identified by "123456";
Query OK, 0 rows affected (0.01 sec)
#创建wordpress数据库
mysql> create database wordpress charset utf8;
Query OK, 1 row affected, 1 warning (0.00 sec)
#创建用户webwz
mysql> create user webwz@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
#授权webwz权限
mysql> grant all on wordpress.* to webwz@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
到这数据库基本搞定了。
部署NGINX
#源码下载
[root@localhost ~]# wget -O /usr/src/nginx-1.24.0.tar.gz https://nginx.org/download/nginx-1.24.0.tar.gz
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels nginx-1.24.0.tar.gz
#解压
[root@localhost src]# tar -xf nginx-1.24.0.tar.gz
[root@localhost src]# cd nginx-1.24.0
#安装依赖库
[root@localhost nginx-1.24.0]# yum install pcre pcre-devel zlib zlib-devel -y
#预编译
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
#编译&&安装
[root@localhost nginx-1.24.0]# make && make install
#创建nginx用户
[root@localhost nginx-1.24.0]# useradd -s /sbin/nologin nginx
#修改配置文件内user 为nginx
[root@localhost nginx-1.24.0]# vim /usr/local/nginx/conf/nginx.conf
#链接nginx
[root@localhost nginx-1.24.0]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
#启动
[root@localhost nginx-1.24.0]# nginx
#查看进程是否起来
[root@localhost nginx-1.24.0]# ps -ef |grep nginx
root 14114 1 0 02:02 ? 00:00:00 nginx: master process nginx
nginx 14115 14114 0 02:02 ? 00:00:00 nginx: worker process
root 14117 1311 0 02:02 pts/0 00:00:00 grep --color=auto nginx
#nginx配置文件修改,整合php
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass 192.168.100.102:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
---------------------------------------------------------------------------------
如需做php负载均衡请参考以下
http模块中添加
upstream php-wordpress {
server 192.168.100.102:9000;
server 192.168.100.104:9000;
}
server模块中添加
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass php-wordpress;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
##最后把wordpress源文件复制一份给 php服务器
[root@localhost html]# scp -r * 192.168.100.102:/usr/local/nginx/html
#一定要注意权限不然安装没办法进行哦
[root@localhost html]# chown -R nginx. /usr/local/nginx/html
部署PHP
#下载源码
[root@localhost src]# wget https://www.php.net/distributions/php-8.2.12.tar.gz --no-check-certificate
[root@localhost src]# ls
debug kernels php-8.2.12.tar.gz
[root@localhost src]# tar -xf php-8.2.12.tar.gz
[root@localhost src]# cd php-8.2.12
[root@localhost php-8.2.12]# ls
appveyor configure EXTENSIONS php.ini-development sapi UPGRADING
build configure.ac LICENSE php.ini-production scripts UPGRADING.INTERNALS
buildconf CONTRIBUTING.md main README.md tests win32
buildconf.bat docs NEWS README.REDIST.BINS travis Zend
CODING_STANDARDS.md ext pear run-tests.php TSRM
#安装依赖
[root@localhost php-8.2.12]# yum -y install gd curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel openssl openssl-devel sqlite-devel -y
#预编译
[root@localhost php-8.2.12]# ./configure --prefix=/usr/local/php \
> --enable-fpm \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-debug \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-freetype-dir \
> --enable-mbstring \
> --with-curl \
> --with-mysqli=mysqlnd \
> --with-mysql-sock=/data/mysql/mysql.sock \
> --with-pdo-mysql=mysqlnd \
> --with-config-file-path=/usr/local/php/etc \
> --with-zip \
> --with-zlib \
> --with-zlib-dir \
> --with-openssl
如果提示openssl版本低,请更新openssl版本
参考下面
#1、下载
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1.tar.gz
#2、解压并切换目录
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
#3、设定Openssl 安装,( --prefix )参数为欲安装之目录,也就是安装后的档案会出现在该目录下
./config --prefix=/usr/local/openssl
#4、执行命令
./config -t
#5、执行make、make install,编译Openssl
make & make install
# 若CentOS7中没有GCC编译器,执行命令 yum -y install gcc 安装GCC。
#6、切换openssl版本
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
#注意:不能直接删除软链接
#如需使用新版本开发,则需替换原来的软链接指向,即替换原动态库,进行版本升级。
#替换/lib(lib64)和/usr/lib(lib64)和/usr/local/lib(lib64)存在的相应动态库:
ln -sf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so
ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so
##源码安装php问题有些多需要更新一些软件版本。
#oniguruma问题处理
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost oniguruma-6.9.4]# yum -y install autoconf automake libtool
[root@localhost oniguruma-6.9.4]# ./autogen.sh
[root@localhost oniguruma-6.9.4]# ./configure --prefix=/usr --libdir=/lib64
[root@localhost oniguruma-6.9.4]# make && make install
问题处理完后,重新预编译
[root@localhost php-8.2.12]# ./configure --prefix=/usr/local/php \
> --enable-fpm \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-debug \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-freetype-dir \
> --enable-mbstring \
> --with-curl \
> --with-mysqli=mysqlnd \
> --with-mysql-sock=/data/mysql/mysql.sock \
> --with-pdo-mysql=mysqlnd \
> --with-config-file-path=/usr/local/php/etc \
> --with-zip \
> --with-zlib \
> --with-zlib-dir \
> --with-openssl
...
+--------------------------------------------------------------------+
| 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 php-8.2.12]# make && make install
#创建配置文件
[root@localhost php-8.2.12]# cp php.ini-development /usr/local/php/etc/php.ini
[root@localhost php-8.2.12]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-8.2.12]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
#创建启动脚本
[root@localhost php-8.2.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.2.12]# chmod +x /etc/init.d/php-fpm
#启动php
[root@localhost php-8.2.12]# /etc/init.d/php-fpm start
Starting php-fpm [01-Nov-2023 04:48:31] ERROR: [pool www] cannot get uid for user 'nginx'
[01-Nov-2023 04:48:31] ERROR: FPM initialization failed
failed
#创建nginx用户(上面编译时写的时nginx)
[root@localhost php-8.2.12]# useradd -s /sbin/nologin nginx -M
[root@localhost php-8.2.12]# /etc/init.d/php-fpm start
Starting php-fpm done
#查看端口进程
[root@localhost php-8.2.12]# ps -ef |grep php
root 59916 1 0 04:50 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx 59917 59916 0 04:50 ? 00:00:00 php-fpm: pool www
nginx 59918 59916 0 04:50 ? 00:00:00 php-fpm: pool www
root 59920 1313 0 04:50 pts/0 00:00:00 grep --color=auto php
[root@localhost php-8.2.12]# netstat -nltp |grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 59916/php-fpm: mast
### 修改php配置文件
[root@localhost php-8.2.12]# vim /usr/local/php/etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 127.0.0.01,192.168.100.101
上传wordpress包
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@localhost html]# tar -xf latest-zh_CN.tar.gz
[root@localhost html]# mv wordpress/* .
安装wordpress
上传一个主题试试