CentOS 7* 搭建LNMP

参考:https://blog.csdn.net/u014558668/article/details/79314878

linux 环境

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.18084 (Core)

NGINX 安装

一、nginx 安装依赖

具体安装步骤详见:https://blog.csdn.net/root_miss/article/details/83011938

二、 nginx 安装

下载nginx最新稳定版本1.12.2源码:

wget wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压并进入nginx代码根目录:

tar zxvf nginx-1.12.2.tar.gz

cd nginx-1.12.2

配置

./configure --with-http_ssl_module --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11

./configure之后的正确信息如下:


Configuration summary
+ using PCRE library: ../pcre-8.41
 + using system OpenSSL library
 + using zlib library: ../zlib-1.2.11
 
  nginx path prefix: "/usr/local/nginx" 
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"  
  nginx configuration prefix: "/usr/local/nginx/conf" 
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf" 
  nginx pid file: "/usr/local/nginx/logs/nginx.pid" 
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp" 
  nginx http proxy temporary files: "proxy_temp"  
  nginx http fastcgi temporary files: "fastcgi_temp" 
  nginx http uwsgi temporary files: "uwsgi_temp" 
  nginx http scgi temporary files: "scgi_temp"


编译安装:

make

make install

检查nginx.conf配置正确性:

/usr/local/nginx/sbin/nginx

在浏览器访问结果如下:
在这里插入图片描述

安装成功后,在浏览器输入IP地址,打不开默认欢迎页面。

原因:CentOS 7版本之后对防火墙进行加强,不再使用原来的iptables,启用firewall防火墙默认不开放任何端口,所以Nginx默认的80端口也没有被放开,故而无法访问。

查看防火墙状态:

systemctl status firewalld.service

关闭防火墙之后在访问

systemctl stop firewalld.service

关于防火墙设置:

1.查看已开放的端口(默认不开放任何端口)
firewall-cmd --list-ports
2.开启80端口
firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效)
#以开启80端口为例
#firewall-cmd --zone=public --add-port=80/tcp --permanent  
3.重启防火墙
firewall-cmd --reload
4.停止防火墙
systemctl stop firewalld.service
5.禁止防火墙开机启动
systemctl disable firewalld.service
6.删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent

安装MySQL

一、下载安装官方提供的yum rpm包
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

安装rpm包:

rpm -ivh mysql57-community-release-el7-11.noarch.rpm

检查mysql的yum源是否安装成功:

yum repolist enabled | grep "mysql.*-community.*" 
二、使用yum install mysql-server安装
yum install mysql-server

查看版本信息:

rpm -qi mysql-community-server

启动mysql-server

service mysqld start

使用初始密码登陆

cat /var/log/mysqld.log|grep 'A temporary password'

最后一行冒号后面的部分就是初始密码。
使用此密码登录MySQL:
mysql -u root -p

修改初始密码

systemctl stop mysqld.service
mysqld --user=root --skip-grant-tables &
mysql -u root mysql
UPDATE user SET authentication_string=PASSWORD('new password') where USER='root';
FLUSH PRIVILEGES;
quit

systemctl start mysqld

正常登陆后发现一个问题:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

解决方法如下:

mysql> alter user 'root'@'localhost' identified by 'yourpassword';Query OK, 0 rows affected (0.00 sec) 
mysql> set password=password("yourpassword");Query OK, 0 rows affected, 1 warning (0.00 sec)
 mysql> flush privileges;Query OK, 0 rows affected (0.00 sec) 
 mysql> show databases;

常用的命令:
systemctl start mysqld #启动
mysqldsystemctl stop mysqld #停止
mysqldsystemctl restart mysqld #重启
mysqldsystemctl enable mysqld #设置开机启动
systemctl status mysqld #查看
MySQL Server 状态

PHP安装

一 选择php版本

通过yum list php*查看是否有自己需要安装的版本,如果没有就需要添加第三方yum源

查看当前 PHP 相关的安装包,删除之

yum list installed | grep php
yum remove php
yum remove php-*

更换 RPM 源

#Centos 5.X:
rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm
 
#CentOs 6.x:
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
 
#CentOs 7.X:
rpm -Uvh http://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum makecache
yum update
二、安装新版本 PHP
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mbstring php71w-mysqlnd php71w-opcache php71w-pdo php71w-xml php71w-ldap php71w-mcrypt

查看版本及支持的扩展

php -v
三、安装php-mysqlnd
yum install php-mysqlnd
四、安装PHP-FPM
yum install php-fpm
五、启动php-fpm
systemctl start php-fpm
六、 配置nginx.conf

去掉红色注释 注意/script 和 root 换成根目录
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页