刘春华 發表於 2020-3-3 17:35:00

CentOS 8搭建LNMP + WordPress(二)

<p>CentOS 8近日推出了,其LNMP环境的搭建也与CentOS7有所不同。基于CentOS 8,我重写了前一篇文章《CentOS7搭建LNMP+WordPress一篇搞定》,得到了这一本文。</p>
<p>为了更好地阅读体验,我将本文分成了三个部分:</p>
<ol>
<li>名词解释与CentOS 8操作系统安装</li>
<li>网页服务器的安装与配置(Nginx + PHP)</li>
<li>数据库(MariaDB)与WordPress的安装与配置</li>
</ol>
<p>以下是本文的第二个部分</p>
<hr>
<h4 id="四安装nginx">四、安装Nginx</h4>
<h5 id="1-安装nginx">1. 安装Nginx</h5>
<p>首先更新已有软件</p>
<pre><code class="language-bash">sudo dnf -y update
</code></pre>
<p>如果您想安装较为稳定的Nginx,可以直接使用下面的命令:</p>
<pre><code class="language-bash">sudo dnf -y install nginx
</code></pre>
<p>如果您想安装较新的版本,可以使用下面的命令<sup>6</sup>:</p>
<p>首先创建一个源</p>
<pre><code class="language-bash">sudo vim /etc/yum.repos.d/nginx.repo
</code></pre>
<p>将下列内容加入刚才创建的文件中:</p>
<pre><code class="language-re">
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true


name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
</code></pre>
<p>使用命令安装:</p>
<pre><code class="language-bash">sudo dnf clean all
sudo dnf makecache
sudo dnf -y install nginx
</code></pre>
<h5 id="2-防火墙开放端口">2. 防火墙开放端口</h5>
<p>使用以下命令:</p>
<pre><code class="language-bash"># http协议需要开启80端口,https协议需要开启443端口。本文只使用http协议
sudo firewall-cmd --zone=public --add-port=[端口号]/tcp --permanent
# 例如:sudo firewall-cmd --zone=public --add-port=80/tcp --permanent

sudo firewall-cmd --reload        # 刷新防火墙
</code></pre>
<p>如果使用云服务器,须在其管理界面上放行对应端口。以阿里云为例,需要在云服务器的控制台的网络与安全组配置中添加安全组的入方向的安全组配置,放行对应端口:</p>
<h5 id="3-配置nginx">3. 配置Nginx</h5>
<p>使用vim打开主配置文件:</p>
<pre><code class="language-bash">sudo vim /etc/nginx/nginx.conf
</code></pre>
<p>如果你想对其进行详细修改,请参阅主配置文件的介绍。将“include /etc/nginx/conf.d/*.conf;”注释掉,换成我自己的配置文件,使其不适用默认的配置文件,即:</p>
<pre><code>#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/myNginx.conf;
</code></pre>
<p>接着输入下面的命令,打开配置文件:</p>
<pre><code class="language-bash">sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/myNginx.conf
sudo vim /etc/nginx/conf.d/myNginx.conf
</code></pre>
<p>您会看到:</p>
<pre><code class="language-conf">server {
        listen       80;
        server_namelocalhost;

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

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

        #error_page404            /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_indexindex.php;
        #    fastcgi_paramSCRIPT_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 {
    #    denyall;
    #}
}
</code></pre>
<p>配置文件的含义,可以参考default.conf配置文件含义。第2行是网页端口(默认80),第9、19行后面是网页文件的位置,你可以修改为自己想放的位置。在这里,我使用默认的位置,不作修改。以下的修改很重要:</p>
<ol>
<li>将第30-36行(含36行)前面表示注释的“#”号去掉(除非您不想使用PHP)。</li>
<li>将第34行的<code>/scripts</code>改为<code>$document_root</code>。</li>
<li>将31行的root路径改成与第9行的root路径一致。</li>
<li>在第10行前加入index.php(除非您不想使用PHP)</li>
<li>在第10行下插入<code>try_files $uri $uri/ /index.php?$args;</code>,以支持伪静态</li>
<li>(可选)第二行可以控制Nginx服务使用的端口。</li>
<li>(可选)将第9行修改为网页文件的位置(否则使用默认/usr/share/nginx/html)。注意,第31行也应随之更改。</li>
<li>如果您有域名,将第3行改为网站的域名。</li>
<li>将32行的“fastcgi_pass”后改为“unix:/run/php-fpm/www.sock;”</li>
</ol>
<p>修改后的文件如下所示:</p>
<pre><code class="language-conf">server {
    listen       80;
    server_namelocalhost;

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

    location / {
      root   /usr/share/nginx/html;
      indexindex.php index.html index.htm;
      try_files $uri $uri/ /index.php?$args;# 伪静态
    }

    #error_page404            /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         /usr/share/nginx/html;
      fastcgi_pass   unix:/run/php-fpm/www.sock;
      fastcgi_indexindex.php;
      fastcgi_paramSCRIPT_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 {
    #    denyall;
    #}
}
</code></pre>
<p>紧接着,启动Nginx:</p>
<pre><code class="language-bash">sudo systemctl enable nginx   # 将Nginx设置为开机自启
sudo systemctl start nginx      # 启动Nginx
systemctl status nginx          # 查看Nginx状态
</code></pre>
<p>如果出现绿色的active(running),就说明Nginx安装完成,并成功运行。</p>
<p>如果出现红色的failed,可能是配置文件出了问题,需要仔细检查。</p>
<center>
<p><img src="https://www.cycode.club/wp-content/uploads/2020/02/C8-12.png" alt="成功启动" loading="lazy"></p>
</center>
<h5 id="5-测试安装情况">5. 测试安装情况</h5>
<p>在浏览器中,输入服务器的IP地址(如果非默认端口,需要在ip后面加上冒号再加端口号。例如你设置了1234端口,IP是123.123.123.123,你可以这么访问:123.123.123.123:1234),观察网页。如果看到了以下图片,说明Nginx配置完全成功。</p>
<center>
<p><img src="https://www.cycode.club/wp-content/uploads/2020/02/C8-13.png" alt="成功访问" loading="lazy"></p>
</center>
<p>如果无法正常访问,但Nginx处于正常运行的状态,则有可能是由于Selinux中没有赋予Nginx相关的权限。可以使用<code>sestatus</code>这个命令检查Selinux的状态。如果它处于enforcing状态,则说明它正常开启;否则,则可能是其它的原因。另外,为了服务器的安全,在公网的服务器中,建议将Selinux开启并处于enforcing状态。注意,Selinux在阿里云提供的CentOS 8系统中是默认关闭的,需要编辑/etc/selinux/config文件,将“SELINUX=”设置为“enforcing”,然后重启服务器。</p>
<p>对于处于Enforcing状态的Selinux,我们可以采用使用如下的命令,根据Selinux对Nginx的拒绝日志,设置Nginx在Selinux中的权限:</p>
<pre><code class="language-bash">dnf install policycoreutils
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
rm mynginx.pp
</code></pre>
<h4 id="五安装php">五、安装PHP</h4>
<h5 id="1-安装">1. 安装</h5>
<p>如果您只是需要安装Wordpress7.2,那么直接使用CentOS8默认的PHP安装即可。<br>
如果您需要安装Wordpress,官方文档<sup>7</sup>,推荐安装PHP7.3及以上版本的PHP并安装一些依赖的模块:</p>
<pre><code class="language-bash">sudo dnf install epel-release
sudo dnf update epel-release
#sudo rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-8.rpm # 安装remi源
sudo dnf clean all
sudo dnf makecache
sudo dnf module enable php:7.3        # 使用PHP7.3版本,如使用更高版本,需安装remi源
# sudo dnf module enable php:remi-7.4        # 使用remi安装7.4版本

sudo dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml php-zip libsodium        # 必须安装组件

# php-pecl-imagick的安装需要remi源
sudo dnf install php-filter php-iconv php-simplexml php-xmlreader php-zlib php-pecl-imagick        # 推荐安装的组件
</code></pre>
<p>然后配置配置PHP:</p>
<pre><code class="language-bash">sudo systemctl enable php-fpm   # 设为开机启动
sudo systemctl start php-fpm    # 启动
systemctl status php-fpm      # 查看服务状态
</code></pre>
<h5 id="2-测试安装">2. 测试安装</h5>
<p>至此,配置完成接下来进行测试:</p>
<pre><code class="language-bash">cd [网页路径]   # 之前在myNginx.conf设置路径。若没修改该,则使用默认路径/usr/share/nginx/html。
vim test.php   # 使用vim创建test.php文件
</code></pre>
<p>输入:</p>
<pre><code class="language-php">&lt;?php
        phpinfo();
?&gt;
</code></pre>
<p>然后在浏览器中输入:[主机ip][:端口]/test.php(使用默认端口,可省略“:端口”的内容。若出现类似下图的界面,说明安装成功。</p>
<p><img src="https://www.cycode.club/wp-content/uploads/2020/02/C8-14.png" alt="phpinfo" loading="lazy"></p>
<p>至此,LNMP环境安装完毕,别忘了将test.php删掉哦。</p>
<h4 id="参考文献">参考文献</h4>
<p> Nginx: Linux packages http://nginx.org/en/linux_packages.html#RHEL-CentOS</p>


</div>
<div id="MySignature" role="contentinfo">
    本文版权,除注明引用的部分外,归作者所有。本文严禁商业用途的转载。非商业用途的转载需在网页明显处署上作者名称及原文链接。<br><br>
来源:https://www.cnblogs.com/fang-d/p/LNMP-CentOS8-2.html
頁: [1]
查看完整版本: CentOS 8搭建LNMP + WordPress(二)