linux单机LNMP环境搭建

一.系统环境准备。

1.系统环境:CentOS_6.5,32位

[root@localhost ~]# cat /etc/redhat-release      

CentOS release 6.5 (Final)

[root@localhost ~]# uname -r

2.6.32-431.el6.i686

2.软件版本

 nginx-1.8.1

 php-5.6.13

mysql-5.1.72

3.关闭防火墙iptables和selinux

3.1关闭防火墙iptables

[root@localhost tools]# chkconfig --level 2345iptables off

[root@localhost tools]# chkconfig --listiptables

iptables       0:off   1:off   2:off  3:off   4:off   5:off  6:off

[root@localhost tools]# service iptables stop

[root@localhost tools]# service iptablesstatus

iptables: Firewall is not running.

3.2关闭selinux

[root@localhost tools]# vim/etc/selinux/config        #修改配置如下图所示

linux单机LNMP环境搭建_mysql

4.新建软件管理文件夹及上传相关软件

4.1创建软件管理文件夹

[root@localhost tools]# mkdir -p/home/jeff/tools/

[root@localhost tools]# ll /home/jeff/

total 4

drwxr-xr-x. 5 root root 4096 May 16 10:16tools

4.2上传相关软件

[root@localhost /]# cd /home/jeff/tools/

利用rz命令上传相关软件。如果无法使用该命令,请安装lrzsz软件。

二、安装nginx相关软件

2.1安装nginx相关工具包;

[root@localhost ~]#  yum -y install gcc gcc-c++ pcre pcre-developenssl openssl-devel

2.2 创建nginx组和用户,并安装nginx软件

2.2.1创建nginx组和用户

[root@localhost ~]# groupadd -g 800 nginx         #创建nginx用户组

[root@localhost ~]# useradd -u 800 -g nginx -M-s /sbin/nologin nginx       #创建nginx用户

[root@localhost ~]# tail -1 /etc/passwd              #检查nginx用户创建情况

nginx:x:800:800::/home/nginx:/sbin/nologin

[root@localhost ~]# tail -1 /etc/group               #检查nginx组创建情况

nginx:x:800:

2.2.2安装nginx软件

2.2.2.1解压nginx软件

[root@localhost ~]# cd /home/jeff/tools/

[root@localhost tools]# tar -zxvfnginx-1.8.1.tar.gz

[root@localhost tools]# ll

total 820

drwxr-xr-x. 9 1001 1001   4096 May 29 13:31 nginx-1.8.1

-rw-r--r--. 1 root root 833473 May 20 22:38nginx-1.8.1.tar.gz

2.2.2.2进入nginx软件目录并进行配置

 [root@localhost tools]# cd nginx-1.8.1

[root@localhost nginx-1.8.1]# ./configure \

--user=nginx \

> --user=nginx \

> --group=nginx \

> --prefix=/usr/local/nginx-1.8.1 \

> --with-http_stub_status_module \

> --with-http_ssl_module \

> --with-http_realip_module

2.2.2.3编译安装

[root@localhost nginx-1.8.1]# make & makeinstall

创建软连接

[root@localhost nginx-1.8.1]# ln -s/usr/local/nginx-1.8.1 /usr/local/nginx

2.3启动及检查nginx安装情况

2.3.1启动nginx

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx     #启动nginx

2.3.2检查nginx启动情况

[root@localhost nginx-1.8.1]# ps -ef | grepnginx            #检查nginx进程启动情况

root     4959     1  0 15:29 ?        00:00:00 nginx: master process/usr/local/nginx/sbin/nginx

nginx    4960  4959  0 15:29 ?        00:00:00 nginx: worker process     

[root@localhost nginx-1.8.1]# netstat -lntp |grep 80         #检查nginx监听端口

tcp       0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4959/nginx

[root@localhost nginx-1.8.1]# lsof -i tcp:80                #检查80端口对应的服务

COMMAND PID  USER   FD  TYPE DEVICE SIZE/OFF NODE NAME

nginx  4959  root    9u IPv4  52360      0t0 TCP *:http (LISTEN)

nginx  4960 nginx    9u  IPv4 52360      0t0  TCP *:http (LISTEN)

用浏览器检查:

http://localhost,如出现如下页面,则说明nginx安装成功。

linux单机LNMP环境搭建_nginx_02

三、安装mysql相关软件

3.1安装编译源码所需的工具和库

[root@localhost tools]# yum -y install gccgcc-c++ ncurses-devel perl

3.2安装cmake

[root@localhost tools]# tar -zxvfcmake-2.8.10.2.tar.gz

[root@localhost tools]# cd cmake-2.8.10.2

[root@localhost cmake-2.8.10.2]# ./bootstrap

[root@localhost cmake-2.8.10.2]# make

3.3新建用户及组

[root@localhost cmake-2.8.10.2]# groupadd -g 902mysql        #创建mysql用户组

[root@localhost cmake-2.8.10.2]# useradd -u 902-g mysql -M -s /sbin/nologin mysql     #创建mysql用户

[root@localhost cmake-2.8.10.2]# cat/etc/group | grep mysql     #检查mysql组创建情况

mysql:x:902:

[root@localhost cmake-2.8.10.2]# cat/etc/passwd | grep mysql    #检查mysql用户创建情况

mysql:x:902:902::/home/mysql:/sbin/nologin

#-s /sbin/nologin表示禁止该用户登录,加强安全。

#-g mysql指定属于mysql组。

#-M表示不创建用户家目录。

3.4解压mysql

[root@localhost tools]# tar -zxvfmysql-5.6.30.tar.gz

3.5进入mysql软件目录并进行编译安装

[root@localhost tools]# cd mysql-5.6.30

[root@localhost mysql-5.6.30]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.30\

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.30/tmp/mysqld.sock\

-DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \

-DWITH_FAST_MUTEXES=1 \

-DWITH_ZLIB=bundled \

-DWITH_READLINE=1 \

-DWITH_EMBEDDED_SERVER=1 \

-DWITH_DEBUG=0

[root@localhost mysql-5.6.30]# make

[root@localhost mysql-5.6.30]# make install

3.6设置权限及设置软连接

[root@localhost mysql-5.6.30]# chown -Rmysql:mysql /usr/local/mysql-5.6.30

[root@localhost mysql-5.6.30]# ln -s/usr/local/mysql-5.6.30 /usr/local/mysql

3.7.初始化数据库及配置启动文件

[root@localhostmysql-5.6.30]# /usr/local/mysql/scripts/mysql_install_db--user=mysql --datadir=/usr/local/mysql/data

#初始化数据库

[root@localhostmysql-5.6.30]# cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld  #配置启动文件

[root@localhostmysql-5.6.30]# chmod 700/etc/init.d/mysqld      #使配置文件可执行

3.8设置全局路径

[root@localhostmysql-5.6.30]# echo 'exportPATH=$PATH:/usr/local/mysql/bin' >> /etc/profile    #配置全局路径

[root@localhostmysql-5.6.30]# source/etc/profile       #使全局路径配置生效

3.9启动及检查启动情况

[root@bogon support-files]# /etc/init.d/mysqldstart      #启动mysql

Starting MySQL.........................SUCCESS!

[root@bogon support-files]# netstat -lntp |grep 3306    #检查mysql监听端口3306

tcp       0      0 :::3306                     :::*                        LISTEN      2550/mysqld

[root@bogon support-files]# ps -ef | grepmysql | grep -v grep     #检查mysql进程

root     2369     1  0 14:52 pts/1    00:00:00 /bin/sh/usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data--pid-file=/usr/local/mysql/data/bogon.pid

mysql    2550  2369 16 14:52 pts/1    00:00:17 /usr/local/mysql/bin/mysqld--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data--plugin-dir=/usr/local/mysql/lib/plugin --user=mysql--log-error=/usr/local/mysql/data/bogon.err--pid-file=/usr/local/mysql/data/bogon.pid--socket=/usr/local/mysql/tmp/mysqld.sock --port=3306

[root@bogon support-files]# mysql -uroot    #进入mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.30 Source distribution

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

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarks oftheir respective

owners.

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

mysql>

3.10设置开机自启动及设置mysql登录密码

1)设置开机自启动

[root@bogon support-files]# chkconfig --addmysqld    

[root@bogon support-files]# chkconfig mysqldon

[root@bogon support-files]# chkconfig --listmysqld

mysqld         0:off   1:off   2:on   3:on    4:on    5:on   6:off

2)设置mysql登录密码

[root@bogon support-files]#  mysqladmin -uroot password '123456'    #设置密码:123456

四、安装php相关软件

4.1检查及安装如下lib库

[root@bogon support-files]# rpm -qa zliblibxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devellibjpeg-devel freetype-devel libpng-devel gd-devel curl-devel     #检查lib库安装情况

[root@bogon support-files]# yum install zliblibxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devellibjpeg-devel freetype-devel libpng-devel gd-devel curl-devel –y

编译安装libiconv

安装libmcrypt

安装mhash加密扩展库

安装mcrypt加密扩展库

4.2解压php软件

[root@bogon tools]# tar -zxvfphp-5.5.32.tar.gz

[root@bogon tools]# ll

total 61908

drwxr-xr-x. 14 1001  1001    4096 Feb  2 21:36 php-5.5.32

-rw-r--r--. 1 root root  17773092 Feb 22 11:22php-5.5.32.tar.gz

4.3配置PHP

[root@bogon php-5.5.32]# ./configure \

--prefix=/usr/local/php \

--with-mysql=/usr/local/mysql \

--with-mysqli=/usr/local/mysql/bin/mysql_config\

--with-iconv-dir=/usr/local/libiconv \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--disable-rpath \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--enable-mbregex \

--enable-fpm \

--enable-mbstring \

--with-mcrypt \

--with-gd \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-zip \

--enable-soap \

--enable-short-tags \

--enable-static \

--with-xsl \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--enable-ftp

4.4编译安装

[root@bogon php-5.5.32]# make & makeinsatll

4.5配置PHP引擎配置文件php.ini及配置php服务(fastCGI)的配置文件

#配置PHP引擎配置文件php.ini

[root@bogon php-5.5.32]# cp php.ini-production/usr/local/php/lib/php.ini

#配置php服务(fastCGI)的配置文件

[root@bogon php-5.5.32]# cd/usr/local/php/etc/

[root@bogon php-5.5.32]# cpphp-fpm.conf.default php-fpm.conf

4.6启动php服务及检查php服务启动情况

启动PHP服务php-fpm,命令如下

[root@bogon etc]# /usr/local/php/sbin/php-fpm

检查php服务php-fpm的进程及启动端口的情况,命令如下

[root@bogon etc]# ps -ef | grep php-fpm | grep-v grep     #检查进程

root    26469     1  0 17:04 ?        00:00:00 php-fpm: master process(/usr/local/php/etc/php-fpm.conf)

nginx   26470 26469  0 17:04 ?        00:00:00 php-fpm: pool www          

nginx   26471 26469  0 17:04 ?        00:00:00 php-fpm: pool www

[root@bogon etc]# netstat -lntp | grep 9000              #检查监听端口

tcp       0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      26469/php-fpm       

4.7配置Nginx支持PHP程序请求访问

修改nginx配置文件nginx.conf

 location / {

     root  html/bbs;

     index  index.php index.html index.htm;

}

location ~ .*\.(php|php5)?$ {

    root/html/bbs;

   fastcgi_pass 127.0.0.1:9000;

   fastcgi_index index.php;

   include fastcgi.conf;

}

4.8检查配置文件并启动nginx

[root@bogon etc]# /usr/local/nginx/sbin/nginx-t

nginx: the configuration file/usr/local/nginx-1.8.1/conf/nginx.conf syntax is ok

nginx: configuration file/usr/local/nginx-1.8.1/conf/nginx.conf test is successful

[root@bogon etc]# /usr/local/nginx/sbin/nginx-s reload

4.9测试php环境

进入指定的默认站点目录后,编辑index.php添加:

进入默认的nginx站点目录

root@bogon html]# cd/usr/local/nginx/html/bbs

<?php

Phpinfo();

?>

#注以上代码为显示php配置信息的简单php文件代码,最好手工敲,以防出错;

#通过访问并刷新浏览器http://ip,如可以看到phpinfo信息,如下图,说明php环境配置ok;

linux单机LNMP环境搭建_nginx_03

至此LNMP单机环境搭建完成!