本文为在云服务器上配置LNMP环境并安装wordpress的教程。阿里云和腾讯云的centos7.4版本中均可使用。 阿里云需要额外配置安全组开启http(80)/https(443)/ftp(20/21)/SSH(22)/数据库(3306)等端口。 为了便于复制粘贴,文末也提供了2000字符一行的版本,可以在阿里云和腾讯云中按顺序批量执行命令。 注:文中出现的域名、FTP帐号、数据库帐号和密码、文件名应根据自己情况修改。
在建设一个网站之前我们还得考虑很多事情,比如说:这是一个什么样的网站,有没有盈利计划。因为特殊国情,在国内办理业务会麻烦的很多,远不是国外信用卡付款那么简单。不过这里并不讨论其他的问题,简单的说,如果你决定用云服务器来做WordPress网站需要准备以下条件: 0.计划做一个XX定位的网站 1.购买域名(然后备案需要半个月) 2.购买拥有独立IP的服务器(开始计算如何省钱) 3.远程连接服务器(直接阿里云实例列表里面远程链接) 4.熟悉在命令行下yum安装软件,或者vim编辑文档等常规操作。
在后面的演示中,我将频繁的使用sed进行批量编辑文档,因为一行一行的手动输入非常效率低,而且容易出错。 sed命令中,【-i】意味着会实际更改原文档,【-e】意味着多个命令按顺序执行。 下面的代码中我随便找了个文本文档,并把它删到只剩下5行,修改内容制作Nginx的repo,然后yum安装Nginx。
需要在notepad++中编辑,去掉换行符后才能用哦。
安装Nginx
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的默认用户和用户组
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/*;
创建info.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的默认设置和开启HugePage
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'
-e '826a\zend_extension=opcache.so'
-e '826a\opcache.enable=1'
-e '826a\opcache.enable_cli=1'
-e '826a\opcache.huge_code_pages=1'
-e '826a\opcache.file_cache=\/tmp\/opcache' /etc/opt/remi/php72/php.ini;
sed -i '32a\vm.nr_hugepages=200' /etc/sysctl.conf;
安装MariaDB
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;
初始化MariaDB并设置root用户的密码
mysql_secure_installation;
命令行登陆MariaDB,为WordPress创建一个数据库
mysql -u root -p;
create database wordpress;
\q
reboot
安装FTP
yum install vsftpd -y;
systemctl start vsftpd.service;
systemctl enable vsftpd.service;
sed -i '12c anonymous_enable=NO' /etc/vsftpd/vsftpd.conf;
useradd Sekia -s /sbin/nologin;
usermod -d /usr/share/nginx/html/ -g root Sekia;
创建FTP专用文件夹并赋予777权限
mkdir /usr/share/nginx/html/FTP;
chmod -R 777 /usr/share/nginx/html/FTP;
设置FTP用户的密码
passwd Sekia;
安装WordPress
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 );"
-e "34c define('ALLOW_UNFILTERED_UPLOADS', true);" /usr/share/nginx/html/wp-config.php;
上传SSL证书文件并移动到Nginx配置中指定的位置
unzip Nginx.zip;
cp /usr/share/nginx/html/FTP/1_acgmart.com_bundle.crt /etc/nginx/1_acgmart.com_bundle.crt;
cp /usr/share/nginx/html/FTP/2_acgmart.com.key /etc/nginx/2_acgmart.com.key;
设置网站根目录的所有者为nginx
chown -R nginx:root /usr/share/nginx/html/;
替换谷歌字体为国内服务商
sed -i "s/ajax.googleapis.com\/ajax\/libs/lib.baomitu.com/g" /usr/share/nginx/html/wp-includes/script-loader.php;
重启服务器
sed -i "s/ajax.googleapis.com\/ajax\/libs/lib.baomitu.com/g" /usr/share/nginx/html/wp-includes/script-loader.php;
在这个代码案例中创建了以下文件和帐号,大家根据自己的情况修改即可。 域名:acgmart.com;www.acgmart.com 证书文件:1_acgmart.com_bundle.crt;2_acgmart.com.key FTP帐号:Sekia 数据库用户帐号和密码:root/root 数据库:wordpress MariaDB占用的端口号:3306 主题的压缩文件:Avada.zip 将证书文件打包压缩后:Nginx.zip 安装成功后,浏览器访问域名就会自动跳转到语言选择界面。
Comments | 1 条评论
其实配置环境没有这么麻烦啦~有很多linux面板可以使用。
用面板会方便很多