如何在centos服务器上搭建WordPress
Admin 2023-05-06 群英技术资讯 1499 次浏览
今天小编跟大家讲解下有关“如何在centos服务器上搭建WordPress”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。LEMP 代表的是(L:Linux OS,E:Nginx,M:MySQL/MariaDB, P:PHP),与之对应的是 LAMP(唯一不同的是 A,代表 Apache 网络服务器),由于Nginx轻量、高效的特性,LEMP 在近来的 Web 服务中的出镜率越来越高。
为了追随潮流,在之前 LAMP 搭建 WordPress 的基础上,我又尝试了使用 Nginx 作为 WordPress 的网络服务器,尽管遇到了各种各样的问题,但是取得了不错的效果。由于 Nginx 的配置比较复杂,于是有很多需要注意的地方。

Nginx 是十分轻量级的 HTTP 服务器,是一个高性能的 HTTP 和反向代理服务器,Nginx 以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡。其拥有匹敌 Lighttpd 的性能,同时还没有 Lighttpd 的内存泄漏问题。
Apache 是世界使用排名第一的 Web 服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一。但是对于那些需要更强大的 Web 应用服务器(比如大小、可定制、响应速度、可扩展性等方面)的人而言,Apache 明显不符合他们的要求。
Lighttpd 是一个具有非常低的内存开销,CPU 占用率低,效能好,以及丰富的模块等特点。Lighttpd 是众多 开源的轻量级的网络服务器中较为优秀的一个。支持 FastCGI、 CGI、 Auth、 输出压缩、URL 重写、Alias 等重要功能。
如果考虑网站的综合状况的话,比较理想的做法是用 Nginx 做高并发、缓存、代理前端内容,而使用 Apache 处理后台动态内容。不过如果只是驱动 WordPress,单独使用 Nginx 就够了。
安装过程和 LAMP 差不多,只是几个地方稍微不同。
# 安装Nginxsudo apt install nginx# 检查Nginx运行状况sudo systemctl status nginx.service# 安装PHP7和PHPFastCGI管理器PHP-FPM sudo apt install php7.0 php7.0-fpm
# 安装MariaDB sudo apt install mariadb-server mariadb-client php7.0-mysql # 重启PHP-FPM服务以便使用MySQL模块与数据库通信 sudo systemctl restart php7.0-fpm.service
为了安全加固 MariaDB,运行来自 Ubuntu 软件仓库中的二进制包提供的安全脚本,这会询问你设置一个 root 密码,移除匿名用户,禁用 root 用户远程登录,移除测试数据库等。
# 使用安全脚本 sudo mysql_secure_installation # 配置MariaDB以便普通用户能够不使用root权限来访问数据库 sudo mysql MariaDB> use mysql; MariaDB> update user set plugin='' where User='root'; MariaDB> flush privileges; MariaDB> exit # 然后使用如下命令执行数据库命令 mysql -u root -p -e 'show databases'
还有一些 WordPress 会用到的模块,比如 mcrypt、mbstring,安装完后需要重启 PHP-FPM。
# 其他模块 sudo apt install php7.0-mcrypt php7.0-mbstring
到这里 LEMP 的安装工作就已经基本搞定了,接下来是最重要的配置环节。
首先是主配置文件 /etc/nginx/nginx.conf:
# Set user and group.
user www-data www-data;
# Usually equal to number of CPUs you have.
worker_processes 1;
# Global error log [ debug | info | notice | warn | error | crit ].
error_log /var/log/nginx/error.log warn;
# Pid file
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
### Basic Settings ###
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
client_max_body_size 15m; # Limit the max size of plugin in WordPress.
### SSL Settings ###
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
### Logging Settings ###
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
### Gzip Settings ###
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
### FastCGI Settings ###
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
### Virtual Host Configs ###
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
然后是 WordPress 的配置文件 /etc/site-enabled/wordpress.conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/wordpress;
index index.html index.php;
server_name www.infiniture.cn;
access_log /var/log/nginx/infiniture.com.access.log;
error_log /var/log/nginx/infiniture.com.error.log;
# Managed by Certbot
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/www.infiniture.cn/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.infiniture.cn/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
# Add rewrite support for wordpress
location / {
try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
# Cache strategy
set $no_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $no_cache 1;
}
if ($query_string != "") {
set $no_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $no_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $no_cache 1;
}
location ~ /wp-admin {
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# Making the updates in WordPress real time.
fastcgi_buffering off;
add_header X-Accel-Buffering "no";
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
add_header X-Cache "$upstream_cache_status From $host";
}
# Purge cache(Nginx Helper Purge Method: Using a Get)
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 182.254.246.42;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
}
以上配置中,需要说明的几个:
client_max_body_size 15m; 因为 WordPress 里很多插件的体积比较大,如果不设置这个值的大小,会导致插件或者主题安装失败。fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G; 这个包括下面的所有内容都是为了使 Nginx 缓存动态文件,参数对应的是缓存位置,缓存级别,缓存标识及大小,有效期,最大容量。.htaccess 文件,因此无法使用 WordPress 自带的地址重写功能,需要自己添加相应的功能。# Add rewrite support for wordpress
location / {
try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
fastcgi_buffering off; 这个选项默认是开启的,它会把 FastCGI 返回的内容缓存起来,直到缓存空间满了之后一并输出,这会导致 WordPress 下更新时不会实时显示,而是更新完成后全部一起显示。关掉这个选项即可解决这个问题。fastcgi_connect_timeout 600; fastcgi_send_timeout 600; fastcgi_read_timeout 600;
# Purge cache(Nginx Helper Purge Method: Using a Get)location ~ /purge(/.*) { allow 127.0.0.1; allow xxx.xxx.xxx.xxx; # 你的服务器地址 deny all; fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";}这样就在 LEMP 平台上搭建好了 WordPress 站点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
linux中有libpcap库;libpcap库是一个网络数据包捕获函数库,提供了一个API,用于直接从Unix衍生操作系统的数据链路层捕获数据包,libpcap库被流行的数据包捕获应用程序使用,使它们能够在几乎任何风格的Unix上运行。
判断方法:1、利用“fdisk -l”命令,若显示结果中的“Disk label type”的值为dos则分区是mbr,若该结果的值为gpt则分区是gpt;2、利用“parted -l”命令,若显示结果中的“Partition Table”的值为msdos则分区是mbr,若该结果的值为gpt则分区是gpt。
linux中删除换行符有几种方法,怎样做?有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
在linux中,h文件是系统中的头文件,一般用于存放一些重复使用的代码,如函数声明、变量声明、常数声明、宏的定义等等;头文件是一种文本文件,使用文本编辑器编辑完成后,以扩展名“.h”保存即可。
新手朋友们在不了解Linux命令的情况下,很容易在移动或者重命名文件时将 mv 写为 rm,这样就会造成文件被删。那Linux rm命令误删文件需要怎么复原?下面请看小编整理的资料。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008