CentOS7下LNMP环境配置-服务器配置

CentOS7下LNMP环境配置-服务器配置 LNMP 环境是当前比较流行的网站服务器构架,各大云服务商均有相应的集成环境,使用比较方便。本文介绍的是如何在一台未安装集成环境的服务器中配置LNMP 环境。

LNMP 环境代表 Linux 系统下 Nginx + MySQL + PHP 网站服务器架构。
使用CentOS7中的yum源安装
Nginx 安装 1.使用yum安装Nginx
- 若没有Nginx源 或想安装比较新的版本可下载更新Nginx源
```
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
```
- 安装
```shell
yum -y install nginx
```
2.Nginx启动
```shell
service nginx start
# 或
systemctl start nginx.service
# 若不支持service 命令 可用systemctl命令
```
##### 3.设置开机启动
```shell
systemctl enable nginx.service
```
##### 4.其他命令
```shell
service nginx restart # 重启Nginx
service nginx stop # 停止Nginx服务
nginx -t # 查看Nginx配置文件路径
systemctl list-units --type=service # 查看开机启动项
systemctl disable nginx.service # 停止开机启动
nginx -v # 查看安装版本
```
##### 5.测试Nginx服务是否正常运行
```shell
wget http://127.0.0.1
```
> 正常运行,输出以下结果:
```shell
--2017-10-28 13:19:31--http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’
[ <=>] 53,024--.-K/sin 0s
2017-10-28 13:19:31 (190 MB/s) - ‘index.html’ saved [53024]
```
## PHP 安装
##### 1.安装PHP及其依赖、扩展,版本5.4.16
```shell
yum -y install php php-fpm lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap
```
##### 2.启动PHP(php-fpm)服务
```shell
service php-fpm start
systemctl enable php-fpm.service # 加入开机启动项
```
##### 3.PHP默认服务IP端口
```shell
# 查看默认配置
cat /etc/php-fpm.d/www.conf |grep -i 'listen ='
# 输出结果
listen = 127.0.0.1:9000
```
##### 4.修改Nginx配置文件,实现PHP转发
- 编辑Nginx配置文件
```shell
vi /etc/nginx/nginx.conf
```
- 进入编辑器,按I或者Insert键进入Insert模式,修改server中的配置参数。注:#号为注释符
```shell
server {
# 监听80端口
listen80 default_server;
listen[::]:80 default_server;
# 服务名称 可自定义已经解析到本服务器的域名,如www.binjx.cn
server_namelocalhost;
# 网站根目录 绝对路径 可自定义
root/usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
【CentOS7下LNMP环境配置-服务器配置】 # 修改、添加如下代码 start
location / {
indexindex.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
# 修改、添加 end
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
```
- 编辑完成,按Esc键退出Insert模式,再按Shift+Q键,此时可以选择是否保存修改,操作命令:q 退出,wq或x保存退出,q!不保存强制退出。
```shell
Entering Ex mode.Type "visual" to go to Normal mode.
:wq
```
- 检测配置
```shell
service nignx restart
```
- 重启Nginx服务器,配置生效
```shell
service nignx restart
```
## MySql 安装
> 在CentOs7的yum源中,默认的数据库已经不是MySQL,而是MariaDB,MariaDB兼容MySql,甚至更优于MySQL。有兴趣的朋友可以看一下两者的区别。[浅谈MySQL和mariadb区别_mariadb](https://yq.aliyun.com/ziliao/92489 "浅谈MySQL和mariadb区别_mariadb")
##### 1.安装MySQL
```shell
yum -y install mysql mysql-server mysql-devel
```
2.启动服务
```shell
service mariadb start
# 设置开启启动
systemctl enable mariadb
```
##### 3.MariaDB相关配置
```shell
mysql_secure_installation
```
- 进入设置
```shell
Enter current password for root (enter for none): # 初次运行直接回车
设置密码
Set root password? [Y/n] # 是否设置root用户密码,输入y(必须的)
New password: #设置root用户密码
Re-enter new password: # 再次输入密码
# 其他配置
Remove anonymous users? [Y/n] # 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] # 是否禁止root远程登录,回车
Remove test database and access to it? [Y/n] #是否删除test数据库,回车
Reload privilege tables now? [Y/n] #是否重新加载权限表,回车
```
- 初始化完成,测试登录MariaDB
```shell
mysql -uroot -p
# 回车后,输入上面设置的秘密
Enter password:
```
- 成功,则显示如下信息并进入数据库
```shell
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2374
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help; ' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
```
> 退出命令:exit,或者直接Ctrl+c
- 4.MariaDB字符集配置
- 修改/etc/my.cnf 文件
```shell
vi /etc/my.cnf
```
> 在[mysqld]标签下添加
```shell
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
```
- 修改/etc/my.cnf.d/client.cnf 文件
```shell
vi /etc/my.cnf.d/client.cnf
```
> 在[client]中添加
```shell
[client]
default-character-set=utf8
```
- 修改 /etc/my.cnf.d/mysql-clients.cnf 文件
```shell
vi /etc/my.cnf.d/mysql-clients.cnf
```
> 在[mysql]中添加
```shell
[mysql]
default-character-set=utf8
```
> 全部修改保存后,重启MariaDB,配置才会生效
```shell
service mariadb restart
```
> 可登录MariaDB数据库中查看字符集
```shell
mysql> show variables like "%character%"; show variables like "%collation%";
```
至此,LNMP服务器环境基本上已经搭建完成了(如果顺利的话),第一次尝试,难免遇到各种坑,其实遇到坑是好事,我们可以从中吸取更多的经验,找到各种解决问题的办法,这样我们才能更加从容的面对日后所遇到的问题。

    推荐阅读