阿里云服务器Centos7.4搭建LNMP环境 WordPress安装

Posted Acgmart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云服务器Centos7.4搭建LNMP环境 WordPress安装相关的知识,希望对你有一定的参考价值。

本文为在云服务器上配置LNMP环境并安装wordpress的教程。阿里云和腾讯云的centos7.4版本中均可使用。
阿里云需要额外配置安全组开启http(80)/https(443)/ftp(20/21)/SSH(22)/数据库(3306)等端口。

安装nginx,官方最新的稳定生产版。截止2018-04-17的版本号为1.14.0。

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/nginx.repo;sed -i -e '6,$d' -e '1c [nginx]' -e '2c name=nginx repo' -e '3c baseurl=http://nginx.org/packages/centos/7/$basearch/' -e '4c gpgcheck=0' -e '5c enabled=1' /etc/yum.repos.d/nginx.repo;yum install nginx -y;systemctl enable nginx;

Nginx设置,在配置/etc/nginx/conf.d/default.conf文件时,要同时考虑到以下功能:
1:用户输入【自己的域名.com】时自动补齐前面的【www.】(可选)
2:配置在腾讯云可以免费申请的亚洲诚信1年免费SSL证书。(可选)
3:开启php的重定向,避免在wordpress中设置了自定义固定链接后无法找到文章。(建议)
4:添加index.php为web服务器的默认目录文件
5:让nginx支持php程序
需要替换代码中的站点网址为自己的网址/IP,如果有不需要的功能则需要删除对应代码。

sed -i -e '10c index index.php index.html index.htm;' -e '3a\return 301 https://www.acgmart.com$request_uri;}' -e '3a\server {' -e '3a\listen 80;' -e '3a\server_name www.acgmart.com;' -e '3a\listen 443 ssl;' -e '3a\ssl_certificate /etc/nginx/1_www.acgmart.com_bundle.crt;' -e '3a\ssl_certificate_key /etc/nginx/2_www.acgmart.com.key;' -e '3a\ssl_session_timeout 5m;' -e '3a\ssl_protocols TLSv1 TLSv1.1 TLSv1.2;' -e '3a\ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;' -e '3a\ssl_prefer_server_ciphers on;' -e '9a\if (-f $request_filename/index.html){rewrite (.*) $1/index.html break;}' -e '9a\if (-f $request_filename/index.php){rewrite (.*) $1/index.php;}' -e '9a\if (!-f $request_filename){rewrite (.*) /index.php;}' -e '12a\location ~ \.php$ {' -e '12a\root /usr/share/nginx/html;' -e '12a\fastcgi_pass 127.0.0.1:9000;' -e '12a\fastcgi_index index.php;' -e '12a\include fastcgi_params;' -e '12a\fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}' /etc/nginx/conf.d/default.conf;

这里也提供一份只有需求3、4、5的Nginx设置。

sed -i -e '10c index index.php index.html index.htm;' -e '9a\if (-f $request_filename/index.html){rewrite (.*) $1/index.html break;}' -e '9a\if (-f $request_filename/index.php){rewrite (.*) $1/index.php;}' -e '9a\if (!-f $request_filename){rewrite (.*) /index.php;}' -e '12a\location ~ \.php$ {' -e '12a\root /usr/share/nginx/html;' -e '12a\fastcgi_pass 127.0.0.1:9000;' -e '12a\fastcgi_index index.php;' -e '12a\include fastcgi_params;' -e '12a\fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}' /etc/nginx/conf.d/default.conf;

安装php的最新版本php7.2及配套软件。

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y;yum install php72 -y;yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache php72-php-pecl-zip -y;systemctl enable php72-php-fpm.service;

修改php的配置中的用户名为nginx。

sed -i -e '24c user = nginx' -e '26c group = nginx' /etc/opt/remi/php72/php-fpm.d/www.conf;

rm -rf /usr/share/nginx/html/*;

创建php测试程序确认nginx和php是否正常安装。

cp /etc/yum.repos.d/CentOS-Base.repo /usr/share/nginx/html/info.php;sed -i -e '4,$d' -e '1c <?php' -e '2c phpinfo();' -e '3c ?>' /usr/share/nginx/html/info.php;

配置php以满足一些企业级别网站的属性需求,如最长处理时间、最大链接数、最大内存限制、最大上传文件大小、最大post文件大小。

sed -i -e '383c max_execution_time = 300' -e '400c max_input_vars = 5000' -e '404c memory_limit = 1000M' -e '672c post_max_size = 300M' -e '825c upload_max_filesize = 200M' /etc/opt/remi/php72/php.ini;

安装ftp,同时关闭匿名登陆、添加ftp专用帐号ftpuser。

yum install vsftpd -y;systemctl start vsftpd.service;systemctl enable vsftpd.service;sed -i '12c anonymous_enable=NO' /etc/vsftpd/vsftpd.conf;useradd ftpuser -s /sbin/nologin;usermod -d /usr/share/nginx/html/ -g root ftpuser;

安装最新版本的MariaDB 10.3。也可以使用其他数据库如PostgreSQL,只是要额外处理和WordPress的兼容问题。

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/MariaDB.repo;sed -i -e '6,$d' -e '1c [mariadb]' -e '2c name = MariaDB' -e '3c baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/' -e '4c gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB' -e '5c gpgcheck=1' /etc/yum.repos.d/MariaDB.repo;yum install MariaDB-server -y;systemctl start mariadb;systemctl enable mariadb;

安装.zip文件的解压工具,腾讯云的centos自带这个软件。

yum install unzip -y;

创建一个FTP专用文件夹,并给与777权限。在Linux环境使用WordPress等程序时会频繁遇到文件读写权限方面的问题。

mkdir /usr/share/nginx/html/FTP;chmod -R 777 /usr/share/nginx/html/FTP;

到此LNMP环境所需的软件和配置已经完成了,但是因为找不到证书文件Nginx还无法正常启动。
全部(只有Nginx需求3、4、5)的代码整合起来为以下内容,因为阿里云限制一次最多输入2000字符所以需要分2次处理。

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/nginx.repo;sed -i -e '6,$d' -e '1c [nginx]' -e '2c name=nginx repo' -e '3c baseurl=http://nginx.org/packages/centos/7/$basearch/' -e '4c gpgcheck=0' -e '5c enabled=1' /etc/yum.repos.d/nginx.repo;yum install nginx -y;systemctl enable nginx;sed -i -e '10c index index.php index.html index.htm;' -e '9a\if (-f $request_filename/index.html){rewrite (.*) $1/index.html break;}' -e '9a\if (-f $request_filename/index.php){rewrite (.*) $1/index.php;}' -e '9a\if (!-f $request_filename){rewrite (.*) /index.php;}' -e '12a\location ~ \.php$ {' -e '12a\root /usr/share/nginx/html;' -e '12a\fastcgi_pass 127.0.0.1:9000;' -e '12a\fastcgi_index index.php;' -e '12a\include fastcgi_params;' -e '12a\fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}' /etc/nginx/conf.d/default.conf;yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y;yum install php72 -y;yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache php72-php-pecl-zip -y;systemctl enable php72-php-fpm.service;sed -i -e '24c user = nginx' -e '26c group = nginx' /etc/opt/remi/php72/php-fpm.d/www.conf;rm -rf /usr/share/nginx/html/*;cp /etc/yum.repos.d/CentOS-Base.repo /usr/share/nginx/html/info.php;sed -i -e '4,$d' -e '1c <?php' -e '2c phpinfo();' -e '3c ?>' /usr/share/nginx/html/info.php;sed -i -e '383c max_execution_time = 300' -e '400c max_input_vars = 5000' -e '404c memory_limit = 1000M' -e '672c post_max_size = 300M' -e '825c upload_max_filesize = 200M' /etc/opt/remi/php72/php.ini;yum install vsftpd -y;systemctl start vsftpd.service;systemctl enable vsftpd.service;sed -i '12c anonymous_enable=NO' /etc/vsftpd/vsftpd.conf;useradd ftpuser -s /sbin/nologin;usermod -d /usr/share/nginx/html/ -g root ftpuser;cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/MariaDB.repo;sed -i -e '6,$d' -e '1c [mariadb]' -e '2c name = MariaDB' -e '3c baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/' -e '4c gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB' -e '5c gpgcheck=1' /etc/yum.repos.d/MariaDB.repo;yum install MariaDB-server -y;systemctl start mariadb;systemctl enable mariadb;yum install unzip -y;mkdir /usr/share/nginx/html/FTP;chmod -R 777 /usr/share/nginx/html/FTP;
cd;

设置FTP用户ftpuser的密码,输入2次密码。

passwd ftpuser;

初始化MariaDB,根据提示操作,设置初始密码时需要输入2次密码。

mysql_secure_installation;

root用户登陆MariaDB客户端去搞事情。

mysql -u root -p;

创建WordPress用的数据库wordpress。

create database wordpress;

退出MariaDB客户端,搞完事就跑。

\q

重启服务器,或者依次重启各个服务systemctl restart xxx.service。

reboot

cd /usr/share/nginx/html/FTP;unzip wordpress-4.9.5.zip;cp -R wordpress/* /usr/share/nginx/html/;cp /usr/share/nginx/html/wp-config-sample.php /usr/share/nginx/html/wp-config.php;sed -i -e "23c define('DB_NAME', 'wordpress');" -e "26c define('DB_USER', 'root');" -e "29c define('DB_PASSWORD', '你的数据库登陆密码');" -e "32c define('DB_HOST', 'localhost:3306');" -e "33c define( 'WPMS_ON', true );" -e "34c define( 'WPMS_SMTP_PASS', '你的邮箱密码' );" /usr/share/nginx/html/wp-config.php;unzip Avada.zip;cp -R /usr/share/nginx/html/FTP/Avada /usr/share/nginx/html/wp-content/themes/Avada;unzip Nginx.zip;cp /usr/share/nginx/html/FTP/Nginx/1_www.acgmart.com_bundle.crt /etc/nginx/1_www.acgmart.com_bundle.crt;cp /usr/share/nginx/html/FTP/Nginx/2_www.acgmart.com.key /etc/nginx/2_www.acgmart.com.key;chown -R nginx:root /usr/share/nginx/html/;rm -rf /usr/share/nginx/html/FTP/*;
cd;

只是尝试安装WordPress可以使用以下代码,数据库用户root密码root,没设置SMTP密码。
如果出现主页提示【Error establishing a database connection】可能是数据库用户名或密码错误,可以再次初始化MariaDB。

cd /usr/share/nginx/html/FTP;wget http://wordpress.org/latest.zip;unzip latest.zip;cp -R wordpress/* /usr/share/nginx/html/;cp /usr/share/nginx/html/wp-config-sample.php /usr/share/nginx/html/wp-config.php;sed -i -e "23c define('DB_NAME', 'wordpress');" -e "26c define('DB_USER', 'root');" -e "29c define('DB_PASSWORD', 'root');" -e "32c define('DB_HOST', 'localhost:3306');" -e "33c define( 'WPMS_ON', true );" /usr/share/nginx/html/wp-config.php;chown -R nginx:root /usr/share/nginx/html/;rm -rf /usr/share/nginx/html/FTP/*;
cd;

到这里WordPress也安装好了,浏览器中输入域名或者IP或者http://你的服务器外网IP/wp-admin/install.php可以开始安装WordPress。
也可以输入http://你的服务器外网IP/info.php,查看php信息。


以上是关于阿里云服务器Centos7.4搭建LNMP环境 WordPress安装的主要内容,如果未能解决你的问题,请参考以下文章

centos7.4 搭建lnmp

从零开始用刚买的阿里云实例搭建lnmp环境(非集成包)

PHP--腾讯云服务器上部署LNMP环境

阿里云CentOS7.4上搭建FTP服务器

阿里云服务器安装配置流程

阿里云CentOS7.4上搭建FTP服务器