LNMP实践-博客平台+电商平台

实践:Wordpress

wordpress

WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,使用PHP语言和MySQL数据库开发,用户可以在支持 PHP 和 MySQL 数据库的服务器上使用自己的博客。

官网安装手册下载

LNMP:Linux Nginx MySQL PHP

[root@server ~ 09:03:24]# hostnamectl set-hostname wordpress.server.cloud

部署数据库

[root@wordpress ~ 09:03:43]# yum install -y mariadb-server
[root@wordpress ~ 09:04:10]# systemctl enable mariadb.service --now
[root@wordpress ~ 09:04:25]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]    
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

部署 Nginx

[root@wordpress ~ 09:04:38]# yum install -y nginx
[root@wordpress ~ 09:05:56]# systemctl enable nginx.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@wordpress ~ 09:05:26]# echo Hello Wrold From Nginx > /usr/share/nginx/html/index.html
[root@wordpress ~ 09:06:01]# curl 10.1.8.10
Hello Wrold From Nginx

部署 PHP

[root@wordpress ~ 09:06:06]# yum install -y php php-fpm php-mysqlnd
[root@wordpress ~ 09:07:12]# vim /etc/php-fpm.d/www.conf
# 修改以下两个参数
user = nginx
group = nginx
[root@wordpress ~ 09:08:21]# systemctl enable php-fpm.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

配置 Nginx 对接 PHP

[root@wordpress ~ 09:10:13]# vim /etc/nginx/default.d/php.conf
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

# 配置web服务器默认主页
[root@wordpress ~ 09:13:51]# vim /etc/nginx/nginx.conf
......
# 在nginx主配置文件 server 块 root 参数下配置index参数
    server {
        ......
        root         /usr/share/nginx/html;
        # 指定主页
        index index.php index.html;
......
[root@wordpress ~ 09:11:03]# systemctl restart nginx.service 

[root@wordpress ~ 09:13:34]# cat > /usr/share/nginx/html/index.php <<EOF
<?php
  echo "<h1>Hello World !</h1>\n";
?>
EOF
[root@wordpress ~ 09:13:49]# curl http://10.1.8.10/
<h1>Hello World !</h1>

准备数据库

[root@wordpress ~ 09:18:14]# mysql -uroot -p
Enter password: 
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)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user wordpress identified by 'wordpress';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to wordpress;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

测试php是否可以访问

[root@wordpress ~ 09:19:39]# cat > /usr/share/nginx/html/test-mysql.php <<'EOF'
<?php
  $link=mysqli_connect('10.1.8.10','wordpress','wordpress');
  if($link)
    echo "<h1>Connect Mysql Success !</h1>\n";
  else
    echo "<h1>Connect Mysql Failed !</h1>\n";
  $link->close();
?>
EOF

[root@wordpress ~ 09:21:48]# curl http://10.1.8.10/test-mysql.php
<h1>Connect Mysql Success !</h1>

部署 wordpress 应用

# 上传到家目录
[root@wordpress ~ 09:22:51]# pwd
/root
[root@wordpress ~ 09:22:54]# ls wordpress-4.9.4-zh_CN.zip 
wordpress-4.9.4-zh_CN.zip
[root@wordpress ~ 09:22:55]# unzip wordpress-4.9.4-zh_CN.zip
[root@wordpress ~ 09:23:21]# ls -d wordpress
wordpress
[root@wordpress ~ 09:23:25]# mv wordpress /usr/share/nginx/html/
[root@wordpress ~ 09:24:02]# chown -R nginx:nginx /usr/share/nginx/html/

初始化站点

浏览器访问http://blog.server.cloud/wordpress

提示windows客户端要配置好域名解析。

# windows客户端修改C:\Windows\System32\drivers\etc\hosts
# Linux或Unix修改 /etc/hosts
# 添加如下记录
10.1.8.10 blog.server.cloud

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

实践:ecshop

ecshop 介绍

官网

ECShop多场景在线商城。

实验环境

CentOS 7.9

ecshop 安装

准备 LNMP 环境

准备 Nginx
# 安装
[root@ecshop ~]# yum install -y nginx
[root@ecshop ~]# systemctl enable nginx.service --now
[root@ecshop ~]# echo "Nginx Test Page" > /usr/share/nginx/html/index.html
# 测试
[root@ecshop ~]# curl http://www.server.cloud/
Nginx Test Page
准备 Mariadb
## Mariadb 5.5.68-1.el7
[root@ecshop ~]# yum install -y mariadb-server
[root@ecshop ~]# systemctl enable mariadb --now

# 安全初始化
# 设置root密码为redhat
# 删除匿名用户
# 删除测试数据库
[root@ecshop ~]# mysql_secure_installation
准备 PHP
## PHP 5.4.16
[root@ecshop ~]# yum install -y php php-fpm
[root@ecshop ~]# systemctl enable php-fpm.service --now

# 配置虚拟主机
[root@ecshop ~]# vim /etc/nginx/conf.d/vhost-www.server.cloud.conf
server {
    listen       80;
    listen       [::]:80;
    server_name  www.server.cloud;
    root         /usr/share/nginx/html;
    index index.php;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
[root@ecshop ~]# systemctl restart nginx

# 准备测试页面
[root@ecshop ~]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > /usr/share/nginx/html/test.php

# 客户端测试
[root@client ~]# curl http://www.server.cloud/test.php

准备数据库

[root@ecshop ~]# mysql -u root -predhat
MariaDB [(none)]> CREATE DATABASE ecshop;
MariaDB [(none)]> CREATE USER ecshop@localhost IDENTIFIED BY 'redhat';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON ecshop.* TO ecshop@localhost;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

准备 ecshop 站点

准备 ecshop 站点数据文件,这里使用 ECShop_V4.1.20 版本。

# 上传到 root 用户家目录
[root@ecshop ~]# unzip ECShop_V4.1.20_UTF8.zip
[root@ecshop ~]# mv /usr/share/nginx/html/ /usr/share/nginx/html.ori
[root@ecshop ~]# cp -a ECShop_V4.1.20_UTF8_release20250416/source/ecshop /usr/share/nginx/html
[root@ecshop ~]# chown nginx:nginx -R /usr/share/nginx/html

# 安装站点需要的各种扩展包
[root@ecshop ~]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt php-mysqlnd

# 修改 php-fpm运行用户身份
[root@ecshop ~]# vim /etc/php-fpm.d/www.conf
# 更改以下两条记录
# user = apache
user = nginx

# group = apache
group = nginx

[root@ecshop ~]# chown nginx:nginx -R /var/lib/php/
[root@ecshop ~]# systemctl restart nginx php-fpm

配置过程

客户端登录 http://www.server.cloud

在这里插入图片描述

在这里插入图片描述

如果时区选择UTC,无法安装,则选择 中国 上海

在这里插入图片描述

激活系统,享受更多服务。不激活也可以使用。关闭网页。

在这里插入图片描述

商城首页 http://www.server.cloud

在这里插入图片描述

商城管理后台 http://www.server.cloud/admin

在这里插入图片描述

使用ecshop账户登录

在这里插入图片描述

实践:反向代理

目标

访问 http://www.server.cloud/shop/ -> http://shop.server.cloud/
访问 http://www.server.cloud/blog/ -> http://blog.server.cloud/

实验过程

配置后端服务器

[root@server conf.d 11:10:31]# pwd
/etc/nginx/conf.d
[root@server conf.d 11:20:17]# cat vhost-shop.server.cloud.conf 
server {
    listen       80;
    listen       [::]:80;
    server_name  shop.server.cloud;
    root         /usr/share/nginx/shop;
    
    # 设置默认主页
    index index.php;
    autoindex on;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
}

[root@server conf.d 11:20:24]# cat vhost-blog.server.cloud.conf 
server {
    listen       80;
    listen       [::]:80;
    server_name  blog.server.cloud;
    root         /usr/share/nginx/blog;
    
    # 设置默认主页
    index index.php;
    autoindex on;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
}

[root@server nginx 11:20:53]# ls -d blog/ shop/
blog/  shop/
blog:目录来源于wordpress压缩包
shop:来源于ECshop压缩包
修改属主

php 配置

[root@server nginx 11:21:06]# cat /etc/nginx/default.d/php.conf 
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

配置代理服务

[root@proxy ~ 11:18:49]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.10 shop.server.cloud
10.1.8.10 blog.server.cloud

[root@client ~ 11:23:03]# cat /etc/nginx/conf.d/proxy.conf
server {
        listen      80 default_server;
        listen      [::]:80 default_server;
        server_name www.server.cloud;
        root         /usr/share/nginx/html;
        
        location /shop/ {
            proxy_pass http://shop.server.cloud/;
        }
        location /blog/ {
            proxy_pass http://blog.server.cloud/;
        }
}
Logo

电商企业物流数字化转型必备!快递鸟 API 接口,72 小时快速完成物流系统集成。全流程实战1V1指导,营造开放的API技术生态圈。

更多推荐