前言
以下是我建站的流程与中间遇到过的坑(主要是centos7和权限方面的坑)。申请域名和绑定IP等都是阿里云一条龙服务,所以略过不表,用的国外VPS所以网站没有备案,直接从LAMP环境搭建开始。
所谓LAMP,是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写,即Linux、Apache、Mysql、PHP。
其中Linux是系统平台,Apache是网页服务器,较常用的还有Nginx等,Mysql是数据库,其他常用还有PostgreSQL等,PHP是脚本语言,较常用的还有Python等。
以下流程主要参考自 JUSTZHI ,第一个大坑就是我的VPS是CentOS 7系统,由于apache(即httpd)、mysql(我用的mysql的分支mariadb)版本的不同,导致遇到问题时查询到的资料都太旧,某些问题卡了很久。
安装apache
yum install httpd httpd-devel #安装 systemctl start httpd #启动服务 systemctl enable httpd #设置服务开机自启 systemctl status httpd #查看服务状态,active firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --reload #防火墙开启80端口 netstat -tulp #确认端口监听中
安装成功后,默认网站根目录为/var/www/html
,可以在此文件夹中放入文件,通过网站访问或下载,从而实现无需密码的ftp文件服务(任何人都能访问,需注意安全性问题)。例如文件为 /var/www/html/test.zip
,则浏览器访问http://*.*.*.*/test.zip
,若能正常下载则apache服务正常。
403 Forbidden: You don''t have permission to access ** on this server.
以上所示为第一个坑,访问文件时显示无权限访问,原因是apache通过root用户启动后以另一个用户和组(默认为apache,可在/etc/httpd/conf/httpd.conf
中更改USER
和GROUP
参数)来管理文件,因此将网站根目录下所有文件拥有者改为apache
即可。
chown -R apache:apache /var/www/
第二个坑也是同样的无权限访问, 原因是如果使用winscp传输文件则需用root或者apache账号传输,否则可能因为上传文件信息为user_home_t:s0
导致apache账号无权限访问,将该信息修改即可修复。
chcon -R -t httpd_sys_content_t /var/www/ #public_content_t也可
安装数据库
mariadb是Mysql被Oracle收购后担心其闭源而出现的分支,命令操作兼容,代码如下。
yum install mariadb mariadb-server mariadb-libs mariadb-devel #安装 rpm -qa |grep maria #查看安装结果 systemctl start mariadb #开启服务 systemctl enable mariadb #设置服务开机自启 systemctl status mariadb #查看服务状态,确保active netstat -tulp #确认端口监听中 mysql_secure_installation #数据库安全设置 mysql -uroot -p #登录数据库展示和创建数据库 //MariaDB [(none)]> show databases; //MariaDB [(none)]> CREATE DATABASE anyname; #创建新数据库用于网站使用
这里遇到的一个坑是在建站成功使用一段时间就打不开网页了,网页错误信息为:Error establishing a database connection
。运行systemctl status mariadb
查看数据库状态为disactive
,果然是数据库挂了。
尝试重启mariadb服务(systemctl restart mariadb
)失败,服务状态变为failed。
本着从windows学来的重启治百病经验重启VPS节点后恢复,但没几分钟后数据库再次崩溃。。
登录数据库查看log文件,错误信息为 [ERROR] mysqld: Out of memory (Needed 128917504 bytes)
,毕竟这个VPS本来只是用来科学上网的,内存只有500M,要啥自行车)。
MariaDB [(none)]> show variables like 'log_error'; #查询错误日志文件位置,默认在/var/log/mariadb/mariadb.log
在不想升级性能的情况下,尝试通过创建SWAP分区方式增加虚拟内存,目前来看稳定多了。
使用 free -m
命令查看内存信息, 服务器swap 分区大小为 0。由于 VPS 使用了 SSD,性能应该足够。
创建并使用该分区命令如下:
dd if=/dev/zero of=/swapfile bs=1024 count=1048576 #创建swap file,大小1G mkswap /swapfile #配置 swapon /swapfile #立即生效,不必重启
在 /etc/fstab
文件中加入下面这一行, 这样可以在系统重启时可以自动使用创建的 swapfile。
/swapfile swap swap defaults 0 0
经过如上操作后,mariadb服务终于比较稳定了,不过这个解决方法属于强堆性能满足服务的需求,实际上还是需要对数据库服务本身做一下优化会更好,不过我还没学会更高深的技术,留个坑等以后再填吧。
安装PHP
yum -y install php #安装 rpm -ql php #查看安装结果 yum install php-mysql #将PHP和mariadb关联起来 rpm -ql php-mysql #查看安装结果 yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath #安装常用PHP模块
安装成功后重启apache(httpd)
systemctl restart httpd
注册并配置ssl
所谓ssl,即https证书,可以在网上免费申请个人证书,有效期多为几个月到两年不等。我使用的是在阿里云注册的一年免费个人证书。
注册过程略去不表,自行搜索即可,这里说一下证书的部署。
将证书的三个文件上传到/etc/httpd
目录下新建文件夹certs内,然后进行以下操作。
yum install mod_ssl #由于centos7的httpd默认不带ssl支持,因此需额外安装 vi /etc/httpd/conf.d/ssl.conf #编辑ssl配置文件,主要修改以下几行 DocumentRoot "/var/www/html" #配置网站根目录 ServerName "www.yumefx.com:443" #配置网站域名和端口 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM SSLHonorCipherOrder on SSLCertificateFile /etc/httpd/certs/ssl_public.pem SSLCertificateKeyFile /etc/httpd/certs/ssl.key SSLCertificateChainFile /etc/httpd/certs/ssl_chain.crt systemctl restart httpd #重启httpd服务
为了快乐,
我们都对自己撒谎。
《记忆碎片》
——克里斯托弗·诺兰
评论
I conceive this web site has got some very superb information for everyone :D. “The test of every religious, political, or educational system is the man that it forms.” by Henri Frdric Amiel.
https://youtu.be/YNdP5b7Z1ss
I’m not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission.
https://www.bigzh.com/hire-a-hacker-online/
I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road. All the best
https://www.fuduku.com/hire-a-hacker-for-instagram/
You are my inspiration , I own few web logs and infrequently run out from to post .
https://youtu.be/By5w3OC0AWA
After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.
https://youtu.be/sE6KesB4jDs
What i do not understood is actually how you are not actually much more well-liked than you may be now. You’re so intelligent. You realize thus significantly relating to this subject, made me personally consider it from a lot of varied angles. Its like women and men aren’t fascinated unless it’s one thing to accomplish with Lady gaga! Your own stuffs outstanding. Always maintain it up!
https://youtu.be/Mnou9U6nQtE
Whats up very cool site!! Man .. Beautiful .. Wonderful .. I will bookmark your website and take the feeds additionally…I’m glad to find numerous helpful info right here within the publish, we need develop extra techniques on this regard, thanks for sharing. . . . . .
https://youtu.be/uEhUJSSuaBE
Hi there, I discovered your web site by way of Google while searching for a comparable topic, your web site came up, it appears good. I’ve bookmarked it in my google bookmarks.
https://www.revtut.com/hire-a-hacker-for-whatsapp/