CentOS7下nginx + php7 + mariadb +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;
    }

 

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

老骥又出发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值