记录:CentOS7.2配置LNMP环境记录

CentOS7.2配置LNMP环境记录

PHP 5.6+ Nginx 1.10+ MysqL 5.5+

LNMP是Linux、NginxMysqL(MariaDB)和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一。本文将带领大家在CentOS 7操作系统上搭建一套LNMP环境。

本教程适用于CentOS 7.x版本。
准备工作

更新 yum 源,自带的源没有 PHP5.6 :

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

安装 epel:

yum install epel-release

然后更新下系统:

yum update

准备工作完成,开始安装!

安装Nginx

CentOS系统模板中配置了内网源,下载速度较快,推荐使用yum安装Nginx

sudo yum install Nginx

按照提示,输入yes后开始安装。安装完毕后,Nginx配置文件在/etc/Nginx目录下。使用以下命令启动Nginx

sudo systemctl start Nginx

检查系统中firewalld防火墙服务是否开启,如果已开启,我们需要修改防火墙配置,开启Nginx外网端口访问。

sudo systemctl status firewalld

如果显示active (running),则需要调整防火墙规则的配置。

修改/etc/firewalld/zones/public.xml文件,在zone一节中增加

<zone>
    ...
    <service name="Nginx"/>
<zone>

保存后重新加载firewalld服务:

sudo systemctl reload firewalld

您可以通过浏览器访问 http://<外网IP地址> 来确定Nginx是否已经启动。

最后将Nginx设置为开机启动:

sudo systemctl enable Nginx.service

这么Nginx就安装成功了!

安装MysqL(MariaDB)

MariaDB是MysqL一个分支,主要由开源社区进行维护和升级,而MysqL被Oracle收购以后,发展较慢。在CentOS 7的软件仓库中,将MysqL更替为了MariaDB。

我们可以使用yum直接安装MariaDB:

sudo yum install mariadb-server

安装完成之后,执行以下命令重启MariaDB服务:

sudo systemctl start mariadb

MariaDB认root密码为空,我们需要设置一下,执行脚本:

sudo /usr/bin/MysqL_secure_installation

这个脚本会经过一些列的交互问答来进行MariaDB的安全设置。

首先提示输入当前的root密码:

  • Enter current password for root (enter for none):
    初始root密码为空,我们直接敲回车进行下一步。

  • Set root password? [Y/n]
    设置root密码,认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。

  • Remove anonymous users? [Y/n] 是否移除匿名用户认选项为Yes,建议按认设置,回车继续。

  • disallow root login remotely? [Y/n]
    是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按认设置,回车继续。 如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。

  • Remove test database and access to it? [Y/n] 是否删除测试用的数据库和权限?
    建议按照认设置,回车继续。

  • Reload privilege tables Now? [Y/n]
    是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。

完成后你会看到Success!的提示,MariaDB的安全设置已经完成。我们可以使用以下命令登录MariaDB:

MysqL -uroot -p

提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。

最后我们将MariaDB设置为开机启动。

sudo systemctl enable mariadb

安装PHP

我们可以直接使用yum安装PHP

sudo yum install PHP56w-fpm PHP56w-MysqL PHP56w-MysqLi PHP56w PHP56w-opcache PHP56w-gd PHP56w-intl PHP56w-mbstring PHP56w-exif PHP56w-mcrypt PHP56w-openssl

//把该安装的一次性装到位

安装完成后我们将PHP-fpm启动:

sudo systemctl start PHP-fpm

PHP-fpm设置为开机启动:

sudo systemctl enable PHP-fpm

接下来需要注意了!~配置Nginx--多个站点

我给大家提供一个范本作为参考:

Nginx.conf

里面我会详细的给予中文注释

vi /etc/Nginx/Nginx.conf

//编辑Nginx.conf的命令
# For more information on configuration,see:
#   * Official English Documentation: http://Nginx.org/en/docs/
#   * Official Russian Documentation: http://Nginx.org/ru/docs/

user Nginx;
worker_processes auto;
error_log /var/log/Nginx/error.log; #错误日志记录的位置
pid /run/Nginx.pid; #Nginx.pid为记录Nginx主进程pid文件;切勿修改、移动
# Load dynamic modules. See /usr/share/Nginx/README.dynamic.
include /usr/share/Nginx/modules/*.conf;
#引入/usr/share/Nginx/modules/ 目录下的所有以.conf结尾的文件

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/Nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/Nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/Nginx/conf.d directory.
    # See http://Nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/Nginx/conf.d/*.conf;
    #这句很重要,引入所有etc/Nginx/conf.d/目录下的.conf文件
    
    #***etc/Nginx/conf.d/目录存放的就是分站点文件(下面会给出实例代码)***
    
    server {
        #由于我们的Nginx需要配置多站点,所以在此就需要注释一些东西
        
         listen       80 default_server;
         listen       [::]:80 default_server;
        #保留监听的端口  
        # server_name  _;
        # root         /usr/share/Nginx/PHP;

        # Load configuration files for the default server block.
        # include /etc/Nginx/default.d/*.conf;

        # location / {
        # }

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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        # location ~ \.PHP$ {
        # root           /usr/share/PHP;
        # fastcgi_pass   127.0.0.1:9000;
        # fastcgi_index  index.PHP;
        # fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        # include        fastcgi_params;
        # }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/Nginx/html;
#
#        ssl_certificate "/etc/pki/Nginx/server.crt";
#        ssl_certificate_key "/etc/pki/Nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/Nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

#注意:此份Nginx.conf可以直接复制了去使用!~好用了就给博主打个赏钱!谢谢!

配置完Nginx之后我们该干啥、?当然是重启Nginx

service Nginx start      #启动Nginx

service Nginx stop       #停止Nginx

service Nginx restart    #重启Nginx

sudo systemctl reload Nginx     #或者执行这条

重启完毕,继续打开 http://<外网IP地址> 来确定Nginx是否已经启动
此时,服务器启动的是Nginx和apache
而且PHP-fpm认发送到apache
所以咱们还得继续修改一下PHP-fpm

配置 PHP-fpm :

vi /etc/PHP-fpm.d/www.conf    

#编辑PHP-fpm配置文件

修改user和group (源代码为:user = apache group = apache)

user = Nginx    
group = Nginx

修改完了之后,还是老样子,重启PHP-fpm服务

service PHP-fpm start      #启动PHP-fpm

service PHP-fpm stop       #停止PHP-fpm

service PHP-fpm restart    #重启PHP-fpm

最后,咱们需要为Nginx添加站点

添加站点这我先给大家一个截图,以帮助大家迅速的了解是怎么回事


大家应该看的很清楚了,猜都可以猜到,博主这一共配置了三个站点,这三个站点是怎么被Nginx引入的呢?

我给大家贴出Nginx配置文件的里面应该有这么一句(注意图中的红框,上面的是地址)

include /etc/Nginx/conf.d/*.conf;
#这句很重要,引入所有etc/Nginx/conf.d/目录下的.conf文件
#***etc/Nginx/conf.d/目录存放的就是分站点文件(下面会给出实例代码)***

好的,大家应该能准确理解了,如果还是理解不了的话只能缺你回去喝点三鹿了!

下面我给大家贴出Nginx站点配置文件代码修改修改就可以用

代码的时候请注意看里面的路径,当然我也还是会给一定的中文注释
#这个文件是上面的qopmall.com.conf
server {

    server_name  qopmall.com www.qopmall.com;#这里就是你要绑定的域名了,空格分开
    location / {
            root   /usr/share/PHP/weixin; #这里是你站点存放的文件名称(也就是说,你当前这个站点文件全部都丢在这个路径的weixin文件夹里面)
            index  index.PHP index.html index.htm; #这里照抄即可
        }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.PHP$ {
        root           /usr/share/PHP/weixin; #这里的配置等同于上面的那个root配置
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/PHP/weixin/$fastcgi_script_name; #这里的配置也是和上面的root配置一样
        include        fastcgi_params;
    }
}

代码非常简单,我没注释到的不用修改就行;

上面的路径,比如/usr/share/PHP/weixin 这就是你站点的根目录,我给大家截图参考:

  • 各位童鞋,创建好站点了,先写个简单的PHP程序测试一下是否正常,比如info;

由于博主是个菜鸡,也就顺带给大家分享一下经验,老司机请绕道行驶!如果你觉得博主写得帮到了你,或者对你来说还算有用了,麻烦点个赞,土豪直接打赏就行,我不反对!see you!~

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native