CentOS7——搭建LNMP环境(WordPress案例)

编程之家收集整理的这篇文章主要介绍了CentOS7——搭建LNMP环境(WordPress案例)编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

CentOS7——搭建LNMP环境(wordpress案例)

LNMP组成介绍

LNMP(Linux-Nginx-MysqL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Nginx网络服务器,MysqL数据库PHP编程语言,所有组成产品均是免费开源软件,这四种软件组合到一起,成为一个免费、高效的网站服务系统。

LNMP工作原理

浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求。如果是静态文本直接返回,否则将脚本(PHP)通过接口传输协议(网关协议)PHP-FCGI(fast-cgi)传输给PHP-FPM(进程管理程序),然后PHP-FPM调用PHP解析器的其中一个进程php-cgi来解析PHP脚本信息。【PHP-FPM在启动时启动了多个php-cgi子进程,并发执行。】然后将解析后的脚本返回到PHP-FPM,PHP-FPM再通过fast-cgi的形式将脚本信息传送给Nginx。服务器再通过Http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。

wordpress介绍

wordpress 介绍 wordpress是一种使用PHP语言开发的博客平台,用户可以在支持PHPMysqL 数据库的服务器上架设自己的网站。 也可以把wordpress 当作一个内容管理系统(CMS)来使用。 wordpress一个免费的开源项目,在GNU通用公共许可证下授权发布。

构建LNMP+wordpress案例

环境配置

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

临时关闭SELINUX

setenforce 0

永久关闭SELINUX

echo SELINUX=disabled>/etc/selinux/config
echo SELINUXTYPE=targeted>>/etc/selinux/config

安装Nginx

增加 Nginx 官方源

cat << EOF > /etc/yum.repos.d/Nginx.repo
[Nginx-stable]
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

[Nginx-mainline]
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
EOF

EPEL 源中的 Nginx.service 由于 KILL 参数问题,启动后无法停止,不建议使用。

安装Nginx

yum install -y Nginx

备份Nginx配置文件

echo y|cp /etc/Nginx/Nginx.conf /etc/Nginx/Nginx.conf.default

修改 Nginx.conf

cat << EOF > /etc/Nginx/Nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://Nginx.org/en/docs/
#   * Official Russian Documentation: http://Nginx.org/ru/docs/

user Nginx;
worker_processes auto;
error_log  /var/log/Nginx/error.log warn;
pid        /var/run/Nginx.pid;

worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
}

http {
    include             /etc/Nginx/mime.types;
    default_type        application/octet-stream;

    log_format  main  '$host $server_port $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $request_time $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/Nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_names_hash_bucket_size 128;
    server_name_in_redirect off;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;

    client_header_timeout  3m;
    client_body_timeout    3m;
    client_max_body_size 50m;
    client_body_buffer_size 256k;
    send_timeout           3m;

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 60;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    proxy_buffer_size 256k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    proxy_temp_file_write_size 256k;
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 128m;
    #让代理服务端不要主动关闭客户端的连接,协助处理499返回代码问题
    proxy_ignore_client_abort on;

    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;

    index index.html index.htm index.PHP default.html default.htm default.PHP;

    # Load modular configuration files from the /etc/Nginx/conf.d directory.
    # See http://Nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/Nginx/conf.d/*.conf;
}
EOF

增加认Host

mkdir /etc/Nginx/conf.d

cat << EOF > /etc/Nginx/conf.d/default.conf
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/Nginx/html;

    # Load configuration files for the default server block.
    include /etc/Nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
EOF

启动Nginx

systemctl start Nginx

增加开机启动

systemctl enable Nginx

查看Nginx状态

# systemctl status NginxNginx.service - Nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/Nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-05-25 05:50:22 EDT; 7s ago
     Docs: http://Nginx.org/en/docs/
   CGroup: /system.slice/Nginx.service
           ├─1853 Nginx: master process /usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
           └─1854 Nginx: worker process

May 25 05:50:22 MysqL1 systemd[1]: Starting Nginx - high performance web server...
May 25 05:50:22 MysqL1 systemd[1]: Can't open PID file /var/run/Nginx.pid (yet?) after start: No such file or directory
May 25 05:50:22 MysqL1 systemd[1]: Started Nginx - high performance web server.

# ss -antpl|grep Nginx
LISTEN     0      128          *:80                       *:*                   users:(("Nginx",pid=1854,fd=6),("Nginx",pid=1853,fd=6))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("Nginx",pid=1854,fd=7),("Nginx",pid=1853,fd=7))

安装 MysqL

安装 MysqL

yum install -y mariadb-server

备份 my.cnf

cp /etc/my.cnf /etc/my.cnf.default

修改 my.cnf

cat << EOF > /etc/my.cnf
[MysqLd]
datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run MysqLd under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

max_allowed_packet=20M
max_heap_table_size = 100M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
tmp_table_size = 100M

# 查询缓存
#query_cache_limit=4M
#query_cache_type=on
#query_cache_size=2G

bind-address = 127.0.0.1
# 跳过主机名解析,比如localhost,foo.com之类,加速访问
skip-name-resolve

# sql执行日志
general_log=off
general_log_file=/var/log/mariadb/general.log

# sql查询日志
slow_query_log=off
slow_query_log_file=/var/log/mariadb/slowquery.log
long_query_time = 5

max_connections = 1000

# 兼容老MysqL代码,比如使用空字符串代替NULL插入数据
sql_mode = ""

[MysqLd_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
EOF

配置 MysqLdump 命令参数

sed -i '16 aquicknquote-namesnmax_allowed_packet = 100M' /etc/my.cnf.d/MysqL-clients.cnf

创建日志文件

touch /var/log/mariadb/general.log /var/log/mariadb/slowquery.log
chown MysqL:MysqL /var/log/mariadb/general.log /var/log/mariadb/slowquery.log

增加开机启动

systemctl enable mariadb

启动 MysqL 服务

systemctl start mariadb

修改root密码

MysqLadmin -uroot password "000000"

查看 MysqL 服务状态

# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-11-29 14:18:12 CST; 1h 7min ago
  Process: 16688 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 16653 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 16687 (MysqLd_safe)
   CGroup: /system.slice/mariadb.service
           ├─16687 /bin/sh /usr/bin/MysqLd_safe --basedir=/usr
           └─17043 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --plugin-dir=/usr/lib64/MysqL/plugin --log-error=/var/log/mariadb/mariadb.lo...

Nov 29 14:18:10 iZ6weebcmroarpx8rrxscrZ systemd[1]: Starting MariaDB database server...
Nov 29 14:18:10 iZ6weebcmroarpx8rrxscrZ mariadb-prepare-db-dir[16653]: Database MariaDB is probably initialized in /var/lib/MysqL already, nothing is done.
Nov 29 14:18:11 iZ6weebcmroarpx8rrxscrZ MysqLd_safe[16687]: 191129 14:18:11 MysqLd_safe Logging to '/var/log/mariadb/mariadb.log'.
Nov 29 14:18:11 iZ6weebcmroarpx8rrxscrZ MysqLd_safe[16687]: 191129 14:18:11 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL
Nov 29 14:18:12 iZ6weebcmroarpx8rrxscrZ systemd[1]: Started MariaDB database server.

# ss -antpl|grep MysqL
LISTEN     0      50     127.0.0.1:3306                     *:*                   users:(("MysqLd",pid=17043,fd=14))

安装 PHP7

增加SCL源

yum install -y centos-release-scl

安装PHP7.2

yum install -y rh-PHP72 
    rh-PHP72-PHP  
    rh-PHP72-PHP-bcmath 
    rh-PHP72-PHP-fpm 
    rh-PHP72-PHP-gd 
    rh-PHP72-PHP-intl 
    rh-PHP72-PHP-mbstring 
    rh-PHP72-PHP-MysqLnd 
    rh-PHP72-PHP-opcache 
    rh-PHP72-PHP-pdo 
    rh-PHP72-PHP-pecl-apcu 
    rh-PHP72-PHP-xmlrpc 
    rh-PHP72-PHP-devel

进入 rh-PHP72 环境

scl enable rh-PHP72 bash

确认PHP状态

# PHP -v
PHP 7.2.24 (cli) (built: Nov  4 2019 10:23:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies

备份PHP.ini

cp /etc/opt/rh/rh-PHP72/PHP.ini /etc/opt/rh/rh-PHP72/PHP.ini.default

修改PHP.ini

# 启用 '<? ... ?>' 代码风格
sed -i '197s/short_open_tag = Off/short_open_tag = On/' /etc/opt/rh/rh-PHP72/PHP.ini

# 禁止一些危险性高的函数
sed -i '314s/disable_functions =/disable_functions = system,exec,shell_exec,passthru,set_time_limit,ini_alter,dl,openlog,syslog,readlink,symlink,link,leak,popen,escapeshellcmd,virtual,socket_create,mail,eval/' /etc/opt/rh/rh-PHP72/PHP.ini

# 配置中国时区
sed -i '902s#;date.timezone =#date.timezone = Asia/Shanghai#' /etc/opt/rh/rh-PHP72/PHP.ini

增加开机启动

systemctl enable rh-PHP72-PHP-fpm

启动 PHP-FPM 服务

systemctl start rh-PHP72-PHP-fpm

查看 PHP-FPM 服务状态

# systemctl status rh-PHP72-PHP-fpm
● rh-PHP72-PHP-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/rh-PHP72-PHP-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-11-29 13:36:03 CST; 1h 56min ago
 Main PID: 15360 (PHP-fpm)
   Status: "Processes active: 0, idle: 6, Requests: 56, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/rh-PHP72-PHP-fpm.service
           ├─15360 PHP-fpm: master process (/etc/opt/rh/rh-PHP72/PHP-fpm.conf)
           ├─15361 PHP-fpm: pool www
           ├─15362 PHP-fpm: pool www
           ├─15363 PHP-fpm: pool www
           ├─15364 PHP-fpm: pool www
           ├─15365 PHP-fpm: pool www
           └─17211 PHP-fpm: pool www

Nov 29 13:36:03 iZ6weebcmroarpx8rrxscrZ systemd[1]: Starting The PHP FastCGI Process Manager...
Nov 29 13:36:03 iZ6weebcmroarpx8rrxscrZ systemd[1]: Started The PHP FastCGI Process Manager.

# ss -antpl|grep PHP-fpm
LISTEN     0      128    127.0.0.1:9000                     *:*                   users:(("PHP-fpm",pid=17211,fd=9),("PHP-fpm",pid=15365,fd=9),("PHP-fpm",pid=15364,fd=9),("PHP-fpm",pid=15363,fd=9),("PHP-fpm",pid=15362,fd=9),("PHP-fpm",pid=15361,fd=9),("PHP-fpm",pid=15360,fd=7))

LNMP 环境测试

增加数据库

MysqL -uroot -p000000 -e 'create database wordpress;grant all privileges on wordpress.* to wordpress@"localhost" identified by "wordpress_password";flush privileges;'

增加Nginx Host设置

cat << EOF > /etc/Nginx/conf.d/wordpress.conf
server{
    listen       8080;

    server_name  wordpress.com;
    root         /data/web/wordpress.com;
    error_log /var/log/Nginx/wordpress.com_error.log;
    access_log /var/log/Nginx/wordpress.com_access.log  main;

    location / {
        try_files $uri /index.PHP$is_args$query_string;
    }

    location ~ .PHP$ {
        root           /data/web/wordpress.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
EOF

# 重载Nginx配置
Nginx -t && Nginx -s reload

准备 wordpress

mkdir -p /data/web/wordpress.com

# 使用 -O 参数指定保存文件名,会强制覆盖已经存在的文件
yum -y install wget
wget https://wordpress.org/latest.tar.gz -O wordpress.tar.gz
tar xf wordpress.tar.gz

mv wordpress/* /data/web/wordpress.com
rm -rf wordpress
chown -R apache:Nginx /data/web/wordpress.com
chmod -R 755 /data/web/wordpress.com

最后,访问http://服务器地址:8080 进入博客安装界面

如果无法访问网站地址:

1.请确保服务器的防火墙和SELINUX为关闭状态,详细操作请看基础环境配置的关闭防火墙与SELINUX操作。

2.测试主机与服务器的通信状态是否正常

1.选择语言模式为中文

LNMP_01

2.填写数据库信息

LNMP_02

3.填写网站信息

LNMP_03

4.访问http://服务器地址:8080 就可以看到博客已经搭起来了

LNMP_04

写在最后

如果文档对你有帮助的话,请点击一下 推荐按钮 ,你的点击是我的最大动力。

我是键盘侠,现实中我唯唯诺诺,网络上我重拳出击,关注我,持续更新Linux干货教程。

总结

以上是编程之家为你收集整理的CentOS7——搭建LNMP环境(WordPress案例)全部内容,希望文章能够帮你解决CentOS7——搭建LNMP环境(WordPress案例)所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢寻找一群志同道合、互帮互助的学习伙伴,可以点击下方链接加入:
编程之家官方1群
编程之家官方2群
编程之家官方3群
编程之家官方4群

相关文章

猜你在找的CentOS相关文章

CentOS 初体验二十一:redis常用命令:String
CentOS6.3安装配置Opennebula3.8.3
一文看懂虚拟机Centos 7的配置(搭建Hadoop开发环境)
CentOS安装intellijIDEA步骤
Centos下,一键执行sh文件。自动安装jdk11
centos7下安装并配置supervisor守护程序
windows10下linux centOS 7.6网络配置(针对 NAT模式)
macOs 虚拟机安装CentOs及环境搭建
微信公众号搜索 “ 程序精选 ” ,选择关注!
微信公众号搜 "程序精选"关注