#介绍

LNMP = linux + nginx + mysql +php
LAMP = linux + apache + mysql + php

开源最多的软件 CMS = content management system.
wordpress,joomla,mantis ,phpcms,dede

ERP,CRM,HR,HIS,OA
在一般的运维中,都是需要安装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;
}

作者:严锋  创建时间:2023-09-10 16:23
最后编辑:严锋  更新时间:2025-05-09 15:48