Centos7.4 安装 LNMP 环境

开始安装,使用 SSH 登录到服务器上,先执行 $ yum update, 更新软件仓库。

安装 Nginx:

# 添加nginx最新版软件源
$ yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装
$ yum install nginx -y
# 安装完成后启动nginx
$ service nginx start
# 查看是否启动成功
$ service nginx status

使用浏览器打开服务器 IP 地址,应该就可以看到 Nginx 的欢迎页面了

安装 MySQL 5.7

# 添加的 mysql5.7 的源
$ yum -y localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
# 安装mysql
$ yum -y install mysql-community-server install mysql-community-devel
# 启动mysql
$ service mysqld start
# 查看是否启动成功
$ service mysqld status

设置 Mysql 的密码,由于 Mysql 5.7 的密码为安装时自己随机生成,所以我们需要找到 Mysql 的默认密码,登录到 Mysql 进行修改。

# 查看mysql的默认密码
$ grep 'temporary password' /var/log/mysqld.log
# 复制密码,登录mysql
$ mysql -u root -p '密码'

执行以下密令进行修改

# 设置的密码必须是大于八位,并且包含数字、字母以及特殊字符
$ ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
$ quit;

退出,使用设置的密码重新登录 Mysql。

配置远程访问
$ GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
$ FLUSH PRIVILEGES;

安装 PHP7.2

yum 默认的是 5.4 版本,比较古老,我们安装 PHP 7.2版本

# 添加php7.2源
$ 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-fpm -y
# 安装扩展
$ yum -y install php72w-mbstring php72w-common php72w-gd php72w-mcrypt php72w-mysql php72w-xml php72w-cli php72w-devel php72w-pecl-memcached php72w-pecl-redis php72w-opcache -y
# 启动php-fpm
$ service php-fpm start
# 查看状态
$ service php-fpm status
# 查看版本
$ php -v

关联 Nginx 和 Php-Fpm

进入 /etc/nginx/conf.d,该目录下默认有个 default.conf 配置文件,将内容修改为:

server {
    listen       80;
    #server_name 根据自己需要进行修改
    server_name  localhost;
    #项目代码目录
    root         /www;
        
    charset utf-8;

    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }
        
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~* \.php$ {
        
        fastcgi_index index.php;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include       fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

根据我的配置,在根目录建立 www 作为项目运行的目录,我们新建一个 php 文件进行测试,是否能够正常解析运行 PHP:

$ mkdir /www
$ vim /www/index.php

<?php
phpinfo();

重新启动 Nginx,每次修改配置文件后都需要进行重启

$ service nginx reload

使用浏览器打开服务器 IP 地址,是否有 phpinfo 的页面


phpinfo

安装已基本完成,其他的配置根据自己的需要进行修改!

Redis

# 添加源
$ yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# 安装
$ yum --enablerepo=remi install -y redis
# 启动
$ service redis start

Git

Centos 使用 Yum 安装的默认 git 版本较古老,我们安装较新的 git,可以使用源码编译和 Yum 的形式,这里使用 yum 的方式。

# 配置源
yum install -y https://centos7.iuscommunity.org/ius-release.rpm
# 安装
yum install -y git2u

END!

文章同步发布在我的个人博客中,传送门Hesunfly Blog

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容