本篇文章介绍LNMP架构部署项目,包括Nginx安装配置、PHP安装配置、数据库Mariadb安装配置、Nginx连通PHP、PHP连通MySQL、拆分数据库至独立服务器、拆分静态资源至服务器、扩展一个web服务器。
LNMP架构
Linux Nginx Mysql PHP
Nginx不能处理动态请求,如果是动态请求,就通过fastcgi协议转交给后端的PHP程序处理
Nginx安装配置
第一步:安装nginx
配置官网yum仓库
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
第二步:创建虚拟用户
[root@Web01 ~]# groupadd -g 666 www
[root@Web01 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
第三步:配置Nginx启动用户为www
[root@Web01 ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes auto;
第四步:启动Nginx加入开机自启动
[root@Web01 ~]# systemctl start nginx
[root@Web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
PHP安装配置
第一步:配置PHP软件仓库并安装PHP
#配置PHP仓库
[root@Web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@Web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@Web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common
#配置第三方源
[root@Web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
#安装PHP
[root@Web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb --nogpgcheck
或者是使用rpm包,上传后解压。
yum -y localinstall *.rpm
第二步:修改PHP启动用户为www
[root@Web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@Web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
第三步:启动服务并设置开机自启
[root@Web01 ~]# systemctl start php-fpm
[root@Web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
第四步:查看状态
[root@Web01 ~]# netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1272/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1413/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4133/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3989/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1272/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1413/master
udp 0 0 127.0.0.1:323 0.0.0.0:* 840/chronyd
udp6 0 0 ::1:323 :::* 840/chronyd
安装数据库mariadb-server
第一步:安装数据库
[root@Web01 ~]# yum install mariadb-server -y
第二步:启动数据库、设置开机自启动、查看状态
[root@Web01 ~]# systemctl start mariadb
[root@Web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@Web01 ~]# netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1272/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1413/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4133/php-fpm: maste
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4480/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3989/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1272/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1413/master
udp 0 0 127.0.0.1:323 0.0.0.0:* 840/chronyd
udp6 0 0 ::1:323 :::* 840/chronyd
第三步:配置用户密码,默认是root
[root@Web01 ~]# mysqladmin password '666666' #设置密码为666666
[root@Web01 ~]# mysql -uroot -p666666 #登录数据库
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; #查看有哪些库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create database hehe; #创建hehe库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> drop database hehe; #删除hehe库
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> quit #退出数据库
Bye
[root@Web01 ~]#
注意:配置数据库密码需要服务启动后才能配置
Nginx连通PHP
[root@Web01 /]# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name php.koten.com;
location / {
root /code;
index index.php index.html;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@Web01 /]# systemctl restart nginx
[root@Web01 /]# vim /code/index.php
<?php
phpinfo();
?>
windows本地hosts解析后,即可访问PHP有关信息
PHP连通MySQL
[root@Web01 /]# vim /code/mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "lzy123.com";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php可以连接MySQL...";
?>
拆分数据库至独立服务器
为什么要拆分数据库
由于单台服务器运行LNMP
架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom
从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。
注意:这种的功能是一个整体,属于集群,不属于微服务。
拆分服务器可以解决如下问题:
1.缓解web网站的压力
2.增强数据库读写性能
3.提高用户访问速度
实现流程
1、克隆一台服务器51
2、安装mariadb-server
【省略,上面有流程】
3、导出10.0.0.7上面的所有库文件,并关闭MySQL服务(如果一开始使用的51就省略该步骤了)
[root@Web01 ~]# mysqldump -uroot -p666666 -A > all.sql
[root@Web01 ~]# ll
total 2508
-rw-r--r-- 1 root root 2563044 Apr 6 09:06 all.sql
-rw-------. 1 root root 1519 Mar 29 18:41 anaconda-ks.cfg
[root@Web01 ~]# scp all.sql 10.0.0.51:/root/ #将all.sql发送到10.0.0.51的root下
The authenticity of host '10.0.0.51 (10.0.0.51)' can't be established.
ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA.
ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.51' (ECDSA) to the list of known hosts.
root@10.0.0.51's password:
all.sql 100% 2503KB 35.0MB/s 00:00
[root@Web01 ~]# systemctl stop mariadb
[root@Web01 ~]# systemctl disable mariadb
Removed symlink /etc/systemd/system/multi-user.target.wants/mariadb.service.
4、导入到10.0.0.51新数据、检查数据库是否导入成功
[root@MySQL ~]# mysql -uroot -p666666 < all.sql
[root@MySQL ~]# mysql -uroot -p666666 -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| phpshe |
| test |
| wecenter |
| wordpress |
+--------------------+
[root@MySQL ~]#
5、修改代码中连接数据库的信息(服务需要挨个修改)
浏览器访问网址
#授权普通用户,允许远程连接51
[root@MySQL ~]# mysql -uroot -p666666
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to koten@'%' identified by '666666';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@Web01 ~]# vim /code/wordpress/wp-config.php
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' ); #数据库名称
define( 'DB_USER', 'koten' ); #数据库用户
/** Database password */
define( 'DB_PASSWORD', '666666' ); #数据库密码
/** Database hostname */
define( 'DB_HOST', '10.0.0.51' ); #修改远程连接IP地址
修改后刷新浏览器,发现恢复(其他服务同理)
拆分静态资源至独立服务器
1、安装NFS
[root@NFS ~]# yum -y install nfs-utils
2、创建匿名压缩用户www
[root@NFS ~]# groupadd -g666 www
[root@NFS ~]# useradd -u666 -g666 -M -s /sbin/nologin www
3、修改配置文件
[root@NFS ~]# cat /etc/exports
/data/wordpress 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/wecenter 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4、创建必要数据文件
[root@NFS ~]# mkdir -p /data/wordpress
[root@NFS ~]# mkdir -p /data/wecenter
[root@NFS ~]# chown www.www /data/*
[root@NFS ~]# ll /data/
total 0
drwxr-xr-x 2 www www 6 Apr 6 10:08 wecenter
drwxr-xr-x 2 www www 6 Apr 6 10:08 wordpress
5、启动NFS
[root@NFS ~]# systemctl start nfs
[root@NFS ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
6、找出业务上传图片的目录并挂载
右键复制图片连接地址:找到上传目录uploads
http://blog.koten.com/wp-content/uploads/2023/04/1.jpg
1)将2023目录拷贝到31的/data/wordpress/
[root@Web01 ~]# scp -r /code/wordpress/wp-content/uploads/* 172.16.1.31:/data/wordpress/
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA.
ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.
root@172.16.1.31's password:
music-music-1.6.2.zip 100% 67KB 6.5MB/s 00:00
HTML5中国象棋.zip 100% 1571KB 30.9MB/s 00:00
小霸王-FC怀旧游 100% 7718KB 48.9MB/s 00:00
[root@Web01 ~]#
NFS重新授权下面的目录权限
[root@NFS wordpress]# chown -R www.www /data/wordpress/
[root@NFS wordpress]# ll /data/wordpress/
total 0
drwxr-xr-x 3 www www 16 Apr 6 15:22 2023
2)将31/data/wordpress挂载到uploads目录
web1安装nfs-utils
[root@Web01 ~]# yum -y install nfs-utils
查看共享目录
[root@Web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wecenter 172.16.1.0/24
/data/wordpress 172.16.1.0/24
挂载NFS共享目录/data/wordpress并查看挂载信息
[root@Web01 ~]# mount -t nfs 172.16.1.31:/data/wordpress /code/wordpress/wp-content/uploads
[root@Web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 19G 2.4G 17G 13% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/data/wordpress 19G 2.0G 17G 11% /code/wordpress/wp-content/uploads
扩展一个web节点(web2服务器)
可以直接克隆,也可以创建新服务器自己安装配置服务,同步文件
1、安装Nginx+PHP+NFS
Nginx
[root@Web02 ~]# scp 172.16.1.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA.
ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.
root@172.16.1.7's password:
nginx.repo 100% 192 110.1KB/s 00:00
[root@Web02 ~]# yum -y install nginx
PHP
安装PHP软件【文末可以下载】
[root@Web02 ~]# rz -E
rz waiting to receive.
[root@Web02 ~]# ls
anaconda-ks.cfg php71.tar.gz
[root@Web02 ~]# tar xf php71.tar.gz
[root@Web02 ~]# yum -y localinstall *.rpm
扩展:批量删除PHP软件
[root@Web02 ~]# rpm -qa|grep php|xargs yum -y remove
NFS
安装nfs-utils,但不用启动
[root@Web02 ~]# yum -y install nfs-utils
2、创建虚拟用户www
[root@Web02 ~]# groupadd -g666 www
[root@Web02 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
3、同步配置文件和WEB01相同(/etc/nginx/,/etc/php-fpm.d/www.conf)
#同步Nginx配置
[root@Web02 ~]# rsync -avz --delete 172.16.1.7:/etc/nginx/ /etc/nginx
root@172.16.1.7's password:
receiving incremental file list
./
nginx.conf
conf.d/
conf.d/default.conf
conf.d/phpshe.conf
conf.d/wecenter.conf
conf.d/wordpress.conf
sent 155 bytes received 1,701 bytes 1,237.33 bytes/sec
total size is 9,775 speedup is 5.27
#同步PHP配置
[root@Web02 conf.d]# rsync -avz --delete 172.16.1.7:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf
root@172.16.1.7's password:
receiving incremental file list
www.conf
sent 199 bytes received 425 bytes 113.45 bytes/sec
total size is 17,962 speedup is 28.79
4、同步代码文件到WEB02
打包code然后拷贝到web02,进行解压到/目录
[root@Web01 04]# tar zcvf code.tar.gz /code/
[root@Web02 ~]# scp 172.16.1.7:/code/wordpress/wp-content/uploads/2023/04/code.tar.gz /
root@172.16.1.7's password:
code.tar.gz 100% 168MB 40.6MB/s 00:04
[root@Web02 /]# tar xf code.tar.gz
[root@Web02 /]# ll /code
total 8
drwxr-xr-x 8 www www 300 Apr 6 09:56 phpshe
drwxr-xr-x 14 www www 4096 Apr 4 17:13 wecenter
drwxr-xr-x 5 www www 4096 Apr 6 09:21 wordpress
5、启动服务、挂载NFS到本地上传目录
#启动服务
[root@Web02 code]# systemctl start nginx php-fpm
[root@Web02 code]# systemctl enable nginx php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
#挂载NFS到本地上传目录
[root@Web02 code]# mount -t nfs 172.16.1.31:/data/wordpress /code/wordpress/wp-content/uploads/
[root@Web02 code]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 19G 2.4G 17G 13% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/data/wordpress 19G 2.2G 17G 12% /code/wordpress/wp-content/uploads
Linux系统PHP软件下载链接:https://pan.baidu.com/s/1E9QfpakeZA1zkA3B7-urjw?pwd=r843
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!