CentOS 7 yum安装LNMP

20 篇文章 1 订阅

1、安装LNMP之前要安装EPEL,以便安装源以外的软件,如Nginx,phpMyAdmin等。

yum install epel-release

提示:EPEL,即Extra Packages for Enterprise Linux,企业版linux附加包。这个软件仓库里有很多非常常用的软件,而且是专门针对RHEL设计的,对RHEL标准yum源是一个很好的补充,完全免费使用,由Fedora项目维护,所以如果你使用的是RHEL,或者CentOS,Scientific等RHEL系的linux,可以非常放心的使用EPEL的yum源。 

yum update

2、安装Nginx

yum install nginx 
systemctl start nginx #启动nginx 
systemctl enable nginx #设置开机启动

安装成功后,浏览器访问主机公网IP,或者本机的127.0.0.1,或者虚拟机ip。会出现以下界面

3、安装mysql

在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/

# 下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安装mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查mysql源是否安装成功

yum repolist enabled | grep "mysql.*-community.*"

可以改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

vim /etc/yum.repos.d/mysql-community.repo

安装MySQL

yum install mysql-community-server

启动MySQL

service mysqld start

查看MySQL的启动状态

systemctl status mysqld

开机启动

systemctl enable mysqld

修改MySQL登陆密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

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

获取到密码之后登陆MySQL进行修改

mysql -uroot -p
set password for 'root'@'localhost'=password('MyNewPass4!'); //默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。

4、安装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 search php71w

//安装php以及扩展
yum install php71w php71w-fpm php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-pecl-redis.x86_64 -y

//开启服务
service php-fpm start

//开机自动启动
systemctl enable php-fpm

//重启nginx
service nginx restart

5、配置

修改php.ini的配置

vim /etc/php.ini 

cgi.fix_pathinfo=1 #将注释去掉,开启PHP的pathinfo伪静态功能。

max_execution_time = 0  #脚本运行的最长时间,默认30秒

max_input_time = 300#脚本可以消耗的时间,默认60秒

memory_limit = 256M#脚本运行最大消耗的内存,根据你的需求更改数值,默认128M

post_max_size = 100M  #单提交的最大数据,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的。限制范围包括表单提交的所有内容.例如:发表贴子时,贴子标题,内容,附件等…默认8M

upload_max_filesize = 10M#上载文件的最大许可大小 ,默认2M

date.timezone = "Asia/Shanghai"  #修改时区

然后重启PHP

service php-fpm restart

PHP-FPM 监听9000 端口正常

netstat -npa | grep 9000

6、配置Nginx根目录

nginx 默认会引入  /etc/nginx/conf.d/*.conf 下面的配置

所以我们在 /etc/nginx/conf.d/ 下面建立 default.conf。

#vim /etc/nginx/conf.d/default.conf

server{
    listen 80; #监听80端口
    server_name localhost; #访问的网址 例如:baidu.com   qq.com
    root /var/www/html; #制定根目录 例如:/var/www/html/baidu /var/www/html/qq
    sendfile on;
    #文件上传大小限制
    client_max_body_size    1000m;

    location / {
        index index.php index.html index.htm;
        if (-e $request_filename) {
            break;
        }
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
        }
    }
    location /api/ {
        index  index.php index.html index.htm;
        #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
        if (!-e $request_filename){
            #地址作为将参数rewrite到index.php上。
            #  rewrite ^/(.*)$ /index.php?s=$1;
            #若是子目录则使用下面这句,将subdir改成目录名称即可。
            rewrite ^/api/(.*)$ /api/index.php?s=$1 last;
        }
    }
    
    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/var/www/html";
        fastcgi_param HTTP_PROXY "";
        include fastcgi_params;
    }
    location ~* ^\/upload\/.+\.(html|php)$ {
        return 404;
    }
    location ~* ^\/plugins\/.+\.(html|php)$ {
        return 404;
    }
    location ~* ^\/themes\/.+\.(html|php)$ {
        return 404;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

//重启nginx

service nginx restart

7、测试

在 /var/www/html  目录下面建立一个info.php

<?php
    phpinfo();

然后访问 http://你的ip/info.php

403 Forbidden 解决办法

当访问时候,nginx 会按照 index.php  index.html  index.htm 的先后顺序在根目录中查找文件。如果这三个文件都不存在,那么nginx就会返回403 Forbidden。所以你可以吧你的/usr/share/nginx/html/目录下面的文件移动到/var/www/html/  即可,或者在/var/www/html/ 目录下面建立 index.php  index.html  index.htm。

注:

项目根目录
root   /usr/share/nginx/html;

A) 修改php.ini的配置
vim /etc/php.ini 

B) 修改php-fpm的配置
vim /etc/php-fpm.d/www.conf 

C) 修改nginx的配置
vim /etc/nginx/conf.d/default.conf  
//重启nginx
service nginx restart

//平稳刷新
service nginx force-reload

重启php
service php-fpm restart
           
#重启MySql
service mysqld restart

#重启防火墙
service iptables restart 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CentOS 7上使用yum安装LNMP是一种方便且快捷的方法。首先,您需要准备工作并切换到里云yum源。具体步骤如: 1. 备份原有的yum源: `mv /etc/yum.repos.d/Cent-Base.repo /etc/yum.repos.d/COS-Base.repo.backup` 2. 下载新的CentOS-Base.repo到/etc/yum.repos.d/: `wget -O /etc/y.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo` 或者 `curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo` 3. 清理缓存: `yum clean all` 4. 生成缓存: `yum makecache` 接下来,您可以使用以下命令来安装LNMP包: `yum install epel-release` 安装EPEL以便安装源以外的软件,如Nginx、phpMyAdmin等。 最后,您可以使用以下命令启动服务: `service nginx start` 这是使用yum安装LNMP的步骤和命令。通过这种方法,您可以方便地安装LNMP并使用各种服务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [centos7搭建LNMP环境-编译安装&yum安装-超详细](https://blog.csdn.net/handsomezls/article/details/116259445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [centos7 使用YUM安装lnmp环境](https://blog.csdn.net/weixin_40288231/article/details/106334673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值