如何在CentOS 6.5上安装部署LEMP运行环境?

本文适用于CentOS6安装LEMP环境

CentOS7请移步:

配置虚拟主机请移步:

老步骤,没有没有安装EPEL的,先安装EPEL:

yum -y install epel-release

开始安装Nginx

yum -y install nginx

设置开机自启动

chkconfig nginx on

启动Nginx

service nginx start

设置防火墙规则

iptables -A INPUT -p tcp --dport 80 -j ACCEPT   #http访问
iptables -A INPUT -p tcp --dport 443 -j ACCEPT    #https访问
service iptables restart

这时候访问http://yourIpAddress,应该会看到Nginx的默认页

Welcome to nginx on EPEL!

如果能访问默认页说明Nginx已经安装成功了。

安装MySQL数据库

yum -y install mysql-server

设置开机自启动

chkconfig mysqld on

启动MariaDB

service mysqld start

设置root密码

[root@********* ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password for root@localhost=password('yourRootPassword');
Query OK, 0 rows affected (0.00 sec)

查看密码设置状态,如果没有密码这里的password应该是空的

mysql> select user,host,password from mysql.user;
+------+--------------+-------------------------------------------+
| user | host         | password                                  |
+------+--------------+-------------------------------------------+
| root | localhost    | *158FB45BBAEA0CA69D32CC0F1D696799328A56D7 |
| root | iz238uepsriz |                                           |
| root | 127.0.0.1    |                                           |
|      | localhost    |                                           |
|      | iz238uepsriz |                                           |
+------+--------------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql> exit

用新密码登陆mysql:

[root@********* ~]# mysql -u root -p
Enter password:     #输入新密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

安装PHP-FPM

yum -y install php-fpm php-mysql php-pear php-pdo php-mysql php-gd php-mbstring php-mcrypt php-xml php-dom php-devel php-pear pcre-devel

很重要的PHP安全配置:cgi.fix_pathinfo,详细内容请参考

https://towait.com/blog/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7/

这里不做过多解释,直接执行命令即可

vi /etc/php.ini

搜索cgi.fix_pathinfo,找到

;cgi.fix_pathinfo=1

将这一行改成

cgi.fix_pathinfo=0

保存并退出。

配置PHP-FPM

修改php-fpm的conf配置文件

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

找到

user = apache
group = apche

修改为:

user = nginx
group = nginx

保存并退出

设置php-fpm开机自启动

chkconfig php-fpm on

开启php-fpm

service php-fpm start

建立第一个站点

新建站点可访问文件,我们这里使用phpinfo函数:

vi /var/www/html/index.php

输入内容:

<?php
phpinfo();
?>

保存并退出。

修改站点配置文件:

vi /etc/nginx/conf.d/default.conf

录入以下内容:

server {
    listen 80; 
    root /var/www/html/; ##站点目录
    index index.php index.html index.htm;  #索引文件

    server_name 127.0.0.1;  ##设置域名

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;  ##错误页
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/html/; 
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    # deny access to .htaccess files, if Apache’s document root
    # concurs with nginx’s one
    #
    location ~ /\.ht {
        deny all;
    }
}

保存后使用命令 /usr/sbin/nginx -t 测试conf文件是否有误

[root@localhost public_html]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx(服务重启)

service nginx restart

或使用命令nginx重新加载配置(推荐,服务不会中断):

/usr/sbin/nginx -s reload

然后访问http://yourIpAddressOrDomain/,这时候我们应该可以看到phpinfo的环境输出页。

Post Comment