Centos7 配置 LNMP环境

本文仅作为原址失效时提供备份访问。备份来源:https://segmentfault.com/a/1190000013842789


-------------------------------------------


1、yum的安装

yum update

2、yum 安装 nginx

# 安装 nginx 最新源
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist enabled | grep "nginx*"   
# 安装 nginx
yum -y install nginx
# 启动 nginx
service nginx start
# 设置 nginx 服务器开机自启动
systemctl enable nginx.service
# 检查开机自动是否设置成功
systemctl list-dependencies | grep nginx
# 浏览器中输入公网 ip, 检测是否安装成功
http://xxx.xxx.xxx.xxx

3、使用 yum 安装 MySQL5.7

# 安装 mysql 源
yum -y localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
# 安装 mysql
yum -y install mysql-community-server install mysql-community-devel
# 若提示 GPG Keys 已过期,需要执行以下命令,再重新安装:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

# 启动 mysql
service mysqld start
# 检查 mysql 启动是否正常
service mysqld status 或者 ps -ef | grep mysql
# 设置 mysqld 服务开机自启动
systemctl enable mysqld.service
# 检查 mysqld 开机自启动是否设置成功
systemctl list-dependencies | grep mysqld

修改 mysql 登录密码

mysql5.7 以后的争强了安全机制,所以使用 yum 安装,启动会系统会自动生成一个随机的密码。

查看 mysql 的随机密码

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

使用查询得到的随机密码在终端登录,并修改密码(mysql 文档规定,密码必须包括大小写字母数字加特殊符号 > 8 位)

mysql -u root -p 初始随机密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';

退出 mysql 客户端,用刚才修改的密码登录确保密码修改成功

exit;
mysql -u root -p

4.1、REMI源 安装 php7.3

# CentOS 7 安装 REMI 源
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 安装 Yum 源管理工具
yum -y install yum-utils
# 安装PHP软件
yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll
yum install -y php73-php-zip php73-php-pecl-memcached php73-php-pecl-redis php73-php-opcache php73-php-xml php73-php-xmlrpc
# 设置开机自启动
systemctl enable php73-php-fpm
# 启动 php-fpm
systemctl start php73-php-fpm
# 建立 PHP 的软链
ln -s /opt/remi/php73/root/usr/bin/php /usr/bin/php
# 查询 php.ini 位置
php -i | grep "Loaded Configuration File"
# 查询状态
php -v
php -m

4.2、yum 安装 php7.2

# 安装 php 源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# 检查源是否安装成功
yum repolist enabled | grep "webtatic*"

# 安装 php 常用扩展源
yum -y install php72w php72w-fpm
yum -y install php72w-mbstring php72w-common php72w-gd php72w-mcrypt
yum -y install php72w-mysql php72w-xml php72w-cli php72w-devel
yum -y install php72w-pecl-memcached php72w-pecl-redis php72w-opcache
# 验证 php 和扩展是否安装成功
php -v
php -m

设置 php-fpm

# 启动 php-fpm
service php-fpm start
# 检查启动是否成功
service php-fpm status
# 设置开机自启动
systemctl enable php-fpm.service
# 检查开机自启动是否设置成功
systemctl list-dependencies | grep php-fpm
ps -ef | grep php-fpm

5、nginx 配置

进入 ngixn 配置目录

cd /etc/nginx
vim nginx.conf

配置 nginx.conf 中的 server

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /data/website;

        include /etc/nginx/default.d/*.conf;

        location / {
	        index  index.html index.htm index.php;
        	try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
        	fastcgi_pass   127.0.0.1:9000;
        	fastcgi_index  index.php;
        	fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        	include        fastcgi_params;
        }

        if ( $http_user_agent ~ ^$) {
        	return 503; # 禁止空 agent
        }

        if ( $http_user_agent ~* "curl") {
        	return 503; # 禁止 curl
        }

        error_page 404 /404.html;
            	location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
         	location = /50x.html {
        }
}