Welcome(〃'▽'〃)!

这里可能没有什么厉害的技术帖,但是会有一些实用的小教程

WordPress建站:VPS+LAMP搭建新手教程

本文记录了博主搭建wordpress网站的一些过程,环境为VPS+LAMP,适合对vps和linux操作系统的命令有一点点了解,但不会建站的人。

1 前期准备

购买好VPS(博主用过Vultr,DigitalOcean,Linode等都不错),这里使用的系统为Centos7,购买好域名(万网,NameCheap等)

2 SSH连接远程VPS

使用SSH软件(比如Putty)连接VPS,登录。

为防止SSH登录一段时间后自动断开,可修改sshd_config文件以保持SSH长时间在线。

# vi /etc/ssh/sshd_config

将如下两行代码前的 # 去掉,然后做相应修改:

#PermitUserEnvironment no
#Compression delayed
ClientAliveInterval 30
ClientAliveCountMax 5
#ShowPatchLevel no
#UseDNS yes

保存,重启SSH即可生效:

# service sshd restart

3 搭建LAMP环境

LAMP指的是Linux(操作系统)、Apache(HTTP服务器),MySQL(数据库软件) 和PHP(有时也是指Perl或Python)的第一个字母,主要用来建立web应用平台,现在我们来搭建LAMP环境。

《WordPress建站:VPS+LAMP搭建新手教程》

博主使用的是LNMP一键安装包,具体可参看这里:https://lnmp.org/install.html

首先创建screen会话:

# screen -S lamp

安装LNMP1.5版本:

# wget -c http://soft.vpser.net/lnmp/lnmp1.5-full.tar.gz && tar -zxf lnmp1.5-full.tar.gz && cd lnmp1.5-full && ./install.sh lamp

接下来进入安装过程,一般选择默认即可:

+------------------------------------------------------------------------+
| LNMP V1.5 for CentOS Linux Server, Written by Licess |
+------------------------------------------------------------------------+
| A tool to auto-compile & install LNMP/LNMPA/LAMP on Linux |
+------------------------------------------------------------------------+
| For more information please visit https://lnmp.org |
+------------------------------------------------------------------------+
You have 10 options for your DataBase install.
1: Install MySQL 5.1.73
2: Install MySQL 5.5.60 (Default)
3: Install MySQL 5.6.40
4: Install MySQL 5.7.22
5: Install MySQL 8.0.11
6: Install MariaDB 5.5.60
7: Install MariaDB 10.0.35
8: Install MariaDB 10.1.33
9: Install MariaDB 10.2.14
0: DO NOT Install MySQL/MariaDB
Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9 or 0): 2
You will install MySQL 5.5.60
===========================
Please setup root password of MySQL.
Please enter: mysqlpasswd       //填写数据库的root密码,务必牢记
===========================
Do you want to enable or disable the InnoDB Storage Engine?
Default enable,Enter your choice [Y/n]: n
You will disable the InnoDB Storage Engine!
===========================
You have 8 options for your PHP install.
1: Install PHP 5.2.17
2: Install PHP 5.3.29
3: Install PHP 5.4.45
4: Install PHP 5.5.38
5: Install PHP 5.6.36 (Default)
6: Install PHP 7.0.30
7: Install PHP 7.1.18
8: Install PHP 7.2.6
Enter your choice (1, 2, 3, 4, 5, 6, 7 or 8): 5
You will install PHP 5.6.36
===========================
You have 3 options for your Memory Allocator install.
1: Don't install Memory Allocator. (Default)
2: Install Jemalloc
3: Install TCMalloc
Enter your choice (1, 2 or 3): 1
You will install not install Memory Allocator.
===========================
Please enter Administrator Email Address: chenshenwei1998@gmail.com    //填写管理员邮箱
===========================
Server Administrator Email: chenshenwei1998@gmail.com     ////填写管理员邮箱
===========================
You have 2 options for your Apache install.
1: Install Apache 2.2.34
2: Install Apache 2.4.33 (Default)
Enter your choice (1 or 2): 2
You will install Apache 2.4.33

Press any key to install...or Press Ctrl+c to cancel

按回车键确认安装,

安装时间大约在半小时左右,可以先去喝杯茶。

安装成功后的界面如下图所示

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
Install lnmp takes 31 minutes.
Install lnmp V1.5 completed! enjoy it.

至此,LAMP环境已经在VPS上搭建完成,按Ctrl+C退出安装界面。

此时在浏览器中输入VPS的IP访问,会出现以下界面:

《WordPress建站:VPS+LAMP搭建新手教程》

(可选)为了安全,建议将 phpmyadmin 目录重命名为不容易猜到的目录(比如chenshenweiadmin)

# cd /home/wwwroot/default
# mv phpmyadmin chenshenweiadmin

(可选)在安装WordPress之前,建议安装PHP缓存加速类扩展,可以降低VPS压力和提高WordPress速度。

推荐安装两个:OPcache和Memcached。

首先,需要进入LNMP解压目录 lnmp1.5-full :

# cd /root/lnmp1.5-full

回车,接下来安装Opcache:

./addons.sh install opcache

回车,再回车。

当出现 “Opcache installed successfully, enjoy it!” 字样时,即表示安装成功。

接着安装Memcached:

./addons.sh install memcached

回车,选择2,回车,再回车。

当出现 “Memcached installed successfully, enjoy it!” 字样时,即表示安装成功。

此时,可以删除之前下载的lnmp1.5安装包,以节省空间。

# rm -rf /root/lnmp1.5-full.tar.gz

4 开启防火墙端口

在防火墙中开启所需端口(80和443等):

CentOS7中默认的防火墙为 firewalld 而非 iptables,博主比较习惯iptables所以首先关闭并禁止firewalld:

# service firewalld stop
# systemctl disable firewalld.service

然后安装iptables:

# yum -y install iptables-services

修改iptables的配置,开启端口:

# vi /etc/sysconfig/iptables

粘贴以下内容

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

保存退出并重启iptables:

# service iptables restart
# systemctl enable iptables.service

端口开启完毕。

5 添加域名

# lnmp vhost add

回车,提示输入域名,将自己解析的域名填写上去:

# chenshenwei.com

之后会有提示是否添加多个域名可以将其他解析的域名填上去(比如www.chenshenwei.com)

回车,填写网站目录,和域名保持相同,默认 /home/wwwroot/chenshenwei.com 即可。

回车是否记录日志,这里随意y或n。

回车后,输入自己的邮箱,一般填之前搭建LUMP环境时的邮箱即可。

继续回车,提示数据库名和数据库用户名是否保持一致,填写y

回车,输入 root 用户的数据库密码(就是之前搭建LUMP环境时的数据库密码)

回车,输入数据库名,自行设置,例如mysql_csw

回车,输入密码

回车,是否需要开启SSL/HTTPS访问,建议开启(反正有免费的证书,而且不麻烦,为什么不用呢?),输入y

回车,使用免费的Let’s Encrypt证书,所以输入2

添加域名完成。

详细步骤如下:

[root@chenshenwei.com ~]# lnmp vhost add
Please enter domain(example: www.lnmp.org): chenshenwei.com
Your domain: chenshenwei.com
Enter more domain name(example: lnmp.org *.lnmp.org): www.chenshenwei.com
domain list: www.chenshenwei.com
Please enter the directory for the domain: chenshenwei.com
Default directory: /home/wwwroot/chenshenwei.com:
Virtual Host Directory: /home/wwwroot/chenshenwei.com
Allow access log? (y/n) n
Disable access log.
Please enter Administrator Email Address: chenshenwei1998@gmail.com
Server Administrator Email:chenshenwei1998@gmail.com
Create database and MySQL user with same name (y/n) y
Enter current root password of Database (Password will not shown):
OK, MySQL root password correct.
Enter database name: mysql_csw
Your will create a database and MySQL user with same name: mysql_csw
Please enter password for mysql user mysql_csw: mypasswd
Your password: mypasswd
Add SSL Certificate (y/n) y
1: Use your own SSL Certificate and Key
2: Use Let's Encrypt to create SSL Certificate and Key
Enter 1 or 2: 2
It will be processed automatically.
Press any key to start create virtul host...

Create Virtul Host directory......
set permissions of Virtual Host directory......
Test Apache configure file...
test apache configure... Syntax OK
done
Restart Apache...
restart apache... done
Add database Sucessfully.
--2018-08-05 22:22:37-- https://soft.vpser.net/lib/acme.sh/latest.tar.gz
Resolving soft.vpser.net (soft.vpser.net)... 45.34.93.228, 2600:3c01::f03c:91ff:fe92:1a06
Connecting to soft.vpser.net (soft.vpser.net)|45.34.93.228|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 107645 (105K) [application/octet-stream] Saving to: ‘latest.tar.gz’

100%[================================================>] 107,645 --.-K/s in 0.02s

2018-08-05 22:22:38 (5.57 MB/s) - ‘latest.tar.gz’ saved [107645/107645] [Sat Aug 5 22:22:38 CST 2018] It is recommended to install socat first.
[Sat Aug 5 22:22:38 CST 2018] We use socat for standalone server if you use standalone mode.
[Sat Aug 5 22:22:38 CST 2018] If you don't use standalone mode, just ignore this warning.
[Sat Aug 5 22:22:38 CST 2018] Installing to /usr/local/acme.sh
[Sat Aug 5 22:22:38 CST 2018] Installed to /usr/local/acme.sh/acme.sh
[Sat Aug 5 22:22:38 CST 2018] Installing alias to '/root/.bashrc'
[Sat Aug 5 22:22:38 CST 2018] OK, Close and reopen your terminal to start using acme.sh
[Sat Aug 5 22:22:38 CST 2018] Installing alias to '/root/.cshrc'
[Sat Aug 5 22:22:38 CST 2018] Installing alias to '/root/.tcshrc'
[Sat Aug 5 22:22:38 CST 2018] Installing cron job
no crontab for root
no crontab for root
[Sat Aug 5 22:22:38 CST 2018] Good, bash is found, so change the shebang to use bash as preferred.
[Sat Aug 5 22:22:38 CST 2018] OK
Starting create SSL Certificate use Let's Encrypt...
[Sat Aug 5 22:22:39 CST 2018] Registering account
[Sat Aug 5 22:22:40 CST 2018] Registered
[Sat Aug 5 22:22:40 CST 2018] ACCOUNT_THUMBPRINT='oKzGuf4OhgE8SZtXn_3e9mnKUn49QPyPwd54i-asQd8'
[Sat Aug 5 22:22:40 CST 2018] Creating domain key
[Sat Aug 5 22:22:41 CST 2018] The domain key is here: /usr/local/apache/conf/ssl/chenshenwei.com/chenshenwei.com.key
[Sat Aug 5 22:22:41 CST 2018] Multi domain='DNS:chenshenwei.com,DNS:www.chenshenwei.com'
[Sat Aug 5 22:22:41 CST 2018] Getting domain auth token for each domain
[Sat Aug 5 22:22:41 CST 2018] Getting webroot for domain='chenshenwei.com'
[Sat Aug 5 22:22:41 CST 2018] Getting new-authz for domain='chenshenwei.com'
[Sat Aug 5 22:22:41 CST 2018] The new-authz request is ok.
[Sat Aug 5 22:22:42 CST 2018] Getting webroot for domain='www.chenshenwei.com'
[Sat Aug 5 22:22:42 CST 2018] Getting new-authz for domain='www.chenshenwei.com'
[Sat Aug 5 22:22:42 CST 2018] The new-authz request is ok.
[Sat Aug 5 22:22:42 CST 2018] Verifying:chenshenwei.com
[Sat Aug 5 22:22:46 CST 2018] Success
[Sat Aug 5 22:22:46 CST 2018] Verifying:www.chenshenwei.com
[Sat Aug 5 22:22:49 CST 2018] Success
[Sat Aug 5 22:22:49 CST 2018] Verify finished, start to sign.
[Sat Aug 5 22:22:50 CST 2018] Cert success.
[Sat Aug 5 22:22:50 CST 2018] Your cert is in /usr/local/apache/conf/ssl/chenshenwei.com/chenshenwei.com.cer
[Sat Aug 5 22:22:50 CST 2018] Your cert key is in /usr/local/apache/conf/ssl/chenshenwei.com/chenshenwei.com.key
[Sat Aug 5 22:22:51 CST 2018] The intermediate CA cert is in /usr/local/apache/conf/ssl/chenshenwei.com/ca.cer
[Sat Aug 5 22:22:51 CST 2018] And the full chain certs is there: /usr/local/apache/conf/ssl/chenshenwei.com/fullchain.cer
[Sat Aug 5 22:22:51 CST 2018] Run reload cmd: /etc/init.d/httpd graceful
graceful apache... done
[Sat Aug 5 22:22:51 CST 2018] Reload success
Let's Encrypt SSL Certificate create successfully.
Test Apache configure file...
test apache configure... Syntax OK
done
Restart Apache...
restart apache... done
================================================
Virtualhost infomation:
Your domain: chenshenwei.com
Home Directory: /home/wwwroot/chenshenwei.com
Enable log: no
Database username: mysql_csw
Database userpassword: mypasswd
Database Name: mysql_csw
Create ftp account: no
Enable SSL: yes
=>Let's Encrypt
================================================

如果之前选择了开启SSL/HTTPS访问,那么最好还要做以下步骤:

1 开启HSTS完善证书链

HTTP全部301重定向到HTTPS

6 安装wordpress软件

经历了千辛万苦,网站的基础设施已经建设完毕,如果我们只是想做一些简单的展示页面的话,那么我们只要把写好的html,css,JavaScript等文件扔到服务器的网站根目录即可,即把所有网页文件放到/home/wwwroot/chenshenwei.com目录中即可,注意将主页文件命名为index.html,这时在浏览器输入网址应该就能看见网页了。

关于如何将文件传输到vps上,这里推荐免费的FileZilla

如果你自己不会写网页想借用模板的话,推荐安装wordpress软件,可以通过可视化操作制作出精美的网页。

首先,进入域名的根目录:

# cd /home/wwwroot/chenshenwei.com

然后下载最新的wordpress安装包

# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

解压安装包

# tar -zxvf wordpress-4.9.4-zh_CN.tar.gz

将解压出来的wordpress文件夹内全部文件移动到当前的域名目录下

# mv wordpress/* .

为了节约空间可以选择删掉空文件夹及源程序(可选)

# rm -rf wordpress 

# rm -rf wordpress-4.9.4-zh_CN.tar.gz

赋予网站根目录文件的可写权限

# chmod -R 755 /home/wwwroot && chown -R www /home/wwwroot

现在打开浏览器输入域名就可以看见wordpress的安装界面了(如果出现403错误建议重启vps),开始建设你的网站吧!

 

点赞

发表评论

电子邮件地址不会被公开。