本篇文章介绍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 repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_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 userwww;worker_processesauto;

四、启动Nginx加入开机自启动

[root@Web01 ~]# systemctl start nginx[root@Web01 ~]# systemctl enable nginxCreated 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 Repositorybaseurl = 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-fpmCreated symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

四、查看状态

[root@Web01 ~]# netstat -tnulpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp00 0.0.0.0:220.0.0.0:* LISTEN1272/sshd tcp00 127.0.0.1:250.0.0.0:* LISTEN1413/master tcp00 127.0.0.1:90000.0.0.0:* LISTEN4133/php-fpm: maste tcp00 0.0.0.0:800.0.0.0:* LISTEN3989/nginx: mastertcp6 00 :::22 :::*LISTEN1272/sshd tcp6 00 ::1:25:::*LISTEN1413/master udp00 127.0.0.1:323 0.0.0.0:* 840/chronyd udp6 00 ::1:323 :::*840/chronyd 

安装数据库mariadb-server

一、安装数据库

[root@Web01 ~]# yum install mariadb-server -y

二、启动数据库、设置开机自启动、查看状态

[root@Web01 ~]# systemctl start mariadb[root@Web01 ~]# systemctl enable mariadbCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.[root@Web01 ~]# netstat -tnulpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp00 0.0.0.0:220.0.0.0:* LISTEN1272/sshd tcp00 127.0.0.1:250.0.0.0:* LISTEN1413/master tcp00 127.0.0.1:90000.0.0.0:* LISTEN4133/php-fpm: maste tcp00 0.0.0.0:33060.0.0.0:* LISTEN4480/mysqld tcp00 0.0.0.0:800.0.0.0:* LISTEN3989/nginx: mastertcp6 00 :::22 :::*LISTEN1272/sshd tcp6 00 ::1:25:::*LISTEN1413/master udp00 127.0.0.1:323 0.0.0.0:* 840/chronyd udp6 00 ::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 3Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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 

windows本地hosts解析后,即可访问PHP有关信息

PHP连通MySQL

[root@Web01 /]# vim /code/mysql.php

拆分数据库至独立服务器

为什么要拆分数据库

由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。

注意:这种的功能是一个整体,属于集群,不属于微服务。

拆分服务器可以解决如下问题:

1.缓解web网站的压力
2.增强数据库读写性能
3.提高用户访问速度

实现流程

一、克隆一台服务器51

二、安装mariadb-server

【省略,上面有流程】

三、导出10.0.0.7上面的所有库文件,并关闭MySQL服务(如果一开始使用的51就省略该步骤了)

[root@Web01 ~]# mysqldump -uroot -p666666 -A > all.sql[root@Web01 ~]# lltotal 2508-rw-r--r--1 root root 2563044 Apr6 09:06 all.sql-rw-------. 1 root root1519 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)" />四、导入到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 ~]# 

五、修改代码中连接数据库的信息(服务需要挨个修改)

浏览器访问网址

#授权普通用户,允许远程连接51[root@MySQL ~]# mysql -uroot -p666666Welcome to the MariaDB monitor.Commands end with ; or \g.Your MariaDB connection id is 10Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> quitBye[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地址

修改后刷新浏览器,发现恢复(其他服务同理)

拆分静态资源至独立服务器

一、安装NFS

[root@NFS ~]# yum -y install nfs-utils

二、创建匿名压缩用户www

[root@NFS ~]# groupadd -g666 www[root@NFS ~]# useradd -u666 -g666 -M -s /sbin/nologin www

三、修改配置文件

[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)

四、创建必要数据文件

[root@NFS ~]# mkdir -p /data/wordpress[root@NFS ~]# mkdir -p /data/wecenter[root@NFS ~]# chown www.www /data/*[root@NFS ~]# ll /data/total 0drwxr-xr-x 2 www www 6 Apr6 10:08 wecenterdrwxr-xr-x 2 www www 6 Apr6 10:08 wordpress

五、启动NFS

[root@NFS ~]# systemctl start nfs[root@NFS ~]# systemctl enable nfsCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

六、找出业务上传图片的目录并挂载

右键复制图片连接地址:找到上传目录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)" />扩展一个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)? yesWarning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.root@172.16.1.7's password: nginx.repo100%192 110.1KB/s 00:00[root@Web02 ~]# yum -y install nginxPHP安装PHP软件【文末可以下载】[root@Web02 ~]# rz -Erz waiting to receive.[root@Web02 ~]# lsanaconda-ks.cfgphp71.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 removeNFS安装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/nginxroot@172.16.1.7's password: receiving incremental file list./nginx.confconf.d/conf.d/default.confconf.d/phpshe.confconf.d/wecenter.confconf.d/wordpress.confsent 155 bytesreceived 1,701 bytes1,237.33 bytes/sectotal size is 9,775speedup 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 listwww.confsent 199 bytesreceived 425 bytes113.45 bytes/sectotal size is 17,962speedup 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%168MB40.6MB/s 00:04[root@Web02 /]# tar xf code.tar.gz [root@Web02 /]# ll /codetotal 8drwxr-xr-x8 www www300 Apr6 09:56 phpshedrwxr-xr-x 14 www www 4096 Apr4 17:13 wecenterdrwxr-xr-x5 www www 4096 Apr6 09:21 wordpress

5、启动服务、挂载NFS到本地上传目录

#启动服务[root@Web02 code]# systemctl start nginx php-fpm[root@Web02 code]# systemctl enable nginx php-fpmCreated 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 -hFilesystem SizeUsed Avail Use% Mounted ondevtmpfs 476M 0476M 0% /devtmpfs487M 0487M 0% /dev/shmtmpfs487M7.7M479M 2% /runtmpfs487M 0487M 0% /sys/fs/cgroup/dev/sda3 19G2.4G 17G13% //dev/sda1197M110M 88M56% /boottmpfs 98M 0 98M 0% /run/user/0172.16.1.31:/data/wordpress 19G2.2G 17G12% /code/wordpress/wp-content/uploads

Linux系统PHP软件下载链接:https://pan.baidu.com/s/1E9QfpakeZA1zkA3B7-urjw?pwd=r843

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!