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;
    }

 

你可以按照以下步骤在CentOS安装WordPress: 1. 首先,确保您的CentOS服务器上已安装NginxMySQL。您可以使用以下命令安装它们: ``` sudo yum install nginx mariadb-server ``` 2. 安装PHP及其相关扩展。执行以下命令: ``` sudo yum install php php-fpm php-mysqlnd php-gd php-xml php-mbstring ``` 3. 配置Nginx以将请求代理到PHP-FPM。首先,编辑Nginx的默认配置文件: ``` sudo nano /etc/nginx/conf.d/default.conf ``` 将以下内容添加到`server`块中: ``` location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 保存并关闭文件。 4. 启动NginxPHP-FPM,并设置它们在系统启动时自动启动: ``` sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl start php-fpm sudo systemctl enable php-fpm ``` 5. 配置MySQL数据库。首先启动MySQL服务: ``` sudo systemctl start mariadb ``` 然后运行MySQL安全脚本以提高安全性并设置root密码: ``` sudo mysql_secure_installation ``` 按照提示进行操作。 6. 创建WordPress数据库和用户。登录到MySQL shell: ``` sudo mysql -u root -p ``` 创建数据库和用户,并授予其所需的权限: ``` CREATE DATABASE wordpress; CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost'; FLUSH PRIVILEGES; EXIT; ``` 记得将 `'password'` 替换为您要为WordPress用户设置的密码。 7. 下载和配置WordPress。在您选择的网站根目录下,使用以下命令下载并解压WordPress: ``` cd /usr/share/nginx/html sudo wget -c http://wordpress.org/latest.tar.gz sudo tar -xzvf latest.tar.gz sudo mv wordpress/* . sudo rm -rf wordpress latest.tar.gz ``` 然后设置文件和目录的所有权: ``` sudo chown -R nginx:nginx /usr/share/nginx/html sudo chmod -R 755 /usr/share/nginx/html ``` 8. 配置WordPress。在浏览器中访问您的服务器的IP地址或域名,以启动WordPress安装向导。根据提示完成安装过程,使用先前创建的数据库和用户。 9. 完成安装后,您可以通过访问您的站点来查看WordPress。默认情况下,它将位于`http://your-server-ip`。 注意:这只是安装WordPress的基本步骤,请根据您的实际需求和环境进行适当的配置和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

老骥又出发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值