在Linux CentOS7系统中搭建LNMP

LNMP就是Linux+Nginx+MySQL+PHP,既然是在Linux CentOS7那么Linux就是已经安装好了。所以接下百度一下接下来的教程,整理测试如下:

教程是centos6.2的有点老,将就着看看,教程:https://www.linuxidc.com/Linux/2012-09/70788.htm

一、教程第1步是:配置防火墙,开启80端口、3306端口,最后重启防火墙使配置生效。但是重启失败,如下图:

经过百度,原来在ContOS7中 使用systemctl工具来管理服务程序的,链接:https://blog.csdn.net/doubleface999/article/details/73358003

第2步是,关闭SELINUX(好像没什么卵用),如果重启失败,请参考:https://blog.csdn.net/Angelloverbest/article/details/77825105

第3步是配置CentOS 6.2 第三方yum源

安装篇:

 

一、安装nginx
       yum install nginx      #安装nginx,根据提示,输入Y安装即可成功安装
       systemctl start nginx.service   #启动
       systemctl enable nginx.service    #设为开机启动
       systemctl restart nginx.service #重启
       rm -rf /usr/share/nginx/html/*  #删除ngin默认测试页

二、安装MySQL数据库。

  1.下载mysql的repo源

  $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

  2.安装mysql-community-release-el7-5.noarch.rpm包

  $ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

  安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。

  3.安装mysql

  $ sudo yum install mysql-server

  安装完成以后使用下面的命令开启数据库服务:
  systemctl start mysql.service #启动MariaDB
  systemctl stop mysql.service #停止MariaDB
  systemctl restart mysql.service #重启MariaDB
  systemctl enable mysql.service #设置开机启动

  4修改mysql密码

  mysql_secure_installation
       回车,根据提示输入Y
       输入2次密码,回车
       根据提示一路输入Y
       最后出现:Thanks for using MySQL!
       MySql密码设置完成,重新启动 MySQL

  另外:

另外有个版本叫Mariadb,如果需要安装Mariadb,可以使用下面的命令进行安装:
yum install -y mariadb
安装完成以后使用下面的命令开启数据库服务:
  systemctl start mariadb.service #启动MariaDB
  systemctl stop mariadb.service #停止MariaDB
  systemctl restart mariadb.service #重启MariaDB
  systemctl enable mariadb.service #设置开机启动

 

三、安装PHP,参考:https://blog.csdn.net/qq_34829953/article/details/78078790

yum install php   #根据提示输入Y直到安装完成,安装的是php5.4
2、安装PHP组件,使PHP支持 MySQL、PHP支持FastCGI模式
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm           
#根据提示输入Y回车

#如果要安装php5.6版本执行以下命令
yum provides php   #自带的只有5.4版本
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm         #更新源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum remove php-common -y     #移除系统自带的php-common
yum install -y php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring         #安装依赖包
php -v                    #版本变为5.6
yum provides php-fpm      #因为我是准备搭建lnmp,所以安装php-fpm,这里会提示多个安装源,选择5.6版本的安装就可以了
yum install php56w-fpm-5.6.31-1.w7.x86_64 -y

    systemctl start php-fpm.service #启动Php
  systemctl stop php-fpm.service #停止Php
  systemctl restart php-fpm.service #重启Php
  systemctl enable php-fpm.service #设置开机启动

=================================================================

==========================================================

配置篇:

一、配置nginx支持php
       cp /etc/nginx/nginx.conf  /etc/nginx/nginx.confbak    #备份原有配置文件
       vi /etc/nginx/nginx.conf  #编辑
       user   nginx  nginx;  #修改nginx运行账号为:nginx组的nginx用户
       :wq!    #保存退出

cp /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.confbak   #备份原有配置文件
vi /etc/nginx/conf.d/default.conf   #编辑

  root   /usr/share/nginx/html;   #增加

  index  index.php index.html index.htm;   #增加index.php

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

     #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径

二、配置php

  vi  /etc/php.ini   #编辑

  date.timezone = PRC     # 把前面的分号去掉,改为date.timezone = PRC

  disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
                          #列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
  expose_php = Off        # 禁止显示php版本的信息
  magic_quotes_gpc = On   # 打开magic_quotes_gpc来防止SQL注入,5.4以后版本未设置
 
      :wq!  #保存退出

三、配置php-fpm

      cp /etc/php-fpm.d/www.conf   /etc/php-fpm.d/www.confbak   #备份原有配置文件
       vi /etc/php-fpm.d/www.conf   #编辑
       user = nginx   #修改用户为nginx
       group = nginx   #修改组为nginx

    systemctl restart mysql.service  #重启MySql
       systemctl restart nginx.service  #重启nginx
      systemctl restart php-fpm.service  #重启php-fpm

===========================================================

============================================================

测试篇
      cd  /usr/share/nginx/html/   #进入nginx默认网站根目录
       vi  index.php   #新建index.php文件

<?php
           phpinfo();
       ?>
       :wq! #保存

chown nginx.nginx /usr/share/nginx/html/ -R  #设置目录所有者

chmod 700  /usr/share/nginx/html/ -R   #设置目录权限

在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!

 

posted @ 2018-08-02 14:58  苟富贵,无相忘  阅读(411)  评论(0编辑  收藏  举报