军哥centos lnmp安装环境

查看vps时区

参考:https://blog.csdn.net/ling1998/article/details/123696598

date
Wed Mar 23 09:12:10 EDT 2022

查看当前时区:date

查看系统当前的时区:timedatectl

修改系统使用的时区:timedatectl set-timezone Asia/Shanghai

查看vps的dns

查看DNS : cat /etc/resolv.conf

临时修改当前DNS

编辑修改dns

vi /etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4

按i 修改dns,完成后 esc  :wq  保存

永久修改dns

vi /etc/sysconfig/network-scripts/ifcfg-eth0

后面添加

DNS1=8.8.8.8
DNS2=8.8.4.4

按i 修改dns,完成后 esc  :wq!  保存

 

vi 编辑

教程:https://blog.csdn.net/heliosj/article/details/78191857

esc

退出:q  保存:w

 

按大写I进入插入模式后,光标所在行开始位置开始输入文件
按a进入插入模式后,光标所在位置的下一个位置开始输入文字;
按A进入插入模式后,光标所在行末尾开始输入文字;
按o进入插入模式后,在当前行下一行插入新的一行,从行首开始输入文字。
按O进入插入模式后,在当前行上一行是插入新的一行,从行首开始输入文字

防止ssh断开链接

检查是否安装screen

运行:command not found
如果提示下面就安装screen可以恢复断开的ssh
[root@localhost ~]# -bash: screen: command not found

使用方法:https://www.vpser.net/manage/screen.html

安装screen

CentOS系统可以执行:yum install screen

CentOS 8上移除了screen,需要安装epel后安装screen执行:yum install screen

创建screen会话
可以先执行:screen -S lnmp ,screen就会创建一个名字为lnmp的会话

恢复screen会话
当回来时可以再执行执行:screen -r lnmp 即可恢复到离开前创建的lnmp会话的工作界面。如果忘记了,或者当时没有指定会话名,

如果忘记了,或者当时没有指定会话名,可以执行:screen -ls screen会列出当前存在的会话列表

可以执行:screen -D -r lnmp 进行恢复

11791.lnmp即为刚才的screen创建的lnmp会话,目前已经暂时退出了lnmp会话,所以状态为Detached,当使用screen -r lnmp后状态就会变为Attached,11791是这个screen的会话的进程ID,恢复会话时也可以使用:screen -r 11791

关闭screen的会话
执行:exit ,会提示:[screen is terminating],表示已经成功退出screen会话

 

如执行screen -S lnmp  遇见报错 查看上面是否有提示 cannot find a valid baseurl for repo:base/7/x86_64

解决:修改CentOS-Base.repo原始文件为备份,重新上传新的文件

参考:https://blog.csdn.net/qq_39720249/article/details/84104646

文件下载:http://mirrors.163.com/.help/centos.html

ftp链接打开/etc/yum.repos.d/
备份CentOS-Base.repo文件为CentOS-Base.repo.bak,可以手动或者命令
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

从网易下载的文件上传进去
网易的命名为CentOS-Base.repo

生成缓存执行命令
yum clean all
yum makecache

验证问题是否解决,输入命令
yum repolist

查看repolist是否有

在执行安装:yum install screen

创建会话命令:screen -S lnmp

可以执行:screen -D -r lnmp 进行恢复

关闭执行:exit

 

安装军哥centos lnmp

安装命令更细页面:

https://lnmp.org/install.html

wget http://soft.vpser.net/lnmp/lnmp1.9.tar.gz -cO lnmp1.9.tar.gz && tar zxf lnmp1.9.tar.gz && cd lnmp1.9 && ./install.sh lnmp

选择mysql版本号:3
在设置mysql密码:直接回车将会设置为lnmp.org#随机数字

 安装php版本号输入:8

提示这个回车
You have 3 options for your Memory Allocator install.
1: Don't install Memory Allocator. (Default)
2: Install Jemalloc
3: Install TCMalloc
Enter your chsice (1, 2 or 3):


提示这个 按任意键继续
Press any key to install.….or Press Ctrltc to cancel

如果提示 any key install or Press Ctrl+c to Press to cancel

就是:任意键安装或按Ctrl+c按来取消

 

安装查看是否运行lnmp和相关

LNMPA 1.2+状态管理: lnmp {start|stop|reload|restart|kill|status}
{开始|停止|重新加载|重新开始|杀死|状态}
lnmp status
lnmp start

 

Nginx状态管理:/etc/init.d/nginx {start|stop|reload|restart}
{状态|开始|停止|重新加载|重新开始}

PHP-FPM状态管理:/etc/init.d/php-fpm {status|start|stop|quit|restart|reload|logrotate}
{状态|开始|停止|退出|重新启动|重新加载|logrotate}

MySQL状态管理:/etc/init.d/mysql {status|start|stop|restart|reload|force-reload|}
{状态|开始|停止|重新开始|重新加载|强制重新加载|}

1.报错 ERROR! MySQL server PID file could not be found!
搜索文件 which my.cnf
find / name my.cnf

进入目录 cd /usr/local/mysql
查看权限 ll
给权限 chmod -R 755 /usr/local/mysql/data
查看状态 lnmp status

 

默认安装目录

参考:https://lnmp.org/faq/lnmp-software-list.html

LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/Nginx日志目录:/home/wwwlogs/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
MariaDB 目录 : /usr/local/mariadb/
MariaDB数据库所在目录:/usr/local/mariadb/var/
PHP目录 : /usr/local/php/

LNMP相关配置文件位置
Nginx主配置(默认虚拟主机)文件:/usr/local/nginx/conf/nginx.conf
添加的虚拟主机配置文件:/usr/local/nginx/conf/vhost/域名.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
php-fpm配置文件:/usr/local/php/etc/php-fpm.conf
PureFtpd配置文件:/usr/local/pureftpd/pure-ftpd.conf 1.3及更高版本:/usr/local/pureftpd/etc/pure-ftpd.conf
PureFtpd MySQL配置文件:/usr/local/pureftpd/pureftpd-mysql.conf
Proftpd配置文件:/usr/local/proftpd/etc/proftpd.conf 1.2及之前版本为/usr/local/proftpd/proftpd.conf
Proftpd 用户配置文件:/usr/local/proftpd/etc/vhost/用户名.conf
Redis 配置文件:/usr/local/redis/etc/redis.conf

 

添加网站

参考:https://lnmp.org/faq/lnmp-vhost-add-howto.html

网站目录 :/home/wwwroot

输入添加网址:lnmp vhost add

添加网站
[root@localhost ~]# lnmp vhost add
Manager for LNMP, Written by Licess
https://lnmp.org
输入域名
Please enter domain(example: www.lnmp.org): ydmd.cc
已添加域名
Your domain: ydmd.cc
输入更多域名,一般加入两个域名带www和不带的
Enter more domain name(example: lnmp.org sub.lnmp.org): www.ydmd.cc
域列表
domain list: ydmd.cc www.ydmd.cc
请输入域的目录
Please enter the directory for the domain:ydmd.cc
默认目录;系统设定目录,回车确认
Default directory: /home/wwwroot/ydmd.cc:

允许重写规则吗?(y / n) 是否添加伪静态 开启Y
Allow Rewrite rule? (y/n)


如果网站是在二级目录则需要修改对应伪静态文件里的二级目录的名称。回车就好
Please enter the rewrite of programme,
wordpress,discuzx,typecho,thinkphp,laravel,codeigniter,yii2,zblog rewrite was exist.
(Default rewrite: other):

启用PHP Pathinfo吗?回车不启动
常见的wordpress、discuz x等都不需要开启!!!不需要pathinfo的网站程序开启pathinfo会访问出现各种问题。
默认回车不启用,启用请输入 y 后回车
You choose rewrite: other
Enable PHP Pathinfo? (y/n)

是否启动日志 y
Allow access log? (y/n)
默认日志目录为:/home/wwwlogs/ 默认文件名为:域名.log 回车确认后,会询问是否添加数据库和数据库用户。 回车
Enter access log filename(Default:ydmd.cc.log):

启用IPv6吗?(y / n) 检查主机是否支持才开 n
Enable IPv6? (y/n) 

创建同名的数据库和MySQL用户 n
Create database and MySQL user with same name (y/n)


添加SSL证书 n
Add SSL Certificate (y/n)


提示 Press any key to start create virtul host... 后,
回车确认便会开始创建虚拟主机

 

 

安装php opcache 和Redis

参考:https://lnmp.org/faq/addons.html

lnmp的sh目录:/root/lnmp1.9

安装redis
进入lnmp解压后的目录,执行:./addons.sh install redis

安装Opcache
进入lnmp解压后的目录,执行:./addons.sh install opcache
Opcache和eAccelerator是冲突的,脚本会卸载eAccelerator后再安装Opcache,回车确认后就会自动安装opcache。

卸载
执行:./addons.sh uninstall opcache

 

网站目录说明

lnmp的sh目录:/root/lnmp1.9

LNMP默认网站配置文件:/usr/local/nginx/conf/nginx.conf

新建的网址目录:/home/wwwroot

 

伪静态可以随时添加或删除,如果添加完虚拟主机后忘记或没有添加伪静态,可以通过修改配置文件来添加伪静态。
虚拟主机配置文件在:/usr/local/nginx/conf/vhost/域名.conf

伪静态规则文件需要放在/usr/local/nginx/conf/ 下面。

Nginx日志目录:/home/wwwlogs/

 

Nginx状态管理:/etc/init.d/nginx {start|stop|reload|restart}

/etc/init.d/nginx restart

 

Linux下重置MySQL root密码

Linux下重置MySQL root密码

1、停止MySQL服务
执行:/etc/init.d/mysql stop,你的机器上也不一定/etc/init.d/mysql也可能是/etc/init.d/mysqld

2、跳过验证启动MySQL
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &

注:如果mysqld_safe的位置如果和上面不一样需要修改成你的,如果不清楚可以用find命令查找。

3.自带目录/root/lnmp1.9/tools/reset_mysql_root_password.sh

cd /root/lnmp1.9/tools/

./reset_mysql_root_password.sh

4、重启MySQL
杀死MySQL进程:killall mysqld

重启MySQL:/etc/init.d/mysql start

二、使用重置脚本
wget http://soft.vpser.net/lnmp/ext/reset_mysql_root_password.sh;sh reset_mysql_root_password.sh

适合lnmp或MySQL管理脚本在/etc/init.d/mysql且mysql安装在/usr/local/mysql/的。否则需要修改脚本后再执行sh reset_mysql_root_password.sh。

 

lnmp代理了cdn 显示真实ip

进入目录 在lnmp目录 /root/lnmp1.9 找到lnmp.conf编辑它
把--with-http_realip_module填写进去,保存

Nginx_Modules_Options='--with-http_realip_module'

在cd 进入目录执行 ./upgrade.sh nginx
提示版本号 再手动输入版本号 回车 编译

完成后 进入nginx 配置目录
/usr/local/nginx/conf/nginx.conf

在http里面gzip on前面添加,保存
#cf ip
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;

重启nginx生效
/etc/init.d/nginx restart

进入日志目录查看日志 是否生效
/home/wwwlogs/

网站日志分割

access.log日志过大也会导致web性能下降

两个日志都要分割

ngixn主配置文件:/usr/local/nginx/conf/nginx.conf

虚拟主机配置文件在:/usr/local/nginx/conf/vhost/域名.conf

server
{
   ……
   #从系统时间中正则匹配出年月日
   if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
      set $date $1$2$3;
   }
   
   # 日期记录日志
   access_log  logs/$date.host.access.log;

重启nginx生效
/etc/init.d/nginx restart

ngixn基础防御CC

目录编辑 /usr/local/nginx/conf/nginx.conf
在 http和server之间加入如下内容 server_tokens off;的前面加上
        

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_req_zone $binary_remote_addr zone=allips:10m rate=5r/s;

解释说明:
limit_conn为限制并发连接数,$binary_remote_addr是限制同一客户端ip地址;
limit_rate为限制下载速度;$server_name是限制同一server最大并发数;


2.在网站配置文件中加入限制条件

vim /usr/local/nginx/conf/vhost/你的域名.conf
在server下加入如下内容 root  /home/wwwroot/**下面添加


        # #每个IP并发连接数量
        limit_conn perip 6;
        #通过令牌桶限制IP连接
        limit_req zone=allips burst=5 nodelay;
		
	参数说明:

limit_conn perip x; x为单个IP的最大并发连接数量,超过这个数量将不接受连接。

limit_req zone=allips burst=x nodelay; x为最大延迟请求数,如果太过多的请求被限制延迟是不需要的 ,这时需要使用nodelay参数,服务器会立刻返回503状态码	
	

 

设置cloudflare添加白名单

参考:http://www.aiii.vip/1259.html

 

 

 

 

 

 

 

 

 

 

 

版权声明:
作者:wanghaha
链接:http://www.aiii.vip/2068.html
来源:我的生活分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>