前言
LAMP平台应该是目前应用最为广泛的网站服务器架构,但随着Nginx在企业中的使用越来越多,LNMP(或LEMP)架构也受到越来越多Linux系统工程师的青睐,其中"E"来自于Nginx的发音[engine x]
构建LNMP网站平台
构建LNMP平台需要Linux服务器、Nginx服务器、MySQL数据库、PHP解析环境,具体方法和LAMP差不多,Nginx服务器的搭建方法上篇博客已经介绍了,下面以安装好的Nginx服务器为基础,搭建LNMP平台
实现目标:
1.安装Nginx服务器
2.安装Mysql数据库
3.安装PHP
4.部署Discuz
实验环境:Centos 7.6 Nginx1.15
[root@localhost opt]# iptables -F
[root@localhost opt]# systemctl restart firewalld.service
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
ncurses-5.6.tar.gz rh
#解压缩
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# yum -y install \
> gcc \
> gcc-c++ \
> pcre-devel \
> zlib-devel
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with—http_stub_status_module
#编译
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# nginx
nginx
#检查配置文件
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx #描述
After=network.target #服务类别
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx #启动服务
ExecReload=/usr/bin/kill -S HUP $MAINPID #根据PID重载配置
ExecStop=/usr/bin/kill -S QUIT $MAINPID #根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#增加权限
[root@localhost ~]# cd /lib/systemd/system
[root@localhost system]# chmod 754 nginx.service
或者用service
[root@localhost nginx-1.12.2]# cd /etc/init.d/
[root@localhost init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
[root@localhost init.d]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22379/nginx: master
[root@localhost init.d]# setenforce 0
[root@localhost opt]# tar zxvf mysql-boost-5.7.20.tar.gz
[root@localhost opt]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# yum -y install \
> ncurses \ #字符终端包,方便
> ncurses-devel \
> bison \ #函数库
> cmake #没有configure用cmake
[root@localhost mysql-5.7.20]# useradd -s /sbin/nologin mysql
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #指定安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ #sock;通讯文件,连接数据库,通讯协议的载体
-DSYSCONFDIR=/etc \ #etc下放的是mysql配置文件
-DSYSTEMD_PID_DIR=/usr/local/mysql \ #pid文件位置在mysql
-DDEFAULT_CHARSET=utf8 \ #此行和下一行为字符集相关
-DDEFAULT_COLLATION=utf8_general_ci \
-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 \ #指定boost位置'
-DWITH_SYSTEMD=1 #守护进程
[root@localhost mysql-5.7.20]# make
[root@localhost mysql-5.7.20]# make install
[root@localhost mysql-5.7.20]# cd /usr/local/
[root@localhost local]# chown -R mysql.mysql mysql/
[root@localhost local]# ll
drwxr-xr-x. 11 mysql mysql 197 8月 9 10:48 mysql
...省略内容
[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
[root@localhost local]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
#申明全局环境变量
[root@localhost local]# echo 'export PATH' >> /etc/profile
#查看插入内容
[root@localhost local]# tail -2 /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
source使加入的数据生效,使mysql的相关命令识别
[root@localhost local]# source /etc/profile
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ls
bin docs man README-test usr
COPYING include mysql-test share
COPYING-test lib README support-files
[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/
[root@localhost system]# ls -l mysqld.service
-rw-r--r--. 1 root root 1639 Dec 23 17:33 mysqld.service
[root@localhost mysql]# systemctl start mysqld.service
[root@localhost mysql]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22216/nginx: master
[root@localhost mysql]# netstat -natp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 41087/mysqld
[root@localhost mysql]# systemctl enable mysqld.service
[root@localhost mysql]# mysqladmin -u root -p passwordEnter password: #默认空密码直接回车
New password: #设定新密码 这边设置abc123
Confirm new password: #再次输入密码
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@localhost mysql]# mysqladmin -u root -p password
[root@localhost mysql]# mysql -u root -p
Enter password: #输入密码
....省略内容
mysql> quit
Bye
[root@localhost mysql]# yum -y install \
libjpeg \ #jpeg图片识别
libjpeg-devel \ #jpeg开发包
libpng libpng-devel \ #png图片识别还有开发包
freetype freetype-devel \ #freetype 识别字体
libxml2 \ #xml文件格式识别
libxml2-devel \
zlib zlib-devel \ #zlib压缩
curl curl-devel \ #curl 支持文件上传下载,网页端的下载
openssl openssl-devel #身份验证去连接网站
[root@localhost mysql]# cd /opt
[root@localhost opt]# tar jxvf php-7.1.10.tar.bz2
[root@localhost php-7.1.10]#./configure \
--prefix=/usr/local/php \ #指定安装目录
--with-mysql-sock=/usr/local/mysql/mysql.sock \ #指向mysql的sock文件,用以后台程序去连接数据库
--with-mysqli \ #客户终端工具,可以直接去访问mysql服务器,去找它进行定义存储过程,事务的处理一些功能
--with-zlib \
--with-curl \
--with-gd \ #gd是一个图像化的处理
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \ #字体
--with-openssl \
--enable-fpm \ #fpm 是指动态请求的模块
--enable-mbstring \ #mbstring 多节点字符串
--enable-xml \ #xml 启用xml格式支持
--enable-session \ #支持session会话
--enable-ftp \ #网页可以ftp的功能
--enable-pdo \ #pdo 类似一个通用的接口一样,下面的不同的数据库的数据,会统一接口接收
--enable-tokenizer \ #tokenizer 是一个函数库
--enable-zip #zip 压缩
#make编译
[root@localhost php-7.1.10]# make && make install
#时间有点长需要慢慢等
[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini
#进来/搜索mysqli.default_socket
mysql.default_socket = /usr/local/mysql/mysql.sock
#搜索date.time 添加时区
date.timezone = Asia/Shanghai
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
...省略内容
[root@localhost php-7.1.10]# cd /usr/local/php/
[root@localhost php]# ls
bin etc include lib php sbin var
[root@localhost php]# cd etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
[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]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@localhost php-fpm.d]# netstat -ntap | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 50968/php-fpm: mast
#创建软链接让php的命令便于识别
[root@localhost php-fpm.d]# ln -s /usr/local/php/bin/* /usr/l/bin/
[root@localhost php-fpm.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf nginx.conf
fastcgi.conf.default nginx.conf.default
fastcgi_params scgi_params
fastcgi_params.default scgi_params.default
koi-utf uwsgi_params
koi-win uwsgi_params.default
mime.types win-utf
mime.types.default
[root@localhost conf]# vim nginx.conf
#/搜索.php
[root@localhost conf]# systemctl stop nginx.service
[root@localhost conf]# systemctl start nginx.service
[root@localhost conf]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mv index.html index.php
[root@localhost html]# vim index.php
#删除里面内容 加入下面的内容
[root@localhost html]# mysql -u root -p
##输入下面内容
mysql> GRANT all ON bbs.* To 'bbsuser'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 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
#进入opt
[root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip
[root@localhost opt]# ls
dir_SC_UTF8 mysql-5.7.20 nginx-1.12.2.tar.gz rh
Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz pcre-devel zlib-devel
gcc ncurses-5.6.tar.gz php-7.1.10 说明.htm
gcc-c++ nginx-1.12.2 php-7.1.10.tar.bz2
[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
[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/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]#
[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/
总结:
LNMP架构的组成
PHP解析方法
LNMP架构下部署应用