LNMP架构部署!
Posted handsomeboy-东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP架构部署!相关的知识,希望对你有一定的参考价值。
LNMP概述
部署LNMP架构
安装Nginx服务
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.150 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::fee0:5929:f3f8:11bc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:08:13:3d txqueuelen 1000 (Ethernet)
RX packets 168706 bytes 210040268 (200.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 80723 bytes 7713895 (7.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost opt]# tar zxf nginx-1.15.9.tar.gz
[root@localhost opt]# cd nginx-1.15.9/
[root@localhost nginx-1.15.9]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.15.9]# yum install -y gcc gcc-c++ pcre-devel zlib-devel make
#### 下载安装nginx所需的软件
[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx \\
> --user=nginx \\
> --group=nginx \\
> --with-http_stub_status_module
[root@localhost nginx-1.15.9]#make && make install
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# 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 nginx-1.15.9]# nginx
[root@localhost nginx-1.15.9]# netstat -antp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 118523/nginx: maste
[root@localhost nginx-1.15.9]# vim /usr/lib/systemd/system/nginx.service #将nginx给予systemctl管理
[unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecSTOP=/bin/kill -s QUIT $MAINPID
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=true
[Install]
wantedBy=multi-user.target
[root@localhost nginx-1.15.9]# chmod 754 /usr/lib/systemd/system/nginx.service
[root@localhost nginx-1.15.9]# systemctl enable nginx.service
[root@localhost nginx-1.15.9]# systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
#### 这里出现错误
[root@localhost nginx-1.15.9]# netstat -antp | grep nginx #可以查出nginx在前面已经开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 118523/nginx: maste
[root@localhost nginx-1.15.9]# kill -3 118523 #先停止nginx
[root@localhost nginx-1.15.9]# systemctl start nginx #用systemctl重新开启
[root@localhost etc]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf #设置nginx配置文件,使其支持php
[root@localhost conf]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
~
安装MySQL数据库
[root@localhost opt]# tar zxf mysql-boost-5.7.20.tar.gz
[root@localhost opt]# yum install -y install ncurses ncurses-devel bison cmake
[root@localhost opt]# cd mysql-5.7.20/
[root@localhost 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 \\ #指定pid目录文件
> -DDEFAULT_CHARSET=utf8 \\ #指定中文字符集
> -DDEFAULT_COLLATION=utf8_general_ci \\ #字符集设定
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \\ #一下三行均为存储引擎ENGINE
> -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 #守护进程id
[root@localhost mysql-5.7.20]# make && make install
[root@localhost local]# useradd -s /sbin/nologin mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost local]# vim /etc/my.cnf #编辑mysql主配值文件,删除原有内容,改为以下内容
[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] #mysql服务配置
user = mysql
basedir = /usr/local/mysql #工作目录
datadir = /usr/local/mysql/data #数据文件目录
port = 3306
character_set_server=utf8 #对应中文字符集
pid-file = /usr/local/mysql/mysqld.pid #PID文件位置
socket = /usr/local/mysql/mysql.sock #通讯文件
server-id = 1 #服务id,在之后的mysql集群中用于标识mysql服务器
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 #默认加载的模块
skip-grant-tables :跳过数据库权限验证命令,即忘记数据库密码时可以使用进入数据库
将mysql主配值文件设置属主属组,设置环境变量
[root@localhost local]# chown mysql:mysql /etc/my.cnf
[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]# source /etc/profile
[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 /usr/lib/systemd/system/
#### 将mysqld给予systemctl管理
[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 -antp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 121407/mysqld
[root@localhost mysql]# mysqladmin -u root -p password #设置root用户登录mysql数据库的密码
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.
[root@localhost mysql]# mysql -u root -p123456 #进入mysql数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
安装PHP服务
[root@lo[root@localhost opt]# cd php-7.1.10/
calhost opt]# tar jxf php-7.1.10.tar.bz2
[root@localhost php-7.1.10]# yum -y install \\ #安装需要的依赖环境
> libjpeg \\ #下载jpg图片依赖
> libjpeg-devel \\
> libpng libpng-devel \\ #下载png图片依赖
> freetype freetype-devel \\ #字体
> libxml2 \\ #支持xml
> libxml2-devel \\
> zlib zlib-devel \\ #压缩格式
> curl curl-devel \\ #识别curl
> openssl openssl-devel
[root@localhost php-7.1.10]# ./configure \\
> --prefix=/usr/local/php \\
> --with-mysql-sock=/usr/local/mysql/mysql.sock \\ #指定sock通讯文件位置
> --with-mysqli \\
> --with-zlib \\
> --with-curl \\
> --with-gd \\
> --with-jpeg-dir \\
> --with-png-dir \\
> --with-freetype-dir \\
> --with-openssl \\
> --enable-fpm \\ #开启fpm模块
> --enable-mbstring \\ #开启多字段模块
> --enable-xml \\ #开启拓展性标记语言模块
> --enable-session \\ #指定会话
> --enable-ftp \\ #文本传输协议
> --enable-pdo \\ #函数库
> --enable-tokenizer \\ #令牌解释器
> --enable-zip #指定压缩格式
[root@localhost php-7.1.10]#make && make install
………………………………
PHP的三个配置文件:
php.ini :核心配置文件
php-fpm.conf :进程服务配置文件
www.conf :拓展配置文件
[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
#### 复制php模板文件至核心配置文件
[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m #验证安装的模块
[root@localhost php-7.1.10]# cd /usr/local/php/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf #复制php-fpm配置文件,使其能使用
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost etc]# vim php-fpm.conf
[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin
[root@localhost etc]# ln -s /usr/local/php/sbin/* /usr/local/sbin
[root@localhost etc]# php-fpm #开启php
[root@localhost etc]# netstat -antp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5764/php-fpm: maste
[root@localhost conf]# systemctl restart nginx.service
[root@localhost conf]# netstat -antp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5959/nginx: master
[root@localhost conf]# ps aux | grep -c "php-fpm" #查看php的进程
4
测试数据库
[root@localhost conf]# mysql -uroot -p123456
mysql> create database bbs;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all on bbs.* to 'bbsuser'@'%' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.11 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.07 sec)
mysql> exit
Bye
[root@localhost conf]# vim /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('192.168.43.150','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "fail!!";
?>
设置论坛
[root@localhost opt]# mkdir dis
[root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip -d dis
[root@localhost opt]# cd dis
[root@localhost dis]# ls
dir_SC_UTF8 说明.htm
[root@localhost dis]# cd dir_SC_UTF8/
[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]# 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/
- 点我同意然后两个下一步
总结
- Nginx处理静态页面,将动态资源请求交给PHP当中的fpm处理动态请求(开启了支持FastCGI方式对接php,端口为9000)
- MySQL用于持久性数据存储
- PHP主要处理动态页面,开启了fpm进程模块配置网页动静分离
以上是关于LNMP架构部署!的主要内容,如果未能解决你的问题,请参考以下文章