CentOS7下nginx + php7 + mariadb +wordpress 的安装

CentOS7部署LNMP环境
本文介绍如何在CentOS7环境下安装配置Nginx、Mariadb及PHP7,实现LNMP环境搭建,并通过WordPress进行验证。

z在一般的运维中,都是需要安装nginx + php + mariadb数据库。注意 mariadb 实际上是mysqlserver的社区版本。功能基本上一样。从ubuntu到centos7 ,目前基本上不提供mysqlserver版本,只提供mariadb版本。

环境

centos7.8 ,192.168.137.104,内存2g

确保系统已经停用selinux 和防火墙

命令分别是.

setenforce 0
systemctl stop firewalld

如果永久性的停用这2个功能,采用如下方法

sed -i 's/eforcing/disabled/g /etc/selinux/config  && reboot
systemctl disable firewalld

安装mariadb

首先我们搜索下yum仓库上有哪些mariadb数据库。

[root@lamp ~]# yum list |grep mariadb
mariadb-libs.x86_64                         1:5.5.65-1.el7             @anaconda
mariadb.x86_64                              1:5.5.65-1.el7             base     
mariadb-bench.x86_64                        1:5.5.65-1.el7             base     
mariadb-devel.i686                          1:5.5.65-1.el7             base     
mariadb-devel.x86_64                        1:5.5.65-1.el7             base     
mariadb-embedded.i686                       1:5.5.65-1.el7             base     
mariadb-embedded.x86_64                     1:5.5.65-1.el7             base     
mariadb-embedded-devel.i686                 1:5.5.65-1.el7             base     
mariadb-embedded-devel.x86_64               1:5.5.65-1.el7             base     
mariadb-libs.i686                           1:5.5.65-1.el7             base     
mariadb-server.x86_64                       1:5.5.65-1.el7             base     
mariadb-test.x86_64                         1:5.5.65-1.el7             base 

我们选取 mariadb-server.x86_64进行安装

[root@lamp ~]# yum -y install  mariadb-server.x86_64

启动服务器并且连接数据库,低版本的mariadb的root账户默认密码是空,高版本的可能要到日志里去看数据库的root的密码。这里的密码是空

[root@lamp ~]# systemctl start mariadb
[root@lamp ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-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)]> 

安装nginx

首先增加nginx的库,默认的话centos7 是找不到直接安装包的。


[root@lamp ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.Kr7S2i: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

然后安装nginx 

[root@lamp ~]# yum -y  install nginx
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager


Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.18.0-1.el7.ngx will be installed
--> Finished Dependency Resolution

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.18.0-1.el7.ngx.x86_64                                                                                                                     1/1 

Installed:
  nginx.x86_64 1:1.18.0-1.el7.ngx                                                                                                                                      

Complete!

安装成功。

启动nginx: systemctl start nginx

浏览器中直接访问可以看到安装成功。

 

安装php7

centos7默认安装php5,安装是比较方便,但因为太老,我们尝试安装php7。默认的centos7 yum库是不带 php7的源。

1.安装epel-release
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2.安装PHP7的rpm源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3.安装PHP7
yum -y install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64

如果需要安装php-fpm在后台运行,还需要安装如下的包

yum -y install php70w-fpm php70w-opcache

正常情况下安装成功。如果出现错误,可以留言在评论区。 

启动php-fpm

systemctl start php-fpm

开机启动设置

systemctl enable php-fpm
systemctl daemon-reload

 查看端口9000是不是已经启动,netstat -an |grep 9000,如果启动就算成功。

[root@lamp ~]# netstat -an |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN

成功!

配置nginx和php

首先创建一个文件,实际上nginx的默认根目录是在/usr/share/nginx/html,我们

# cat <<EOF >/usr/share/nginx/html/phpinfo.php
<?php phpinfo();
?>
EOF

配置nginx,yum安装后的nginx默认的配置文件在/etc/nginx/nginx.conf,该文件中指明了当前conf.d中文件为模块的配置文件。安装之后有一个文件default.conf. 直接修改/etc/nginx/conf.d/default.conf.

[root@lamp conf.d]# vi default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

我们把有关php配置的#去掉,#在配置文件中表示注释。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
#修改1
        root   /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
#修改2
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

测试:

安装wordpress

首先找到wordpress的源代码,可以到这里下载  https://cn.wordpress.org/latest-zh_CN.tar.gz

使用wget下载完,解压,并复制到/usr/share/nginx/html中。

[root@lamp ~]# ll
total 12576
-rw-------. 1 root root     1261 Sep  6 05:35 anaconda-ks.cfg
-rw-r--r--  1 root root 12872547 Oct  2 02:31 wordpress-5.4.2-zh_CN.tar.gz
[root@lamp ~]# tar zxf wordpress-5.4.2-zh_CN.tar.gz 
[root@lamp ~]# ls -l
total 12580
-rw-------. 1 root root     1261 Sep  6 05:35 anaconda-ks.cfg
drwxr-xr-x  5 1006 1006     4096 Aug  3 00:00 wordpress
-rw-r--r--  1 root root 12872547 Oct  2 02:31 wordpress-5.4.2-zh_CN.tar.gz

创建数据库给wordpress使用

[root@lamp ~]# mysql -uroot << EOF
> create database wordpress;
> EOF

拷贝完成以后直接在浏览器输入地址,访问,进入安装页面,如果没权限写入配置文件,请将/usr/share/nginx/html/wordpress 设置成属主为apache.

如果访问当中出现问题,请禁用seLinux权限控制和关闭防火墙

安装成功访问页面

 

在php-fpm和nginx不在同一台机器上的时候,这个段中的php,root的配置,是指在php-fpm的机器上的目录,如果放在单独的nginx的服务器上,是访问不到php文件的。

也就是说,如果nginx接收到访问PHP文件,nginx根本不读取php文件,而是直接把这个文件的路径和其他信息交个了php-fpm去处理了。所以这里的路径也是指php在php-fpm上的路径。

 location ~ \.php$ {
        root   /usr/share/nginx/html;
        fastcgi_pass   dkphpfpm7:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

 

### 安装 LNMP 环境及 WordPress 的详细步骤 #### 准备工作 在 CentOS 7安装 LNMP 和 WordPress 需要先更新系统的软件包并确保必要的开发工具已安装。可以运行以下命令来完成准备工作: ```bash yum update -y && yum groupinstall "Development Tools" -y ``` --- #### 步骤一:安装 Nginx 及其依赖项 Nginx 是 LNMP 中的关键组件之一,以下是具体操作: 1. **安装依赖包** 使用 `yum` 命令安装所需的依赖库,包括 `zlib`, `openssl`, 和 `pcre`[^1]: ```bash yum -y install gcc-c++ && \ yum install -y pcre pcre-devel && \ yum install -y zlib zlib-devel && \ yum install -y openssl openssl-devel ``` 2. **下载并解压 Nginx 源码** 下载最新版本的 Nginx 并将其解压缩到指定目录: ```bash wget http://nginx.org/download/nginx-<version>.tar.gz && tar zxvf nginx-<version>.tar.gz && cd nginx-<version> ``` 3. **编译和安装 Nginx** 进入解压后的目录后执行以下命令进行配置、编译和安装: ```bash ./configure --prefix=/usr/local/nginx && make && make install ``` 4. **启动 Nginx** 启动服务并通过浏览器访问服务器 IP 地址验证是否正常运行: ```bash /usr/local/nginx/sbin/nginx ``` --- #### 步骤二:安装 MariaDB/MySQL 数据库 MariaDBMySQL 的分支,在此作为数据库管理系统。 1. **安装 MariaDB** 执行以下命令以安装 MariaDB: ```bash yum install mariadb-server mariadb -y ``` 2. **初始化和安全设置** 初始化数据库并启用开机自启功能: ```bash systemctl start mariadb && systemctl enable mariadb mysql_secure_installation ``` 3. **创建数据库和用户** 登录 MariaDB 控制台并为 WordPress 创建专用数据库及其权限: ```sql CREATE DATABASE wordpress; GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost' IDENTIFIED BY '<password>'; FLUSH PRIVILEGES; EXIT; ``` --- #### 步骤三:安装 PHP-FPM PHP-FPM 负责处理动态请求并与 Nginx 协同工作。 1. **安装 PHP 及扩展模块** 安装基础 PHP 版本及相关支持插件: ```bash yum install epel-release -y && yum install remi-release -y && yum-config-manager --enable remi-php73 && \ yum install php php-fpm php-mysqlnd php-gd php-json php-mbstring php-xml php-opcache -y ``` 2. **修改 PHP-FPM 配置文件** 编辑 `/etc/php-fpm.d/www.conf` 文件中的监听模式为 Unix Socket 或 TCP/IP 方式,并调整其他参数适应需求。 3. **重启 PHP-FPM** 应用更改后重新加载服务: ```bash systemctl restart php-fpm && systemctl enable php-fpm ``` 4. **配置环境变量** 将 PHP 主路径加入全局 PATH 中以便于管理[^2]: ```bash echo "export PHP_HOME=/usr/local/php7" >> /etc/profile && \ echo "export PATH=\$PATH:\$PHP_HOME/bin:\$PHP_HOME/sbin" >> /etc/profile && source /etc/profile ``` --- #### 步骤四:安装 WordPress 最后一步是在 Web 根目录下部署 WordPress CMS。 1. **获取 WordPress 源代码** 访问官方站点下载最新的稳定版 ZIP 包或者通过命令行拉取: ```bash curl -O https://wordpress.org/latest.tar.gz && tar xzf latest.tar.gz && mv wordpress/* /usr/share/nginx/html/ ``` 2. **赋予适当权限** 设置正确的所有权给网站根目录下的所有文件夹与文档: ```bash chown -R apache:apache /usr/share/nginx/html/* chmod -R 755 /usr/share/nginx/html/ ``` 3. **编辑 Nginx 配置文件** 修改默认虚拟主机配置使其能够解析 `.php` 结尾的内容指向 FastCGI 处理器[^3]。 4. **完成前端界面引导流程** 浏览至域名地址按照提示填写先前建立好的数据库连接信息即可正式上线运营。 --- ### 总结 上述过程涵盖了从零开始构建完整的 LNMP 架构直至成功导入 WordPress 的全过程。每部分均提供了详尽指导帮助理解各环节作用机制。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老骥又出发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值