说明
本篇文章基于LNMP(linux+nginx+mysql+php)搭建,其中linux为CentOS7.9;nginx版本为1.16.1;MySQL版本为5.7.34;PHP为7.0.33。
操作步骤
Nginx安装
nginx采用源码包编译安装的方式,安装包下载地址:
在编译之前安装所需依赖:
yum install -y openssl-devel pcre-devel zlib-devel gcc-c++
编译安装nginx:
tar -zxf nginx-1.16.1.tar.gz && cd nginx-1.16.1
./configure --prefix=/opt/public/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
echo $?
当echo $?输出0时表示我们安装成功,安装目录为prefix中定义的路径。
为了保证我们站点的安全性,我们尽量不要用root用户去启动任何的应用程序,创建一个普通用户,并使用它去运行程序。
useradd appadmin
chown -R appadmin:appadmin /opt/public/nginx
MySQL安装
通过yum方式进行安装:
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
service mysqld start
grep "password" /var/log/mysqld.log
mysql -uroot -p #输入上一步获取到的临时密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password';
exit #推出mysql控制台
#尝试用新密码登录
mysql -uroot -p'your password'
修改完密码后,输入exit退出MySQL控制台,然后尝试用新密码登录。确认没问题后将配置免密登录到控制台,编辑/etc/my.cnf文件,在末行插入:
[client]
user=root
password=your new password
配置完成后在控制台输入mysql 查看是否无需输入密码就能登录MySQL控制台。采用yum安装的MySQL无需使用appadmin用户去运行,因为它会自动帮我们创建一个mysql用户,并使用该用户运行MySQL进程。
安装PHP
由于我这里下载的wordpress版本较新,不支持PHP 5.6.20以下的版本,因为安装了PHP7。通过yum安装:
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php70w php70w-mysql php70w-fpm
安装完成后,我们使用上面的appadmin去启动php-fpm(原先是apache用户),使用同一用户去运行nginx和fpm,可以有效减少因为文件权限而产生的错误。编辑/etc/php-fpm.d/www.conf文件
user = appadmin
group = appadmin
启动php-fpm:
#启动php-fpm
service php-fpm start
systemctl enable php-fpm
#检测fpm启动是否正常
netstat -lntp |grep 9000 && ps -ef|grep php-fpm
配置WordPress
下载wordpress,下载地址:https://wordpress.org/latest.tar.gz,下载后将文件owner改为appadmin用户:
chown appadmin:appadmin wordpress-5.7.2.tar.gz
#将文件移到到nginx html目录
mv wordpress-5.7.2.tar.gz /opt/public/nginx/html
接下来的操作我们都使用appadmin这个用户进行操作,执行su - appadmin 切换用户。
cd /opt/public/nginx/html
tar -zxf wordpress-5.7.2.tar.gz
编辑nginx配置文件/opt/public/nginx/conf/nginx.conf
NGINX works perfectly well with a wide variety of applications, NGINX’s configuration language is very powerful and straightforward if one is familiar with it, but often people coming from other servers are not sure how things work in NGINX and just copy and paste whatever they see from a blog that seems to fill their needs. Everyone, especially people new to NGINX, should check out the nginx.org documentation for an overview of how things work and should be done in NGINX.
来自nginx官方的吐槽:不知道NGINX的工作原理,只是复制和粘贴他们在博客上看到的似乎能满足他们需要的东西,nginx官方推荐的配置来自:https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
wordpress官方推荐配置:https://wordpress.org/support/article/nginx/
这里我贴出我的配置:
user appadmin;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream wordpressphp {
#server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
listen 80;
#server_name localhost;
root /opt/public/nginx/html/wordpress;
index index.php;
#access_log logs/host.access.log main;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass wordpressphp;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
}
配置wordpress与MySQL
cd /opt/public/nginx/html/wordpress
cp wp-config-sample.php wp-config.php
编辑wp-config.php文件
/** MySQL database password */
define( 'DB_PASSWORD', 'your password' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
启动nginx,在浏览器访问你的wordpress站点
/opt/public/nginx/sbin/nginx
在浏览器访问wordpress需要防火墙开通80端口,如果出现workpress配置页面,则表示安装无误,根据页面提示修改相关内容就行啦~