搭建环境:

   操作系统:CentOS 6.7

   nginx软件:nginx-1.8.1.tar.gz

   mysql软件:mysql-5.5.51-linux2.6-x86_64.tar.gz

   PHP软件:php-5.6.11.tar.gz 

一、LNMP安装前环境准备

1、设置linux防火墙

1)在iptables上开放80,3306,443端口

[root@db ~]# vim /etc/sysconfig/iptables

# Firewall configuration written by system-config-firewall

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

[root@db ~]# service iptables restart-----重启iptables服务

2)关闭selinux防火墙

[root@db ~]# vim /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disabled

# SELINUXTYPE= can take one of these two values:

#     targeted - Targeted processes are protected,

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

[root@db ~]#setenforc 0

3)安装依赖包

[root@db ~]#yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers make  libtool* git tree bison pcre-devel perl gd gd-devel

4)编译安装其他软件

A:安装libiconv软件(该软件支持编码转换为函数)

[root@db software]# tar xf libiconv-1.14.tar.gz 

[root@db libiconv-1.14]# ./configure --prefix=/usr/local 

[root@db libiconv-1.14]# make && make install

[root@db libiconv-1.14]# echo $?

0

(显示0,表示软件编译安装成功了)

B:安装libmcrypt  (加密算法扩展库,支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法)

[root@db software]# tar xf libmcrypt-2.5.7.tar.gz 

[root@db software]# cd libmcrypt-2.5.7

[root@db libmcrypt-2.5.7]# ./configure 

[root@db libmcrypt-2.5.7]# cd libltdl/

[root@db libltdl]# ./configure --enable-ltdl-install

[root@db libltdl]# make && make install

C: 安装mhash(mhash的可以用于创建校验数值,消息摘要,消息认证码,以及无需原文的关键信息保存

[root@db software]# tar xf mhash-0.9.9.9.tar.gz 

[root@db software]# cd mhash-0.9.9.9

[root@db mhash-0.9.9.9]# ./configure && make && make install

D:安装mcript(mcrypt 是 php 里面重要的加密支持扩展库,Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。)

[root@db software]# tar xf mcrypt-2.6.8.tar.gz 

[root@db software]# cd mcrypt-2.6.8

[root@db mcrypt-2.6.8]# export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

[root@db mcrypt-2.6.8]# ./configure && make && make install

E:增加库文件

[root@db software]# ln
 
-s 
/usr/local/lib/libmcrypt
.la 
/usr/lib/libmcrypt
.la 
[root@db software]# ln
 
-s 
/usr/local/lib/libmcrypt
.so 
/usr/lib/libmcrypt
.so 
[root@db software]# ln
 
-s 
/usr/local/lib/libmcrypt
.so.4 
/usr/lib/libmcrypt
.so.4 
[root@db software]# ln
 
-s 
/usr/local/lib/libmcrypt
.so.4.4.8 
/usr/lib/libmcrypt
.so.4.4.8 
[root@db software]# ln
 
-s 
/usr/local/lib/libmhash
.a 
/usr/lib/libmhash
.a 
[root@db software]# ln
 
-s 
/usr/local/lib/libmhash
.la 
/usr/lib/libmhash
.la 
[root@db software]# ln
 
-s 
/usr/local/lib/libmhash
.so 
/usr/lib/libmhash
.so 
[root@db software]# ln
 
-s 
/usr/local/lib/libmhash
.so.2 
/usr/lib/libmhash
.so.2 
[root@db software]# ln
 
-s 
/usr/local/lib/libmhash
.so.2.0.1 
/usr/lib/libmhash
.so.2.0.1 
[root@db software]# ln
 
-s 
/usr/local/bin/libmcrypt-config
 
/usr/bin/libmcrypt-config

F:安装cmake (MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具)

[root@db ~]# yum -y install cmake

二、安装mysql数据软件

1)创建mysql用户

[root@db software]#groupadd -r mysql

[root@db software]# useradd -r -g mysql mysql -s /sbin/nologin

2)创建mysql的数据目录

[root@db software]# mkdir -p /data/mysqldb

3)安装mysql

[root@db ~]# cd /software/

[root@db software]# tar xf mysql-5.5.51-linux2.6-x86_64.tar.gz -C /usr/local/

[root@db software]# cd /usr/local/

[root@db local]# ll

total 48

drwxr-xr-x.  2 root root 4096 Aug 18 11:46 bin

drwxr-xr-x.  2 root root 4096 Sep 23  2011 etc

drwxr-xr-x.  2 root root 4096 Sep 23  2011 games

drwxr-xr-x.  3 root root 4096 Aug 18 11:45 include

drwxr-xr-x.  3 root root 4096 Aug 18 11:45 lib

drwxr-xr-x.  2 root root 4096 Sep 23  2011 lib64

drwxr-xr-x.  2 root root 4096 Sep 23  2011 libexec

drwxr-xr-x.  3 root root 4096 Aug 18 11:41 man

drwxr-xr-x. 13 root root 4096 Aug 18 11:55 mysql-5.5.51-linux2.6-x86_64

drwxr-xr-x.  2 root root 4096 Sep 23  2011 sbin

drwxr-xr-x.  8 root root 4096 Aug 18 11:41 share

drwxr-xr-x.  2 root root 4096 Sep 23  2011 src

[root@db local]# ln -sv mysql-5.5.51-linux2.6-x86_64/ mysql

`mysql' -> `mysql-5.5.51-linux2.6-x86_64/'

修改mysql目录权限

[root@db mysql]# chown -R mysql:mysql .

[root@db mysql]# cd /data/mysqldb/

[root@db mysqldb]# chown -R mysql:mysql .

初始化mysql数据库

[root@db ~]# cd /usr/local/mysql

[root@db mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldb

编辑mysql配置文件/etc/my.cnf

[mysqld]

datadir=/data/mysqldb

socket=/tmp/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

复制MySQL启动文件及其命令加入PATH

[root@db mysql]# cp support-files/mysql.server  /etc/init.d/mysqld

[root@db mysql]# chmod +x /etc/init.d/mysqld 

[root@db mysql]# chkconfig mysqld on

[root@db mysql]# vim /etc/profile.d/mysqld.sh

 
PATH=
/usr/local/mysql/bin
:
/usr/local/mysql/lib
:$PATH

[root@db mysql]# source /etc/profile.d/mysql.sh

启动mysql服务器

[root@db etc]# service mysqld start

Starting MySQL..                                           [  OK  ]

修改root用户的密码及授权普通用户test可以远程连接到mysql数据库

mysql> grant all on *.* to 'root'@'localhost' identified by 'test123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to 'test'@'%' identified by 'test1234';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec