CentOS LNMP环境搭建

上一篇说了如何搭建 LAMP 环境,那除了 Apache 之外我们知道还有一个著名的 Web 服务软件 - Nginx,今天说说如何搭建 LNMP 环境,即 Linux - Nginx - MySQL - PHP。

编译安装

如果你想快速搭建 LNMP 环境,建议选择 yum 安装,如果想要自己定制或者是生产环境,建议选择 - 编译安装LAMP、LNMP环境

配置yum源

epel

RedHat 扩展软件 yum 源

官网 --> http://mirrors.ustc.edu.cn/fedora/epel/

CentOS 6
rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/epel-release-latest-6.noarch.rpm

CentOS 7
rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/epel-release-latest-7.noarch.rpm

mysql

mysql 社区版 yum 源

官网 --> http://repo.mysql.com/

CentOS 6
rpm -ivh http://repo.mysql.com/mysql57-community-release-el6.rpm

CentOS 7
rpm -ivh http://repo.mysql.com/mysql57-community-release-el7.rpm

mariadb

mariadb yum 源
mysql 被甲骨文收购之前的一个分支版本,避免 mysql 闭源风险;
这里选择 mysql,你也可以选择安装 mariadb,配置基本兼容。

官网 --> https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft

CentOS 6
--- mariadb.repo ---
# MariaDB 10.1 CentOS repository list - created 2016-11-28 10:25 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
--- mariadb.repo ---

CentOS 7
--- mariadb.repo ---
# MariaDB 10.1 CentOS repository list - created 2016-11-28 10:27 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
--- mariadb.repo ---

安装Nginx

yum -y install nginx

安装MySQL

yum -y install mysql mysql-server mysql-devel

安装PHP5

PHP7 - 参考

yum -y install php-fpm php-devel 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
# nginx 不能处理 php,所以需要安装 php-fpm,将 php 交给 php-fpm 处理

相关配置

nginx

vim /etc/nginx/conf.d/www.conf
## server字段添加location
location ~* \.php$ {
        fastcgi_pass   127.0.0.1:9000;  # 9000为php-fpm监听端口
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

## 隐藏nginx服务器信息
http {
server_tokens off;
}

## 默认编码utf-8
http {
    charset utf-8;
}

php-fpm

vim /etc/php-fpm.d/www.conf
user = nginx    # 改为nginx
group = nginx   # 改为nginx

chown nginx /var/log/php-fpm/

cd /var/lib/php/
mkdir session wsdlcache
chmod 775 *
chown :nginx *

mysql

service mysqld start
mysql_secure_installation   # 安全配置
# 输入root密码,然后设置root的密码,一路回车 出现Thanks for using MySQL! 表示设置成功!

启动服务

chkconfig nginx on
chkconfig php-fpm on
chkconfig mysqld on
service nginx start
service php-fpm start
service mysqld start

ss -lnp | egrep 'nginx|php-fpm|mysqld'

环境测试

vim /usr/share/nginx/html/www/info.php
--- info.php ---
<?php
phpinfo();
?>
--- info.php ---

chown nginx:nginx /usr/share/nginx/html/www/info.php

浏览http://IP/info.php

lnmp环境搭建