一、Lnmp简介:
LNMP是Linux、Nginx、MySQL(MariaDB)和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一。下面我将带领大家在CentOS6操作系统上搭建一套LNMP环境。本教程适用于CentOS 6.x/7.X版本,在安装LNMP环境之前,您需要先对CentOS操作系统做一些初始化的工作。
1.1 硬件环境:
1.2 软件环境
二、部署过程
2.1 源码安装nginx
[lnmp@BJ-YF-www001 ~]$ mkdir soft
[lnmp@BJ-YF-www001 ~]$ cd soft
[lnmp@BJ-YF-www001 soft]$ rz
[lnmp@BJ-YF-www001 soft]$ tar zxvf nginx_soft.zip
[lnmp@BJ-YF-www001 soft]$ sudo su -
#安装依赖软件包
yum install -y libxml2 curl-devel libtool-ltdl-devel.x86_64
yum install -y libxml2-* libjpeg* openssl-devel libpng-devel
yum install -y freetype-devel libevent-devel
#安装nginx
[root@BJ-YF-www001 ~]# useradd www -M -s /sbin/nologin
[root@BJ-YF-www001 ~]# mkdir /application
[root@BJ-YF-www001 ~]# cd /home/lnmp/soft/nginx_soft
[root@BJ-YF-www001 nginx_soft]# tar xvf pcre-8.30.tar.bz2
[root@BJ-YF-www001 nginx_soft]#cd pcre-8.30
[root@BJ-YF-www001 pcre-8.30]# ./configure
[root@BJ-YF-www001 pcre-8.30]# make
[root@BJ-YF-www001 pcre-8.30]# make install
[root@BJ-YF-www001 pcre-8.30]# cd ../
[root@BJ-YF-www001 nginx_soft]#tar xvf nginx-1.10.2.tar.gz
[root@BJ-YF-www001 nginx_soft]#cd nginx-1.10.2
[root@BJ-YF-www001 nginx-1.10.2]#./configure \
--prefix=/application/nginx-1.10.2 \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_gunzip_module \
--with-pcre=../pcre-8.30
[root@BJ-YF-www001 nginx-1.10.2]#make
[root@BJ-YF-www001 nginx-1.10.2]#make install
#创建nginx1.10.2的软链接
ln -s /application/nginx-1.10.2 /application/nginx
ls -l /application
#检查并启动nginx
#检查配置文件,提示如下则正常
[lnmp@BJ-YF-www001 ~]$ /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
/application/nginx/sbin/nginx #启动nginx
ps -ef |grep nginx --color #检查进程
netstat -lnt |grep 80 --color #检查端口
#打开浏览器测试
2.2源码安装mysql
#安装编译代码需要的包包
[root@BJ-YF-www001 nginx_soft]# yum -y install make \
gcc-c++ cmake ncurses-devel
#安装mysql
[root@BJ-YF-www001 nginx_soft]# mkdir /data
[root@BJ-YF-www001 nginx_soft]# tar zxvf mysql-5.6.16.tar.gz
[root@BJ-YF-www001 nginx_soft]# cd mysql-5.6.16
[root@BJ-YF-www001 mysql-5.6.16]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data \
-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
#整个过程需要20分钟左右……漫长的等待
[root@BJ-YF-www001 mysql-5.6.16]# make && make install
#配置mysql
[root@BJ-YF-www001 mysql-5.6.16]# groupadd mysql
[root@BJ-YF-www001 mysql-5.6.16]# useradd -g mysql mysql
[root@BJ-YF-www001 mysql-5.6.16]# chown -R mysql:mysql /usr/local/mysql
[root@BJ-YF-www001 mysql-5.6.16]# chown -R mysql:mysql /data
#进入安装路径,执行初始化配置脚本,创建系统自带的数据库和表
[cd /usr/local/mysql
scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data --user=mysql
#添加服务,拷贝服务脚本到init.d目录,并设置开机启动
cp support-files/mysql.server /etc/init.d/mysql
chkconfig mysql on
#检查/etc/my.cnf配置文件,如存在,将其移动到/tmp/,重命名为my_bak.cnf_
[root@BJ-YF-www001 mysql]# ls /etc/my.cnf
[root@BJ-YF-www001 mysql]# mv /etc/my.cnf /tmp/my_bak.cnf_
#启动myql
#启动MySQL
[root@BJ-YF-www001 mysql]# service mysql start
#配置用户 , 修改/etc/profile文件,在文件末尾添加
[root@BJ-YF-www001 mysql]# vim /etc/profile
#add by lnmp 2014-10-17
PATH=/usr/local/mysql/bin:$PATH
export PATH
#关闭文件,让配置立即生效
source /etc/profile
#执行下面的命令修改root密码
[root@BJ-YF-www001 mysql]# mysql -uroot
mysql> SET PASSWORD = PASSWORD('test001');
mysql> quit;
#mysql-5.6.16 安装完毕
[root@BJ-YF-www001 mysql]# mysql -uroot
mysql> SET PASSWORD = PASSWORD('test001');
mysql> quit;
2.3 源码安装php
[root@BJ-YF-www001 mysql]# cd /home/lnmp/soft/nginx_soft
[root@BJ-YF-www001 nginx_soft]# tar zxvf libmcrypt-2.5.7.tar.gz
[root@BJ-YF-www001 nginx_soft]# cd libmcrypt-2.5.7
[root@BJ-YF-www001 libmcrypt-2.5.7]# ./configure \
--prefix=/usr/local/libmcrypt
[root@BJ-YF-www001 libmcrypt-2.5.7]# make && make install
[root@BJ-YF-www001 libmcrypt-2.5.7]# cd ../
[root@BJ-YF-www001 nginx_soft]# tar zxvf php-5.6.36.tar.gz
[root@BJ-YF-www001 nginx_soft]# cd php-5.6.36
[root@BJ-YF-www001 php-5.6.36]# ./configure \
--prefix=/application/php-5.6.36 \
--with-config-file-path=/application/php-5.6.36/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mbstring \
--with-curl \
--with-mcrypt=/usr/local/libmcrypt \
--with-mysql=/usr/local/mysql \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-xmlrpc \
--with-openssl \
--with-zlib \
--with-freetype-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--enable-bcmath \
--enable-short-tags \
--enable-sockets \
--enable-soap \
--enable-static \
--enable-gd-native-ttf
[root@BJ-YF-www001 php-5.5.18]# make && make install
ln -s /application/php-5.6.36 /application/php
cp php.ini-production /application/php-5.6.36/etc/
cp ../php-fpm.conf /application/php/etc/
#生成测试页面
[root@BJ-YF-www001 php-5.5.18]# /application/php/sbin/php-fpm
[root@BJ-YF-www001 php-5.5.18]# cd /application/nginx/conf
[root@BJ-YF-www001 conf]# cp nginx.conf nginx.conf_xk_bak
/bin/cp /home/lnmp/soft/nginx_soft/nginx.conf .
mkdir -p /export/data/www
mkdir -p /export/servers/nginx-1.10.2/logs/
mkdir vhost && cd vhost
#拷贝配置文件
cp /home/lnmp/soft/nginx_soft/www.conf .
cd /export/data/www
#拷贝测试页面
cp /home/lnmp/soft/nginx_soft/test.php .
#检查nginx配置文件
/application/nginx/sbin/nginx –t
#安全关闭nginx
/application/nginx/sbin/nginx -s quit
#检查nginx进程是否退出
ps -ef |grep nginx|grep -v grep
#启动nginx
/application/nginx/sbin/nginx
#打开浏览器,输入http://IP/test.php
至此,Lnmp 基础环境部署完毕。
--End--
领取专属 10元无门槛券
私享最新 技术干货