在CentOS编译安装LNMP环境

一、安装mysql-5.6.10#

1、安装编译代码需要的包

yum -y install make gcc-c++ cmake bison-devel  ncurses-devel
yum -y install  gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake

2、添加mysql用户和用户组

[root@localhost~]# groupadd mysql
[root@localhost~]# useradd -g mysql mysql

3、编译安装

[root@localhost mysql-5.6.10]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

[root@localhost~]# make && make install

4、配置MySQL

修改/usr/local/mysql权限

[root@localhost~]# chown -R mysql:mysql /usr/local/mysql

初始化MySQL配置

[root@localhost~]# cd /usr/local/mysql/scripts
[root@localhost~]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

启动MySQL,添加服务,拷贝服务脚本到init.d目录,并设置开机启动

[root@localhost~]# cd /usr/local/mysql/support-files

加入启动项

[root@localhost~]# cp mysql.server /etc/rc.d/init.d/mysql

使用默认配置文件

[root@localhost~]# cp my-default.cnf /etc/my.cnf

开机启动

[root@localhost~]# chkconfig mysql on

设置root密码

[root@localhost~]# mysql -u root
mysql> set password for root@localhost = password('newpassword');

注:###

如果安装过程中遇见以下问题
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
解决方法:
(1)有可能是没启动mysql,

[root@localhost~]# /usr/local/mysql/support-files/mysql.server start

启动数据库,在进入数据库,mysql -uroot

(2)vim /usr/local/mysql/my.cnf在[mysqld]下添加

datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

重启mysql

二、编译安装PHP-5.4.39#

1、先安装各种依赖

[root@localhost~]# yum -y install gcc gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel gd openssl openssl-devel openldap-devel
[root@localhost~]# yum -y install bzip2 bzip2-devel curl curl-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel 

2、安装PHP
解压tar包

[root@localhost~]# tar -xvf php-5.4.39.tar

进入解压好的文件夹中编译安装

[root@localhost~]# cd php-5.4.39
[root@localhost php-5.4.39]# ./configure --prefix=/usr/local/php \
--with-libxml-dir=/usr/include/libxml2 \
--with-config-file-path=/usr/local/php/etc/ \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-gd \
--enable-gd-native-ttf \
--with-zlib \
--with-mcrypt \
--with-pdo-mysql=/usr/local/mysql \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-wddx \
--enable-zip \
--with-xmlrpc \
--enable-fpm \
--enable-mbstring=all \
--with-zlib-dir \
--with-bz2 \
--with-curl \
--enable-exif \
--enable-ftp \
--with-jpeg-dir=/usr/lib64 \
--with-png-dir=/usr/lib64 \
--with-freetype-dir=/usr/lib64 \
--with-iconv \
--enable-xml \
--enable-bcmath \
--enable-sysvsem \
--enable-inline-optimization \
--with-curlwrappers \
--enable-mbregex \
--with-openssl \
--enable-pcntl \
--with-gettext \
--enable-session

[root@localhost php-5.4.39]# make && make install

php全局执行命令

[root@localhost ~]# ln -s /usr/local/php/bin/php /usr/bin/

php的配置文件(注意:php配置文件来源在源码包里面)

[root@localhost ~]# cp /Downloads/php-5.4.39/php.ini-development /usr/local/php/lib/php.ini

php-fpm的配置文件,暂时先不改内容:

[root@localhost ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

三、安装nginx#

1、安装之前先安装下各种依赖

[root@localhost ~]# yum  -y install zlib-devel pcre-devel openssl-devel gcc
[root@localhost ~]# cd /usr/local
[root@localhost ~]# tar -xvf nginx-1.6.2.tar
[root@localhost nginx-1.6.2]# cd nginx-1.6.2
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module

--prefix=/usr/local/nginx的目录是源码的目录不是安装的目录

[root@localhost nginx-1.6.2]# make && make install

2、配置nginx,编辑nginx配置文件

[root@localhost nginx-1.6.2]# vim /usr/local/nginx/conf/nginx.conf

主要修改一下内容:

server {
  listen 80;
  server_name localhost;    #这里随意起一个域名,然后在访问的机器上配置下自己的hosts

  location / {
    root /data/www;    #网站的目录
    index index.php index.html index.htm;
  }

  location ~ \.php$ {
    root /data/www;
    fastcgi_pass 127.0.0.1:9000;    # 这个9000端口就是php-fpm的使用端口
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;        #这个地方注意下$document_root...,这是网站的根目录,要绝对路径,如/usr/local/nginx/html
    include fastcgi_params;
  }
}

3、开启nginx

/usr/local/nginx/sbin/nginx

开启php-fpm

/usr/local/php/sbin/php-fpm

4、测试环境是否成功
(1)在地址栏输入localhost或者127.0.0.1,若出现如下图说明nginx安装成功了。

Paste_Image.png

(2)访问验证
在根目录随意创建一个php文件

[root@localhost www]# vim phpinfo.php

写上以下内容,测试是否成功

<?php
    phpinfo();
?>

然后从地址栏输入localhost/phpinfo.php访问,出现下图

Paste_Image.png

其他主机访问服务器访问不了可能是防火墙没关闭

service iptables stop  //关闭防火墙

或者是selinux问题

setenforce 0    #关闭selinux
setenforce 1    #开启selinux

推荐阅读更多精彩内容