lnmp

Centos 7 下安装 LNMP 官方最新版

软件版本

Nginx 版本: nginx 1.13.0
Mysql 版本:mysql 5.7.18
PHP 版本:php 7.1.5

查看 CentOS 版本号
cat /etc/redhat-release

Centos 版本:CentOS Linux release >7.3.1611 (Core) 64位

软件更新

阿里云 ECS 不建议升级服务器的内核和操作系统版本,所以,在进行系统软件更新操作时,应该将其剔除。

下面是查看服务器内核及系统版本,可以看一下在软件升级前后,剔除与否的区别。

1
2
uname -a
lsb_release -a

首先打开 /etc/yum.conf (vi /etc/yum.conf )文件,然后在 [main] 后面加入以下两行代码,这样在软件更新时,就不会升级内核及系统版本了。

1
2
exclude=kernel*
exclude=centos-release*

然后更新软件,-y 参数是在执行过程中,遇到的交互默认选择 yes ,如果想看一下软件的更新信息,可以去掉 -y 参数。

1
yum -y update

主机名修改

查看主机名指令

1
hostname

阿里云 ECS 默认的主机名是随机生成的,看着不是特别舒服,可以修改 /etc/hostname 文件,将现在的主机名改成自己想要的名称。

1
vim /etc/hostname

然后,按 ESC 输入 :wq 保存(vim 基本指令)。reboot(重启),看一下主机名是否已经变更。

配置第三方 yum 源

安装 centos7 的源

1
2
yum install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Nginx 安装

1
2
yum -y install nginx
nginx -v

使用 service 命令 操作 Nginx

1
2
3
4
service nginx start  //启动
service nginx stop //停止
service nginx restart //重启
service nginx reload //重新加载配置文件

Nginx 随开机自动启动

1
2
3
4
systemctl enable nginx

结果如下
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

Centos7 启动服务命令(启动 Nginx)

1
systemctl start nginx

查看 Nignx 状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
systemctl status nginx

结果如下
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2017-05-12 17:35:13 CST; 50s ago
Docs: http://nginx.org/en/docs/
Process: 22179 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 22178 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 22182 (nginx)
CGroup: /system.slice/nginx.service
├─22182 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─22183 nginx: worker process

May 12 17:35:13 iZ2zehs5ueqrbkxhzynwasZ systemd[1]: Starting nginx - high performance web.....
May 12 17:35:13 iZ2zehs5ueqrbkxhzynwasZ nginx[22178]: nginx: the configuration file /etc/n...k
May 12 17:35:13 iZ2zehs5ueqrbkxhzynwasZ nginx[22178]: nginx: configuration file /etc/nginx...l
May 12 17:35:13 iZ2zehs5ueqrbkxhzynwasZ systemd[1]: Failed to read PID from file /run/ngi...nt
May 12 17:35:13 iZ2zehs5ueqrbkxhzynwasZ systemd[1]: Started nginx - high performance web ...r.
Hint: Some lines were ellipsized, use -l to show in full.

防火墙配置

如果上述步骤结束后,没有看到欢迎页面,可能是防火墙配置造成的(80端口没有开放),Centos7 默认的防火墙是 FirewallD。

查看防火墙运行状态

1
firewall-cmd --state

开启 FirewallD 服务

1
systemctl start firewalld

查看 FirewallD 状态

1
systemctl status firewalld

查看 FirewallD 配置

1
firewall-cmd --list-all

开启端口

1
2
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent

命令含义

1
2
3
4
5
--zone #作用域

--add-port=80/tcp #添加端口,格式为:端口/通讯协议

--permanent #永久生效,没有此参数重启后失效

重启防火墙

1
firewall-cmd --reload

MySQL 最新版安装

1
2
3
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

yum -y install mysql-community-server mysql-community-devel

MySQL 安装完成后配置文件会在这个路径 /etc/my.cnf 可以根据实际需要修改里边的选项。这里暂时不做任何修改,但有个选项是要注意的,因为后面配置 PHP 的时候讲会用到:

1
2
3
4
grep socket /etc/my.cnf

结果如下
socket=/var/lib/mysql/mysql.sock

查看 MySQL 状态

1
2
3
4
5
6
7
8
systemctl status mysqld

结果如下
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html

Mysql 添加到开机启动项

1
systemctl enable mysqld

启动 MySQL

1
systemctl start mysqld

MySQL 5.7 和之前版本很大区别是在安装后会自动为 root@localhost 用户设置一个随机初始密码,之前的版本密码为空的。那如何找到这个初始密码呢?网上很多文章说初始密码在这个文件中 /root/.mysql_secret 我不清楚早前的版本是不是这样,但 MySQL 5.7.11 并不然,而是保持到 error log 文件中。可以通过下面方法找到 MySQL 5.7 root 的初始密码

1
2
3
4
grep 'temporary password' /var/log/mysqld.log

结果如下
2017-05-12T09:41:44.686145Z 1 [Note] A temporary password is generated for root@localhost: U4ygpaZyy(up

其中 U4ygpaZyy(up 就是密码。现在立即用这个密码登录 MySQL 并且修改密码(MySQL 5.7 版本对密码的安全性要求很严格,必须至少包含1个大写字母、1个小写字母、1个数字和1个特殊字符,长度不得小于8个字符)

1
2
3
4
5
6
7
8
9
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Aliyun2017.';

update mysql.user set Host='%' where HOST='localhost' and User='root';

flush privileges;

SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;

quit

mysql5.7 重置密码使用这个语句

1
update mysql.user set authentication_string=password('abcabc') where user='root' and Host = 'localhost';

ps:如果 mysql 授权远程访问之后,访问还是失败。可能是由于防火墙和 selinux 没有关闭到底。
Centos7 关闭防火墙和 selinux 命令如下:

1
2
3
4
5
6
systemctl disable firewalld.service 

systemctl stop firewalld.service

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

在 mysql 中输入命令,开启远程连接权限

1
grant all privileges on *.* to 'root'@'%' identified by 'YOUR_PASSWORD' with grant option;

YOUR_PASSWORD 替换成你的mysql连接密码。

该命令的具体意思可见阿里云官方说法:官方

1
2
3
4
5
6
刷新权限
flush privileges;
exit

重启 mysql 服务
systemctl start mysqld

PHP7 最新版安装

安装 REMI 仓库之前,首先安装 EPEL 仓库,因为 REMI 中的一些包依赖于 EPEL

1
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

接下来安装 REMI 仓库

1
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

再安装 yum 的配置管理工具,方便配置 yum 仓库

1
yum install yum-utils

启用 remi-php71 仓库

1
yum-config-manager --enable remi-php71

最后,安装 PHP

1
2
3
4
5
yum install php

find / -type f -name "php" -print

/opt/remi/php71/root/usr/bin/php

查看 PHP 版本以及扩展信息

1
2
php --v
php --m

安装 PHP-FPM,简单理解,PHP-FPM 是 FastCGI 的管理器,FastCGI 是处理 Nginx 转发过来的 PHP 脚本的。

1
yum install php-fpm

配置 nginx 支持 php

将默认的配置文件作为配置文件

1
cp /etc/nginx/conf.d/default.conf abc.conf

进入nginx配置文件进行修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cp /etc/nginx/conf.d/abc.conf

找到80端口的配置,加入index.php
index index.php index.html index.htm;

加入下面的配置代码,如果已经存在的,去掉注释,并修改,其中/usr/share/nginx/html是web根目录的地址,可以根据自己的喜好进行选择,我一般放在/var/www下

location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

最后,编辑文件php.ini,在文件末尾添加cgi.fix_pathinfo = 1,已经存在的去掉注释就好了

1
cgi.fix_pathinfo = 1

重启nginx,启动php-fpm

1
2
systemctl restart nginx.service
systemctl start php-fpm.service

设置php-fpm开机自启动

1
2
3
systemctl enable php-fpm.service

netstat -antp

安装Redis

1
yum install redis php-pecl-redis

把Redis添加到开机启动服务中:

1
2
3
service redis start

systemctl enable redis.service

检查Redis是否运行:

1
systemctl is-active redis.service

重启php-fpm:

1
service php-fpm restart

源码编译安装 Git

centos 自带 git,7.x版本自带 git 1.8.3.1
在 Linux 中有的需要先卸载原来自带的 Git

1
sudo yum remove git

安装 Git 的依赖包

1
sudo yum install dh-autoreconf curl-devel expat-devel gettext-devel openssl-devel perl-devel zlib-devel

为了能支持多种格式的文档也需要安装一些额外的包

1
sudo yum install asciidoc xmlto docbook2X getopt

下载 Git 源码

1
sudo wget https://www.kernel.org/pub/software/scm/git/git-2.9.4.tar.gz

解压

1
tar -xzvf git-2.9.4.tar.gz

注意:如果使用的 Fedora 系列的操作系统需要设置软连接

1
sudo ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi

进入解压目录编译安装(时间比较漫长)

1
2
3
4
cd git-2.9.4
./configure --prefix=/usr/local/git
sudo make all doc info
sudo make install install-doc install-html install-info

添加 Git 到环境变量

1
2
3
4
sudo echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile 
sudo source /etc/profile

git --version

学习参考

(yum安装nginx和mysql,php7源码安装)
centos上安装lnmp环境
不可不知的centos7 firewalld 防火墙的使用
CentOS7 安装 Mysql 5.7,密码查看与修改
Centos 7.2 安装配置

如果对您有用,请博主喝杯咖啡!

热评文章