安装环境

  • CentOS 7 Linux系统安装nginx,php,mysql简写为LNMP

磨刀不误砍柴功,准备工作做好

关闭selinux

1
vi /etc/sysconfig/selinux

修改成SELinux=disabled,需要重启系统生效

检查网络连通性,保证设备能访问外网

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=44.8 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=63.2 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=128 time=44.8 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=4 ttl=128 time=44.5 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 44.513/49.378/63.281/8.032 ms

关闭防火墙

1
systemctl stop firewalld.service & systemctl disable firewalld.service

如果遇到这样的报错Failed to start firewalld.service: Unit not found.解决办法如下(依次执行)

1
2
3
4
5
6
yum install firewalld
systemctl unmask firewalld
systemctl enable firewalld
systemctl start firewalld
systemctl stop firewalld.service
systemctl disable firewalld.service

配置阿里云yum源

安装wget

1
yum -y install wget

备份原来的yum源

1
cd /etc/yum.repos.d/
1
mkdir bak
1
mv *.repo bak

配置阿里云yum源

1
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
1
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

清里yum缓存(重要)

1
yum clean all && yum makecache

方法一:编译安装nginx(更适合专业人员)

安装c++编译环境

1
yum -y install gcc-c++

安装openssl,pcre,zlib,vim,httpd-tools

1
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel vim httpd-tools

编译安装nginx

1
wget http://nginx.org/download/nginx-1.22.0.tar.gz
1
cd /usr/local/src/     # 默认下载路径
1
tar -zxvf nginx-1.22.0.tar.gz
1
cd nginx-1.22.0
1
./configure && make && make install

环境变量部分,配置好了就可以在任何目录执行nginx命令,感觉很有必要

查看nginx安装目录

1
whereis nginx

配置环境变量

1
vi /etc/profile.d/nginx.sh

里面写

1
export PATH="$PATH:/usr/local/nginx/sbin"

重新ssh连接使环境变量生效,不用重启系统

检查nginx环境变量是否配置成功,出现/usr/local/nginx/sbin就成功了,即出现你的安装路径/sbin

1
2
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/nginx/sbin:/root/bin

方法二:rpm包安装(简单)

下载rpm包

1
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm

安装,默认安装路径/etc/nginx

1
rpm -ivh nginx-1.20.2-1.el7.ngx.x86_64.rpm

启动

1
systemctl start nginx

开机自启(记得关selinux,上面有)

1
systemctl enable nginx

浏览器访问虚拟机或者服务器ip,出现Welcome to nginx!就安装成功啦

基本操作命令

相关常用指令及参数

参数 释义
nginx -h/-? 输出nginx的帮助信息
nginx -v 输出nginx版本信息
nginx -t 检查nginx配置文件语法是否正确
nginx -T 检查配置输出配置信息
nginx -s [参数] 参数=stop,quit,reopen,reload
nginx -c 设置自定义的nginx配置文件路径
nginx -g 设置覆盖一些默认的参数
nginx -q 检测配置文件期间屏蔽非错误信息

启动nginx

1
nginx 

访问ip:80,能访问则启动成功

停止nginx

1
nginx -s stop

平滑重启

1
nginx -s reload

查看nginx相关进程

1
ps -ef|grep nginx |grep -v grep

每次启动nginx前记得检查配置文件语法,ok就是没有问题

1
2
3
[root@localhost nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

安装php7.0

添加epel第三方依赖库,安装YUM源

1
yum -y install epel-release yum-utils
1
rpm -Uvh http://mirror.webtatic.com/yum/el7/epel-release.rpm
1
rpm -ivh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
1
yum clean all && yum makecache

安装PHP相关依赖库和模块

1
yum install -y php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm

检查php版本

1
php -v

启动php-fpm,并设置开机自启

1
systemctl start php-fpm && systemctl enable php-fpm

安装mysql

下载mysql压缩包

1
wget https://llwp.linlink.xyz/%E8%BD%AF%E4%BB%B6%E7%B3%BB%E5%88%97/Linux_x64%20mysql/mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar

解压

1
tar -xvf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar

本地安装MySQL

1
sudo yum localinstall *.rpm

查看MySQL版本

1
mysql --version

重启MySQL,并设置开启自启

1
systemctl restart mysqld && systemctl enable mysqld

获取密码,最后一段就是密码

1
grep 'temporary password' /var/log/mysqld.log

登录MySQL

1
mysql -u root -p

先设置一个复杂的密码

1
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '000Plinlink%';

修改密码规则

1
2
3
4
5
set global validate_password.length=6;
set global validate_password.mixed_case_count=0;
set global validate_password.number_count=0;
set global validate_password.policy=LOW;
set global validate_password.special_char_count=0;

现在可以设置简单的密码

1
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

进入MySQL数据库

1
use mysql;

修改权限、允许远程登陆

1
update user set host = '%' where user = 'root';
1
flush privileges;
1
select host, user from user;
1
quit