详述Linux系统中搭建LNMP架构+Discuz论坛

LNMP架构解读

  • LNMP平台就是Linux、Ngnix、MySQL、PHP的组合架构,需要Linux服务器、MySQL 数据库、PHP解析环境

搭建Nginx服务

  • 下载Nginx源码包 Nginx源码包下载

  • 在Linux虚拟机中挂载存放源码包的目录
[root@localhost ~]# mount.cifs //192.168.100.10/lnmp /mnt/    //挂载目录
Password for root@//192.168.100.10/lnmp:  
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# yum install gcc gcc-c++ make pcre-devel zlib-devel -y     //安装环境包
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
...//省略部分内容...
已安装:
  gcc.x86_64 0:4.8.5-39.el7          gcc-c++.x86_64 0:4.8.5-39.el7      pcre-devel.x86_64 0:8.32-17.el7   
  zlib-devel.x86_64 0:1.2.7-18.el7     

作为依赖被安装:
  cpp.x86_64 0:4.8.5-39.el7                          glibc-devel.x86_64 0:2.17-292.el7                       
  glibc-headers.x86_64 0:2.17-292.el7                kernel-headers.x86_64 0:3.10.0-1062.4.1.el7             
  libmpc.x86_64 0:1.0.1-3.el7                        libstdc++-devel.x86_64 0:4.8.5-39.el7                     
更新完毕:
  make.x86_64 1:3.82-24.el7                                                                                    
作为依赖被升级:
  glibc.x86_64 0:2.17-292.el7        glibc-common.x86_64 0:2.17-292.el7      libgcc.x86_64 0:4.8.5-39.el7     
  libgomp.x86_64 0:4.8.5-39.el7      libstdc++.x86_64 0:4.8.5-39.el7         zlib.x86_64 0:1.2.7-18.el7       

完毕!
[root@localhost mnt]# tar zvxf nginx-1.12.2.tar.gz -C /opt/       //解压环境包
[root@localhost mnt]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx    //创建Nginx程序用户,不可登录系统,不创建家目录
[root@localhost nginx-1.12.2]# ./configure \     //配置nginx 
> --prefix=/usr/local/nginx \      //指定安装目录
> --user=nginx \                   //指定用户
> --group=nginx \                  //指定组
> --with-http_stub_status_module   //关联统计模块
...//省略部分内容...
 nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.12.2]# make && make install    //制作安装nginx
...//省略部分内容...
test -d '/usr/local/nginx/html' \
    || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/opt/nginx-1.12.2”
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/   //将命令放入系统命令下
[root@localhost nginx-1.12.2]# cd /lib/systemd/system    //进入system管理目录
[root@localhost system]# vim nginx.service              //创建nginx程序管理脚本
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq
[root@localhost system]# chmod 754 nginx.service    //添加执行权限
[root@localhost system]# systemctl start nginx.service    //启动服务
[root@localhost system]# netstat -ntap | grep 80      //查看端口是否开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4593/nginx: master
[root@localhost system]# systemctl stop firewalld.service      //关闭防火墙 
[root@localhost system]# setenforce 0                          //关闭增强性安全功能
  • 在客户机测试网页是否可以正常访问

详述Linux系统中搭建LNMP架构+Discuz论坛

编译安装MySQL

  • 下载源码包,并放入已经挂载到Linux的目录 MySQL源码包下载
[root@localhost system]# yum install ncurses ncurses-devel bison cmake -y       
//安装环境包 ncurses ncurses-devel 字符终端处理工具    bison 语法分析器
...//省略部分内容...      
已安装:
  bison.x86_64 0:3.0.4-2.el7    cmake.x86_64 0:2.8.12.2-2.el7    ncurses-devel.x86_64 0:5.9-14.20130511.el7_4   
作为依赖被安装:
  m4.x86_64 0:1.4.16-10.el7                                                                                    
更新完毕:
  ncurses.x86_64 0:5.9-14.20130511.el7_4                                                                      
作为依赖被升级:
  ncurses-base.noarch 0:5.9-14.20130511.el7_4            ncurses-libs.x86_64 0:5.9-14.20130511.el7_4           
完毕!
[root@localhost system]# useradd -s /sbin/nologin mysql         //创建mysql程序目录
[root@localhost system]# cd /mnt/
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/       //解压源码包 
...//省略部分内容...
mysql-5.7.20/boost/boost_1_59_0/boost/unordered/unordered_set.hpp
mysql-5.7.20/boost/boost_1_59_0/boost/unordered/unordered_set_fwd.hpp
mysql-5.7.20/boost/boost_1_59_0/boost/unordered/unordered_map_fwd.hpp
mysql-5.7.20/boost/boost_1_59_0/boost/timer.hpp
[root@localhost mnt]# cd /opt
[root@localhost opt]# ls
mysql-5.7.20  nginx-1.12.2  rh
[root@localhost opt]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# cmake \             //使用cmake配置mysql
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \         //指定安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \   //指定链接性文件路径
-DSYSCONFDIR=/etc \                               //指定配置文件存放位置
-DSYSTEMD_PID_DIR=/usr/local/mysql \              //指定PID文件存放位置
-DDEFAULT_CHARSET=utf8 \                          //指定字符集utf-8
-DDEFAULT_COLLATION=utf8_general_ci \             //指定字符集utf-8
-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 \                              //关联支持c++运行库
-DWITH_SYSTEMD=1                                  //开启systemd(主从复制使使用)
[root@localhost mysql-5.7.20]# make             //制作MySQL
[root@localhost mysql-5.7.20]# make install     //安装MySQL
[root@localhost mysql-5.7.20]# cd /usr/local/  //进入MySQL安装目录
[root@localhost local]# ls -l                     //长格式常看
总用量 0
drwxr-xr-x.  2 root root   6 11月  5 2016 bin
drwxr-xr-x.  2 root root   6 11月  5 2016 etc
drwxr-xr-x.  2 root root   6 11月  5 2016 games
drwxr-xr-x.  2 root root   6 11月  5 2016 include
drwxr-xr-x.  2 root root   6 11月  5 2016 lib
drwxr-xr-x.  2 root root   6 11月  5 2016 lib64
drwxr-xr-x.  2 root root   6 11月  5 2016 libexec
drwxr-xr-x. 11 root root 197 11月 11 21:42 mysql
drwxr-xr-x. 11 root root 151 11月 11 20:49 nginx
drwxr-xr-x.  2 root root  19 11月 11 20:49 sbin
drwxr-xr-x.  5 root root  49 8月  10 03:42 share
drwxr-xr-x.  2 root root   6 11月  5 2016 src
[root@localhost local]# chown -R mysql.mysql mysql/    //更改mysql目录属主、属组为mysql程序用户
[root@localhost local]# ls -l
总用量 0
drwxr-xr-x.  2 root  root    6 11月  5 2016 bin
drwxr-xr-x.  2 root  root    6 11月  5 2016 etc
drwxr-xr-x.  2 root  root    6 11月  5 2016 games
drwxr-xr-x.  2 root  root    6 11月  5 2016 include
drwxr-xr-x.  2 root  root    6 11月  5 2016 lib
drwxr-xr-x.  2 root  root    6 11月  5 2016 lib64
drwxr-xr-x.  2 root  root    6 11月  5 2016 libexec
drwxr-xr-x. 11 mysql mysql 197 11月 11 21:42 mysql
drwxr-xr-x. 11 root  root  151 11月 11 20:49 nginx
drwxr-xr-x.  2 root  root   19 11月 11 20:49 sbin
drwxr-xr-x.  5 root  root   49 8月  10 03:42 share
drwxr-xr-x.  2 root  root    6 11月  5 2016 src
[root@localhost local]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306 
default-character-set=utf8
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
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
:wq
[root@localhost local]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile 
//在profile目录定义环境变量
[root@localhost local]# echo 'export PATH' >> /etc/profile      //将声明环境变量写入profile目录
[root@localhost local]# source /etc/profile       //使用source执行profile目录
[root@localhost local]# echo $PATH               //输出环境变量,查看定义的环境变量是否被系统识别
/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost local]# cd /usr/local/mysql/
[root@localhost mysql]# bin/mysqld \         
--initialize-insecure \             //初始化数据库
--user=mysql \
--basedir=/usr/local/mysql \       //指定数据库工作路径
--datadir=/usr/local/mysql/data    //指定数据存放位置
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /lib/systemd/system/  
//复制启动脚本到system管理器
[root@localhost mysql]# systemctl enable mysqld.service    //设置开启自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# systemctl start mysqld.service            //启动服务
[root@localhost mysql]# netstat -ntap | grep 3306          //查看服务端口是否开启
tcp6       0      0 :::3306                 :::*                    LISTEN      24084/mysqld
[root@localhost mysql]# mysqladmin -u root -p password      //设置数据库密码
Enter password:                                            //输入旧密码,没有密码直接回车
New password:                                    //设置新密码
Confirm new password:                            //再次输入新密码
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

源码编译安装php

  • 下载源码包,并放入已经挂载到Linux的目录 php源码包下载
[root@localhost ~]# yum install -y libjpeg libjpeg-devel libpng libpng-devel libxml2 libxml2-devel freetype freetype-devel zlib zlib-devel curl curl-devel openssl openssl-devel    //安装环境包
...//省略部分内容...
已安装:
  freetype-devel.x86_64 0:2.8-14.el7                        libcurl-devel.x86_64 0:7.29.0-54.el7     
  libjpeg-turbo-devel.x86_64 0:1.2.90-8.el7                 libpng-devel.x86_64 2:1.5.13-7.el7_2     
  libxml2-devel.x86_64 0:2.9.1-6.el7_2.3                    openssl-devel.x86_64 1:1.0.2k-19.el7
...//省略部分内容...
完毕!
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# tar zxvf php-7.1.20.tar.gz -C /opt/              //解压源码包
[root@localhost mnt]# cd /opt/
[root@localhost opt]# ls
mysql-5.7.20  nginx-1.12.2  php-7.1.20  rh
[root@localhost opt]# cd php-7.1.20/
[root@localhost php-7.1.20]# ./configure \             //配置php
--prefix=/usr/local/php \                              //定义安装路径
--with-mysql-sock=/usr/local/mysql/mysql.sock \        //关联mysql数据库
--with-mysqli \                                        //关联MySQL客户端
--with-zlib \                                          //支持压缩
--with-curl \                                          
--with-gd \                                            //支持图像处理
--with-jpeg-dir \                                      //支持jpeg图片
--with-png-dir \
--with-freetype-dir \                                  //支持字体处理
--with-openssl \                                       //支持安全功能
--enable-fpm \                                         //支持动态请求
--enable-mbstring \                                    //支持字符串处理
--enable-xml \                                         //支持xml格式
--enable-session \                                     //指出session会话
--enable-ftp \                                         //指出ftp功能
--enable-pdo \                                         //指出通用接口
--enable-tokenizer \                                   //支持tokenizer函数库
--enable-zip                                          //支持压缩
[root@localhost php-7.1.20]# make && make install   //制作安装php
[root@localhost php-7.1.20]# cp php.ini-development /usr/local/php/lib/php.ini 
//复制php配置文件到安装的php目录下
[root@localhost php-7.1.20]# vim /usr/local/php/lib/php.ini    //编辑配置文件
...//省略部分内容...
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /usr/local/mysql.sock             //找到此条目,输入关联的mysql数据路径
...//省略部分内容...
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai                       //找到此条目,设置时区
...//省略部分内容...
:wq
[root@localhost php-7.1.20]# /usr/local/php/bin/php -m     //查看php默认模块是否开启
[PHP Modules]
Core
ctype
curl
...//省略部分内容...
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf  //开启fpm配置文件
[root@localhost etc]# vim php-fpm.conf     //编辑配置文件
...//省略部分内容...
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
pid = run/php-fpm.pid           //开启此条目
...//省略部分内容...
:wq
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf      //开启扩展配置文件
[root@localhost php-fpm.d]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini //启动php
[root@localhost php-fpm.d]# netstat -ntap | grep 9000    //查看端口是否开启
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      35687/php-fpm: mast
[root@localhost php-fpm.d]# ln -s /usr/local/php/bin/* /usr/local/bin/   
//将php命令建立软链接到系统命令目录
  • 设置nginx支持php
[root@localhost php-fpm.d]# vim /usr/local/nginx/conf/nginx.conf    //编辑nginx配置文件
...//省略部分内容...
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; //更改站点目录
            include        fastcgi_params;
        }
...//省略部分内容...
:wq
[root@localhost php-fpm.d]# systemctl stop nginx.service         //停止nginx服务
[root@localhost php-fpm.d]# systemctl start nginx                //启动服务
[root@localhost php-fpm.d]# cd /usr/local/nginx/html/           //进入nginx站点目录
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mv index.html index.php             //将index.html更改为index.php
[root@localhost html]# ls
50x.html  index.php
[root@localhost html]# vim index.php                     //编辑网页内容
<?php
 phpinfo();
?>
:wq
  • 测试lnpm架构是否搭建成功

详述Linux系统中搭建LNMP架构+Discuz论坛

搭建Discuz论坛

  • 下载Discuz,并放入已经挂载到Linux的目录 Discuz压缩包下载
[root@localhost html]# mysql -u root -p                    //进入mysql数据库
Enter password:                                            //输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> CREATE DATABASE bbs;                  //创建bbs数据库
Query OK, 1 row affected (0.02 sec)

mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
//提权数据库用户bbsuser为管理员并设定密码
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;                  //刷新数据库
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;                     //查看数据库内容
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> quit                               //退出
Bye
[root@localhost html]# cd /mnt
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# unzip Discuz_X3.4_SC_UTF8.zip -d /opt/    //解压到opt目录
[root@localhost mnt]# cd /opt/
[root@localhost opt]# ls
dir_SC_UTF8  mysql-5.7.20  nginx-1.12.2  php-7.1.20  rh  说明.htm
[root@localhost opt]# cd dir_SC_UTF8/              //进入论坛目录
[root@localhost dir_SC_UTF8]# ls
readme  upload  utility
[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs 
//复制/opt目录里的内容到html站点的bbs目录中
[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/bbs/       //进入bbs站点目录
[root@localhost bbs]# chown -R root:nginx ./config/            //修改属组
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/
[root@localhost bbs]# chmod -R 777 ./config/                      //修改全部权限
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/
  • 使用客户机访问站点,安装Discuz论坛
    详述Linux系统中搭建LNMP架构+Discuz论坛详述Linux系统中搭建LNMP架构+Discuz论坛详述Linux系统中搭建LNMP架构+Discuz论坛详述Linux系统中搭建LNMP架构+Discuz论坛详述Linux系统中搭建LNMP架构+Discuz论坛详述Linux系统中搭建LNMP架构+Discuz论坛