centos7.4 搭建lnmp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7.4 搭建lnmp相关的知识,希望对你有一定的参考价值。

系统:阿里云 centos7.4

php:PHP 7.1.13 (cli) 

mysql:mysql5.7

nginx:nginx/1.12.2

 

一、更新centos7 yum源

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
wget http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all yum makecache yum -y update yum -y install gcc* #安卓gcc组件 yum -y install vim

 

#下载yum源
wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-3.4.3-132.el7.centos.0.1.noarch.rpm
wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm   
wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm 
wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm 

#安装下载的rpm包
rpm -ivh *.rpm 


#创建文件/etc/yum.repos.d/rhel-debuginfo.repo并写入
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.163.com/centos/7.2.1511/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/7.2.1511/os/x86_64/RPM-GPG-KEY-CentOS-7


#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.163.com/centos/7.2.1511/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/7.2.1511/os/x86_64/RPM-GPG-KEY-CentOS-7


[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.163.com/centos/7.2.1511/extras//$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/7.2.1511/os/x86_64/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.163.com/centos/7.2.1511/centosplus//$basearch/
gpgcheck=1
enabled=0


yum clean all 
yum update
#安装 epel 源
yum install epel-release

 

 

 

二 、firwall 替换成iptables

//关闭默认防火墙
#停止firewall
systemctl stop firewalld.service 
#禁止firewall开机启动
systemctl disable firewalld.service 
#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state 
//安装iptables
yum install  -y iptables  iptables-services
// 查看iptables配置文件
iptables -L  
// 配置里面的规则 -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 
vim /etc/sysconfig/iptables
#添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
#开启
service iptables start 
#设置防火墙开机启动
systemctl enable iptables.service 
#查看iptables状态
service iptables status      
#iptables服务重启
service iptables restart     
#iptables服务禁用 
service iptables stop    

三、安装Nginx

#安装nginx源【centos7.4 yum安装默认的Nginx为1.12.2】
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm  
#安装
yum install nginx
service nginx start  
 
在浏览器输入ip查看
 

四、安装mysql

#安装mysql源  
yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm  
#安装mysql
yum install mysql-community-server  
#安装mysql的开发包
yum install mysql-community-devel  
#启动mysql
service mysqld start  
#查看mysql启动状态
service mysqld status 
#获取mysql默认密码

[root@localhost /]# grep \'temporary password\' /var/log/mysqld.log
2018-03-30T06:49:03.200170Z 1 [Note] A temporary password is generated for root@localhost: g0Zb#ku;dOh&

 

五、安装php

资料来源   https://blog.csdn.net/river131/article/details/53838640
rpm 安装 Php7 相应的 yum源
    CentOS/RHEL 7.x:
    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
安装PHP7.2
    yum install php72w php72w-opcache
 安装依赖
 yum install php72w php72w-bcmath php72w-cli php72w-common php72w-dba php72w-devel php72w-embedded php72w-enchant php72w-fpm php72w-gd php72w-imap php72w-interbase php72w-intl php72w-ldap php72w-mbstring php72w-mcrypt php72w-mysql php72w-mysqlnd php72w-odbc php72w-opcache php72w-pdo php72w-pdo_dblib php72w-pear php72w-pecl-apcu php72w-pecl-imagick php72w-pecl-xdebug php72w-pgsql php72w-phpdbg php72w-process php72w-pspell php72w-recode php72w-snmp php72w-soap php72w-tidy php72w-xml php72w-xmlrpc

如果是Apache + PHP的话必须使用 PHPIniDir 指定php5的配置文件php.ini的路径
PHPINIDir /etc/php.ini

 

六、配置Nginx支持PHP

 

#添加用户组、用户
groupadd www #添加mysql组
useradd -g www www -s /bin/false #添加的用户直接登录系统

#直接使用编译后未经优化处理的配置
cp /usr/local/src/php-7.1.13/php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/local/src/php-7.1.13/sapi/fpm/php-fpm.service /lib/systemd/system/php-fpm.service

#配置PHP文件
vi /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.d/www.conf

######测试配置
/usr/local/php/sbin/php-fpm -t
######修改文件权限
chmod 745 /lib/systemd/system/php-fpm.service   
######设置为开机启动
systemctl enable php-fpm.service
######启动php-fpm
systemctl start php-fpm.service

#配置域名
vim /etc/nginx/conf.d/vhost.conf
#添加

server {
    listen       80;
    server_name  www.xxx.com;
    access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /www/;
        index  index.html index.htm index.php ;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~* \\.php$ {
        root           /www/;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~ /\\.ht {
        deny  all;
    }
}

 

 nginx报403

一、缺少index.html或者index.php文件,
二、权限问题,如果nginx没有web目录的操作权限,也会出现403错误。
三、SELinux设置为开启状态(enabled)的原因

 

 

 

 

  

 

 

 

 

文献参考:

    http://blog.csdn.net/u012027999/article/details/73744032,

    http://blog.csdn.net/trh0123/article/details/53899610?utm_source=itdadao&utm_medium=referral,

    https://www.cnblogs.com/zzh10086130/p/6440378.html,

    https://www.cnblogs.com/zzh10086130/p/6440378.html

以上是关于centos7.4 搭建lnmp的主要内容,如果未能解决你的问题,请参考以下文章

centos7.4安装最新lnmp架构设置授权访问并搭建Discuz论坛

Linux精华篇—CentOS 7.4下源码编译构建LNMP架构

Linux下Dockerfile搭建lnmp(目前只有n和p)

CentOS 7.4 yum方式搭建LNMP环境,部署WordPress博客,并安装XCache模块

CentOS7.4 源码编译安装LNMP

CentOS7.4—构建Apache+Nginx动静分离