Centos6.5 LNMP 环境搭建

环境介绍

查看服务器版本:

cat /etc/redhat-release
[root@localhost nginx-1.14.0]# yum install patch make gcc gcc-c++ automake cmake autoconf kernel-devel libtool libtool-libs libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel glibc glibc-devel glibc-headers glibc-static glibc-utils openssl openssl-devel crypto-utils gettext gettext-devel ncurses ncurses-devel gmp-devel aspell aspell-devel perl-IO-Compress-Base perl-HTML-Parser perl-ExtUtils-MakeMaker perl-libwww-perl perl-Pod-Escapes perl-Module-Pluggable perl-libs perl-ExtUtils-FindFunctions perl-Compress-Raw-Zlib perl-IO-Compress-Zlib perl-Test-Harness perl-ExtUtils-ParseXS perl-Newt perl-HTML-Tagset perl-URI perl-Convert-ASN1 perl-ExtUtils-Embed perl-Pod-Simple perl-ExtUtils-DynaGlue perl-Compress-Zlib perl-devel perl-DBI gd gd-devel curl libcurl libcurl-devel readline readline-devel

nginx

1.安装依赖包

yum  -y install zlib-devel pcre-devel openssl-devel
# 下载nginx
[root@localhost /]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@localhost src]# tar -zxvf nginx-1.14.0.tar.gz

2.编译nginx

cd nginx-1.0.13
 ./configure --prefix=/usr/local/nginx\   # 指定安装目录为/usr/local/nginx
--with-openssl=/usr/include/openssl\  # 启用ssl
--with-pcre\                          # 启用正规表达式
--with-http_stub_status_module        # 安装可以查看nginx状态的程序

make && make install

3.启动nginx

/usr/local/nginx/sbin/nginx
nginx安装成功

4.nginx 启动脚本

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

mysql

创建mysql用户和组

[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -g mysql -s /sbin/nologin mysql

下载mariadb

1,安装特定的开发包
[root@localhost src]# yum -y install readline-devel zlib-devel openssl-devel

2.编译及安装

[root@localhost src]# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz
[root@localhost src]# tar zxvf mysql-5.6.17.tar.gz
[root@localhost src]# cd mysql-5.6.17
[root@localhost src]# 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 \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system
[root@localhost src]# make && make install

3.修改/usr/local/mysql权限

[root@localhost local]# chmod +w /usr/local/mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql

在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索”$basedir/my.cnf” 就是安装目录下 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置! 注意:在CentOS 6.x版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字。 如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。 由于我们已经卸载了最小安装完成后的mysq库所以,就没必要操作了

  1. mysql初始化
[root@localhost mysql-5.6.17]# cd support-files/
# 执行初始化脚本
[root@localhost mysql-5.6.17]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#拷贝脚本
[root@localhost mysql-5.6.17]#  cp support-files/mysql.server /etc/init.d/mysqld
#赋予权限
[root@localhost mysql-5.6.17]#  chmod +x /etc/init.d/mysqld
  1. 设置开机启动
[root@localhost mysql-5.6.17]# chkconfig mysqld on
启动MySQL
[root@localhost mysql-5.6.17]# service mysqld start
或者
[root@localhost mysql-5.6.17]# /etc/init.d/mysql start

6.设置mysql PATH

修改/etc/profile文件
[root@localhost mysql-5.6.17]# vi /etc/profile
在文件末尾添加
PATH=/usr/local/mysql/bin:$PATH
export PATH
[root@localhost mysql-5.6.17]# source /etc/profile
设置密码
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
  1. 远程登录
# 查看用户
select user,host from mysql.user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
# 删除不必要的用户
drop user ""@localhost;
drop user ""@localhost.localdomain; 
drop user [email protected]; 
drop user root@'::1';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '你的密码' WITH GRANT OPTION; 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'c65mini.localdomain' IDENTIFIED BY '你的密码' WITH GRANT OPTION;

php

创建www用户组和用户

[root@localhost src]# groupadd www
[root@localhost src]# useradd -g www -s /sbin/nologin www

1.安装PHP前首先要安装几个源码包依赖:libmcrypt mhash mcrypt

wget https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2
tar -jxvf libmcrypt-2.5.8.tar.bz2   # 这个包是bz2的  使用-j参数解压
cd libmcrypt-2.5.8
./configure
make
make install
####################################################
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make
make install
# 这两个包安装完成后要把动态链接库做一个软连接到/usr/lib,以为接下来的mcrypt依赖于这两个包
ln -s /usr/local/lib/libmcrypt* /usr/lib
ln -s /usr/local/lib/libmhash.* /usr/lib/
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
###########################################################
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
出现onfigure: error: *** libmcrypt was not found
export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH
make
make install

2 下载php

wget http://cn2.php.net/distributions/php-7.2.5.tar.gz
tar -zxvf php-7.2.5.tar.gz
cd php-7.2.5
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make && make install
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 启动php-fpm
cp /usr/local/php/sbin/php-fpm /etc/init.d/php-fpm
/etc/init.d/php-fpm retart

到这里lnmp环境已经完成了

扩展

你可能感兴趣的:(Centos6.5 LNMP 环境搭建)