热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

z在一般的运维中,都是需要安装nginxphpmariadb数据库。注意mariadb实际上是mysqlserver的社区版本。功能基本上一样。从ubuntu到cen

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 ServerCopyright (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-managerResolving 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.rpm2.安装PHP7的rpm源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm3.安装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 </usr/share/nginx/html/phpinfo.php
?>
EOF

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

[root&#64;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 &#61; /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&#39;s document root# concurs with nginx&#39;s one##location ~ /\.ht {# deny all;#}
}

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

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 &#61; /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$ {
#修改1root /usr/share/nginx/html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;
#修改2fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}# deny access to .htaccess files, if Apache&#39;s document root# concurs with nginx&#39;s one#location ~ /\.ht {deny all;}
}

测试&#xff1a;

安装wordpress

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

使用wget下载完&#xff0c;解压&#xff0c;并复制到/usr/share/nginx/html中。

[root&#64;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&#64;lamp ~]# tar zxf wordpress-5.4.2-zh_CN.tar.gz
[root&#64;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&#64;lamp ~]# mysql -uroot <> create database wordpress;
> EOF

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

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

安装成功访问页面

 

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

也就是说&#xff0c;如果nginx接收到访问PHP文件&#xff0c;nginx根本不读取php文件&#xff0c;而是直接把这个文件的路径和其他信息交个了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;}

 


推荐阅读
author-avatar
xiaol
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有