Linux系统运维LNMP架构搭建

本文详细介绍了如何在Linux系统中搭建LNMP(Nginx、Linux、MySQL、PHP)架构,包括Nginx的location配置、LNMP各组件的安装与配置,以及WordPress博客的搭建步骤,涵盖了从基础服务配置到应用实战的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、nginx的location配置

使用Nginx Location可以可以控制访问网站的路径,但一个server可以有多个location配置,多个location的优先级该如何区分

1、语法

Syntax:    location [ = | ~ | ~* | ^~ ] uri { ... }
        location @name { ... }
Default:    —
Context:    server, location

2、location匹配符

匹配符匹配规则优先级
=精准匹配1
^~以某个字符串开头2
~区分大小写的正则匹配3
~*不区分大小写的正则匹配4
/通用匹配,任何请求都会匹配到5

 

 

 

 

 

 

 

3、location应用场景

#通用匹配,任何请求都会匹配到

location / {

    ...

}

#严格区分大小写,匹配一PHP结尾的都走这个location

location ~ \.php$ {

    ...

}

#严格区分大小写,匹配以jsp结尾的都走这个location

location ~ \.jsp$ {

    ....

}

#不区分大小写匹配,只要用户访问 . jpg,gif,png,js,css 都走这条location

location ~* .*\.(jpg | gif | png | js | css)$ {

    ...

}

http://linux.test.com/1.PHP

http://linux.test.com/1.JPG

http://linux.test.com/1.jsp

http://linux.test.com/1.Gif

http://linux.test.com/1.PnG

http://linux.test.com/1.Jsp

二、LNMP架构

1、简介

LNMP是一套技术组合,L=Linux,N=Nginx,M=MysQL,P=PHP

不仅仅只有这些服务,还有很多

redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink

2、搭建nginx

①配置官方源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

②安装nginx

[root@web01 ~]# yum install -y nginx

③配置nginx

[root@web01 ~]# vim /etc/nginx/nginx.conf             #统一服务用户名便于管理
user  www;

④创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

⑤启动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.

#验证启动
[root@web01 ~]# ps -ef | grep nginx
root       9953      1  0 11:17 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        9954   9953  0 11:17 ?        00:00:00 nginx: worker process

3、安装PHP

①安装PHP

1.上传包
[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--. 1 root root 19889622 Nov 22 15:52 php.tar.gz

2.解压包
[root@web01 ~]# tar xf php.tar.gz

3.本地安装php的rpm包
[root@web01 ~]# yum localinstall -y *.rpm

②配置PHP

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = www
group = www

③启动服务

[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 ~]# ps -ef | grep php-fpm
root      10195      1  0 11:29 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www       10196  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10197  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10198  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10199  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10200  10195  0 11:29 ?        00:00:00 php-fpm: pool www

4、搭建 mariadb

①安装

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

②启动服务

[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 ~]# ps -ef | grep mariadb
mysql     11006  10841  1 12:06 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

④连接

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \
Your MariaDB connection id is 2
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)

⑤设置数据库密码

[root@web01 ~]# mysqladmin -uroot password '123'

#使用密码连接数据库
[root@web01 ~]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
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)]>

5、测试PHP和mariadb关联

①编写PHP测试连接数据库代码

[root@web01 ~]# vim /code/zuoye/test.php
<?php
    $servername = "localhost";
    $username = "root";
    $password = "123";

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);

    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "小哥哥,php可以连接MySQL...";
?>

<img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/php_mysql.png>

三、搭建WordPress博客

1、上传代码

[root@web01 code]# rz
[root@web01 code]# ll
-rw-r--r--. 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz

2、减压代码

[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz

3、授权减压文件

[root@web01 code]# chown -R www.www wordpress

4、配置nginx

[root@web01 code]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

5、重启访问

#检查配置
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#重启
[root@web01 code]# systemctl restart nginx

6、访问测试

#配置hosts
10.0.0.7 linux.wp.com

#访问
http://linux.wp.com/

7、创建数据库

[root@web01 code]# mysql -uroot -p123

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+-----------------------------+
| Database                    |
+-----------------------------+
| information_schema    |
| mysql                           |
| performance_schema  |
| test                               |
| wordpress                    |
+------------------------------+
5 rows in set (0.00 sec)

在浏览器输入IP地址进行访问

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值