LNMP架构是一种用于搭建Web服务器环境的常用架构,由Linux、Nginx、MySQL和PHP组成
组成 | 功能 |
Linux | 作为操作系统的基础,提供稳定的环境 |
Nginx | 作为反向代理服务器,处理客户端的请求并将他们转发给后端的应用服务器 |
MySQL | 作为关系型数据库管理系统,用于存储和管理数据 |
PHP | 用来编写服务器端的动态网页代码,与数据库和其他服务进行交互 |
一、编译安装Nginx
1.关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
2.安装依赖环境
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3.创建用户(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin nginx
4.编译安装Nginx
cd /opt
tar zxvf nginx-1.22.0.tar.gz -C /opt
5.开始编译安装
cd nginx-1.22.0
#编译
./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module
6.安装并建立软连接让系统识别nginx操作命令,可以补全
make -j4 && make install ##安装
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin ##让系统可以识别nginx操作命令,可以补全
7.添加到系统服务(systemd启动)
##启动nginx
systemctl start nginx
##新建nginx.service
vim /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
8.更新nginx配置,启动并检查nginx
systemctl daemon-reload ##刷新nginx配置
systemctl start nginx ##启动nginx
systemctl status nginx ##检查nginx状态
二、编译安装MySQL服务
1.yum安装依赖环境
##安装Mysql环境依赖包
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake##yum安装
yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
2.添加用户
useradd -s /sbin/nologin mysql
3.将MySQL源代码包放在/opt目录下
cd /opt
将mysql-boost-5.7.20.tar.gz安装包拖进来
tar xf mysql-boost-5.7.20.tar.gz
4.去到mysql目录下编译安装
cd 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 -j4 && make install
5.修改配置文件
#将配置文件的内容全部删掉,然后加入以下内容
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 = 1
sql_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
6.更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
8.设置路径环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
9.初始化数据库
cd /usr/local/mysql/bin
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
10.添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
11.刷新服务并启动mysql服务
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
12.修改mysql的登录密码
mysqladmin -u root -p password "123"
#给root账号设置密码为123,提示输入的是原始密码(为空)要求输入的是原密码直接回车即可
13.测试,授权远程登录
mysql -u root -p
#退出
quit
三、编译安装PHP软件
1.安装环境依赖包
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
2.在/opt目录下安装并解压软件包
cd /opt
tar jxvf php-7.1.10.tar.bz2
3.检测编译安装环境
./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
4.编译安装
make -j4 && make install
5.创建软连接,路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
6.调整PHP配置文件
php.ini #主配置文件
php-fpm.conf #进程服务配置文件
www.conf #扩展配置文件
#修改主配置文件
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai#验证安装模块
php -m
#修改进程服务配置文件
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
--17行--去掉";"注释
pid = run/php-fpm.pid
#调整扩展配置文件:
cd /usr/local/php/etc/php-fpm.d/cp www.conf.default www.conf
7.启动php-fpm
#创建查看
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000#PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个 PHPFastCGI 管理器, 由于Nginx服务器不能处理动态页面,需要由 Nginx 把动态请求交给 php-fpm 进程进行解析
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service
8.配置nginx支持PHP解析
vim /usr/local/nginx/conf/nginx.conf
--65行--取消注释,修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #将 /scripts 修改为nginx的工作目录
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #$document_root 代表当前请求在root指令中指定的值
include fastcgi_params;
}
9.验证PHP测试页
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
10.重启nginx.service服务并在网页测试
systemctl restart nginx.service
http://192.168.7.20/index.php
四、部署Discuz论坛
1.在/opt目录下安装并解压Discuz
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip
2.移动到论坛目录中拷贝论坛目录
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/
3.调整论坛目录的权限
cd /usr/local/nginx/html/bbs/
chown -R nginx ./config/
chown -R nginx ./data/
chown -R nginx ./uc_client/
chown -R nginx ./uc_server/
都要处理
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
4.在虚拟机上访问论坛页面
http://192.168.7.20/bbs/install/index.php