Centos7 安装LNMP

准备篇:
1、配置防火墙,开启http服务
firewall-cmd --get-service
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
#########################################################
2、SELINUX设置为开放模式
setenforce 0
vim  /etc/selinux/config
SELINUX=permissive
:x  保存退出
3、配置第三方yum源(CentOS默认的标准源里没有nginx软件包)
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/7/x86_64/
enabled=1
gpgcheck=0
:x  保存退出
yum clean all

4、停止Apache

systemctl stop httpd  #停止Apache
systemctl disable httpd  #禁用Apache开机启动
################################################################
安装篇:
一、安装nginx
yum install nginx      #安装nginx,根据提示,输入Y安装即可成功安装
systemctl start nginx    #启动
systemctl enable nginx    #设为开机启动
rm -rf  /usr/share/nginx/html/*  #删除ngin默认测试页
二、安装数据库
1、安装mariadb
yum install mariadb mariadb-server   #询问是否要安装,输入Y即可自动安装,直到安装完成
systemctl start mariadb   #启动MySQL
systemctl enable mariadb   #设为开机启动
cp /usr/share/mysql/my-medium.cnf   /etc/my.cnf  #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
2、为root账户设置密码
mysql_secure_installation
提示输入根用户密码,直接回车
根据提示输入Y
输入2次密码,回车
根据提示一路回车
  
三、安装PHP
1、安装PHP
yum install php   #根据提示输入Y直到安装完成 
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回车
systemctl start php-fpm  #启动php-fpm
systemctl enable php-fpm  #设置开机启动
##################################################################
配置篇
一、配置nginx支持php
cp /etc/nginx/nginx.conf  /etc/nginx/nginx.confbak    #备份原有配置文件
vim /etc/nginx/nginx.conf       #编辑
user   nginx  nginx;          #修改nginx运行账号为:nginx组的nginx用户
:x!    #保存退出
cp   /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.confbak   #备份原有配置文件
vim  /etc/nginx/conf.d/default.conf   #编辑
index index.php index.html index.htm;   #增加index.php
            ......
location ~ \.php$ {
 root           /usr/share/nginx/html;
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 include        fastcgi_params;
}
#取消FastCGI server部分location的注释,要注意把root根目录改为绝对路径/usr/share/nginx/html,并且fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
二、配置php
vim  /etc/php.ini   #编辑
date.timezone = PRC     #把前面的分号去掉,改为date.timezone = PRC
:x!  #保存退出
三、配置php-fpm
cp   /etc/php-fpm.d/www.conf   /etc/php-fpm.d/www.confbak   #备份原有配置文件
vim  /etc/php-fpm.d/www.conf   #编辑
            user = nginx   #修改用户为nginx
            group = nginx   #修改组为nginx
systemctl restart mariadb  #重启数据库
systemctl restart nginx  #重启nginx
systemctl restart php-fpm  #重启php-fpm
#######################################################
测试篇
cd  /usr/share/nginx/html/   #进入nginx默认网站根目录
vim  index.php   #新建index.php文件
<?php
        phpinfo();
?>
:x!    #保存
chown nginx.nginx /usr/share/nginx/html/ -R  #设置目录所有者
chmod 700  /usr/share/nginx/html/ -R   #设置目录权限
在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!
#######################################################
备注
nginx默认站点目录是:/usr/share/nginx/html/
权限设置:chown nginx.nginx /usr/share/nginx/html/ -R


猜你喜欢

转载自blog.51cto.com/xlyong/2126665