0.准备
文章目录
- 0.准备
- 1.nginx
- 2.mysql
- 2.1 mysql8
- 2.2 mysql5.7
- 3.php
- 4.测试php访问mysql
- 5.部署 Discuz
- 6.其他
yum源:
# 没有wget,用这个
# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum makecache && yum clean all
如果 yum 报错: Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64
centos7的scl源在2024年6月30日停止维护,其配置中默认使用了centos官方的地址,无法连接,需要替换为阿里云。
# 重命名原来的文件
cd /etc/yum.repos.d/
mv CentOS-SCLo-scl.repo CentOS-SCLo-scl.repo.bak
mv CentOS-SCLo-scl-rh.repo CentOS-SCLo-scl-rh.repo.bak# 编辑文件
vim CentOS-SCLo-rh.repo# 内容如下
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
安装包下载地址:
-
nginx:
- https://nginx.org/en/download.html
-
mysql8(with boost):
- https://cdn.mysql.com//Downloads/MySQL-8.1/mysql-boost-8.1.0.tar.gz
- https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-boost-8.2.0.tar.gz
-
php8
- https://www.php.net/distributions/php-8.2.3.tar.gz
-
cmake:
- https://cmake.org/files
-
libzip
- https://libzip.org/download/libzip-1.3.2.tar.gz
1.nginx
安装依赖包
yum -y install openssl openssl-devel make zlib zlib-devel gcc gcc-c++ libtool pcre pcre-devel
创建nginx用户
useradd -M -s /sbin/nologin nginx
安装nginx
#解压
tar -xf nginx-1.26.1.tar.gz -C /opt/cd /opt/nginx-1.26.1#配置
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module#编译安装
make -j 2 && make install
创建命令的软连接,方便管理
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
创建服务
vim /usr/lib/systemd/system/nginx.service# 内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
刷新系统服务,启动nginx
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx# firewall
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload
网页访问
2.mysql
2.1 mysql8
安装依赖包
yum -y install ncurses ncurses-devel libaio-devel openssl libssl-dev libncurses-dev devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils#删除依赖包
yum remove ncurses ncurses-devel libaio-devel openssl libssl-dev libncurses-dev devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils
如果已安装cmake,并且版本低于 3.5.1,则需升级 (mysql8要求更高版本,否则报错如下)
解决:
# 如果已用yum安装过cmake,则先删除
# yum -y remove cmake# 下载
# wget https://cmake.org/files/v3.30/cmake-3.30.0.tar.gz# 解压
tar -xf cmake-3.30.0.tar.gz# 配置、编译、安装
cd cmake-3.30.0/
./configure --prefix=/usr/local/cmake && make -j 2 && make install# 软连接
ln -sv /usr/local/cmake/bin/cmake /usr/bin/cmake --version
创建mysql用户、目录等
# 创建目录
mkdir /usr/local/mysql/
mkdir /usr/local/mysql/{data,log}# 创建用户
useradd -M -s /sbin/nologin mysql# 设置权限
chown -R mysql:mysql /usr/local/mysql/data
chmod -R 755 /usr/local/mysql/data
安装
cd /opt# 解压
tar xf mysql-boost-8.1.0.tar.gz# 切换目录
cd mysql-8.1.0/
mkdir build
cd build/# 配置
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data/ -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DWITH_BOOST=/opt/mysql-8.1.0/boost -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNODB_MEMCACHED=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1# 编译、安装
make -j 8 && make install
初始化数据库、配置等
# 初始化数据库
[root@localhost ~]# cd /usr/local/mysql/bin/
[root@localhost ~]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data# mysql配置文件
[root@localhost ~]# vim /etc/my.cnf[client]
port=3306
socket=/usr/local/mysql/mysql.sock[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/mysql.sock# 配置MySQL环境变量
[root@localhost ~]# echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@localhost ~]# source /etc/profile# 配置系统启动mysql
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# netstat -anptu | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 30897/mysqld
tcp6 0 0 :::33060 :::* LISTEN 30897/mysqld # 登录,设置密码
[root@localhost ~]# mysql -uroot
mysql> set password = '123456' ##设置密码
mysql> exit# 再次测试登录
[root@localhost ~]# mysql -uroot -p# 授予root远程登录权限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
2.2 mysql5.7
安装依赖包
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
创建用户
# 不创建数据库用户的目录,s指定权限
useradd -M -s /sbin/nologin mysql
解压安装包
cd /opt
tar xf mysql-boost-5.7.20.tar.gz
配置
cd /opt/mysql-5.7.20/cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
编译、安装
make -j 2 && make install
mysql 配置文件
vim /etc/my.cnf# 内容如下
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
更改目录和配置文件属性
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
初始化数据库
cd /usr/local/mysql/bin/# 执行
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
添加服务
# 拷贝服务文件
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/# 重新加载系统服务
systemctl daemon-reload# 启动mysql
systemctl start mysqld.service
systemctl enable mysqld.service
mysql 的root用户密码
mysqladmin -u root -p password "123456"# 直接回车
root用户远程登录权限
mysql -u root -p# 查看是否创建成功数据库
mysql> show databases;# 开启root远程登录权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
firewall
firewall-cmd --zone=public --add-service=mysql --permanent
firewall-cmd --reload
3.php
安装依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
解压、配置、编译、安装
# yum -y install bzip2cd /opt
tar -xf php-7.1.10.tar.bz2cd php-7.1.10# 配置
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip# 编译、安装
make -j2 && make install
软连接
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
验证安装的模块
php -m
php配置文件
- php用 ; 做注释
- php有三个配置文件:
- php.ini 主配置文件
- php-fpm.conf 进程服务配置文件(进程配置打开)
- www.conf 扩展配置文件(一般开发使用)
修改 php.ini
# 复制 php.ini
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini # 编辑
vim /usr/local/php/lib/php.ini# 939行 修改时区
date.timezone = Asia/Shanghai# 1170行 和mysql数据库进行网络通信
mysqli.default_socket = /usr/local/mysql/mysql.sock
修改 php-fpm.conf
# 复制 php-fpm.conf
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf# 编辑
vim php-fpm.conf# 17行去掉 ; 注释,
pid = run/php-fpm.pid
www.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
启动 php-fpm
PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个 PHPFastCGI 管理器, 由于Nginx服务器不能处理动态页面,需要由 Nginx 把动态请求交给 php-fpm 进程进行解析。
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000
添加 php-fpm 系统服务
#添加service文件
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service# 重启服务
systemctl restart php-fpm.service
systemctl enable php-fpm.service
nginx 支持 php 解析
# 修改主配置
vim /usr/local/nginx/conf/nginx.conf# 65行--取消注释,修改
location ~ \.php$ {#指定默认路径/usr/localroot html;#解析的本地地址fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;#将 /scripts 修改为nginx的工作目录fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #$document_root 代表当前请求在root指令中指定的值#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
重启 nginx 服务
systemctl restart nginx.service
测试php页
vim /usr/local/nginx/html/index.php# 以下内容
<?php
phpinfo();
?>
浏览器访问
4.测试php访问mysql
创建bbs数据库和用户
mysql -uroot -pmysql> CREATE DATABASE bbs;
mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
mysql> flush privileges;
编写php
vim /usr/local/nginx/html/index.php#替换原来的测试页内容
<?php
$link=mysqli_connect('192.168.9.3','bbsuser','admin123');
if($link) echo "<h1>success</h1>";
else echo "fail";
?>
浏览器访问
5.部署 Discuz
解压、复制网站
# yum -y install unzipcd /opt
unzip Discuz_X3.4_SC_UTF8.zip -d /opt/discd /opt/dis/dir_SC_UTF8/# 给予静态页面访问地址
cp -r upload/ /usr/local/nginx/html/bbs/
文件权限
cd /usr/local/nginx/html/bbschmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
网页访问
http://192.168.9.3/bbs/install/index.php
安装完毕,网页访问
# 前端
http://192.168.9.3/bbs/index.php# 后台
http://192.168.9.3/bbs/admin.php
6.其他
fpm参数优化
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid
FPM 启动时有 5 个进程,最小空闲 2 个进程,最大空闲 8 个进程,最多可以有20个进程存在。
vim /usr/local/php/etc/php-fpm.d/www.conf# 96行 fpm进程启动方式,动态的
pm = dynamic
# 107行 fpm进程启动的最大进程数
pm.max_children=20
# 112行 动态方式下启动时默认开启的进程数,在最小和最大之间
pm.start_servers = 5
# 117行 动态方式下最小空闲进程数
pm.min_spare_servers = 2
# 122行 动态方式下最大空闲进程数
pm.max_spare_servers = 8
# 重启php-fpm
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` netstat -anpt | grep 9000
ps -elf | grep php-fpm
n.php
[外链图片转存中...(img-vjV0GUbQ-1734590023574)][外链图片转存中...(img-xWJqChnc-1734590023574)]## 6.其他fpm参数优化```shell
vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid
FPM 启动时有 5 个进程,最小空闲 2 个进程,最大空闲 8 个进程,最多可以有20个进程存在。
vim /usr/local/php/etc/php-fpm.d/www.conf# 96行 fpm进程启动方式,动态的
pm = dynamic
# 107行 fpm进程启动的最大进程数
pm.max_children=20
# 112行 动态方式下启动时默认开启的进程数,在最小和最大之间
pm.start_servers = 5
# 117行 动态方式下最小空闲进程数
pm.min_spare_servers = 2
# 122行 动态方式下最大空闲进程数
pm.max_spare_servers = 8
# 重启php-fpm
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` netstat -anpt | grep 9000
ps -elf | grep php-fpm