CentOS6.8搭建LNMP环境

本文介绍如何在CentOS 6.8上安装配置Nginx作为Web服务器,MySQL作为数据库服务器及PHP作为脚本解释器,并实现三者的整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux

选择的是阿里云的服务器,系统版本是CentOS 6.8。先在安全组里面设置相应安全组规则。如80,3306端口。


一、安装Nginx

1.安装相关依赖


# gcc:
    yum install gcc-c++

#PCRE:
    yum install -y pcre pcre-devel

#zlib:
    yum install -y zlib zlib-devel

#OpenSSL:
    yum install -y openssl openssl-devel

2.下载Nginx

  • 切换到/usr/local/src目录下
  • 下载
cd /usr/local/src
wget http://nginx.org/download/nginx-1.10.3.tar.gz
  • src目录下多了一个压缩文件

  • 解压

     tar -zxvf nginx-1.10.3.tar.gz
  • 编译
    • 切换到/usr/local/src/nginx-1.10.3.tar.gz 目录下
    • 编译安装
./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
make && make install
  • 安装目录
whereis nginx
#nginx: /usr/sbin/nginx /etc/nginx

3.创建启动脚本

vim /etc/init.d/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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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' -`
   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
  • 改变nginx脚本权限
chmod 755 /etc/init.d/nginx
  • 添加至service管理服务并设置开机启动
chkconfig --add nginx
service nginx reload
chkconfig nginx on

4.开启nginx

service nginx start

5.查看nginx进程

ps aux |grep nginx

安装MYSQL

1.安装cmake扩展

wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar -zvxf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./configure && gmake && gmake install

2.安装MYSQL

groupadd mysql
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置MySQL数据库存放目录权限
wget http://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35.tar.gz
tar -zxvf mysql-5.6.35.tar.gz
cd mysql-5.6.35
#这里cmake会报错,需要安装扩展以及删除CMakeCache.txt文件
yum install appropriate
rm CMakeCache.txt
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
make
make install
rm -rf /etc/my.cnf #删除系统默认的配置文件(如果默认没有就不用删除)
cd /usr/local/mysql #进入MySQL安装目录
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql #生成mysql系统数据库
ln -s /usr/local/mysql/my.cnf /etc/my.cnf #添加到/etc目录的软连接
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vi /etc/rc.d/init.d/mysqld #编辑
basedir=/usr/local/mysql #MySQL程序安装路径
datadir=/data/mysql #MySQl数据库存放目录
:wq! #保存退出
service mysqld start #启动
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
:wq! #保存退出
source /etc/profile #使配置立刻生效
#下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址。
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
#修改密码
mysql -uroot -p  #登录mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; #配置root用户可以远程登录
use mysql;
update user set password=PASSWORD("root") where user="root";
flush privileges;

安装PHP5.6

1.编译、安装

groupadd www
useradd -g www mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
wget http://cn2.php.NET/distributions/php-5.6.2.tar.gz
tar zvxf php-5.6.2.tar.gz -C /usr/src/
yum -y install gd libxml2-devel libjpeg-devel libpng-devel
cd /usr/src/php-5.6.2/
./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql/ --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib
make && make install

2.配置

cp php.ini-production /usr/local/php5/php.ini #复制php配置文件到安装目录
rm -rf /etc/php.ini #删除系统自带配置文件
ln -s /usr/local/php5/php.ini /etc/php.ini #添加软链接到 /etc目录
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
ln -s /usr/local/php5/etc/php-fpm.conf /etc/php-fpm.conf #添加软连接到 /etc目录
vi /usr/local/php5/etc/php-fpm.conf #编辑
user = www #设置php-fpm运行账号为www
group = www #设置php-fpm运行组为www
pid = run/php-fpm.pid #取消前面的分号
:wq! #保存退出

#设置 php-fpm开机启动
cp /usr/local/src/php-5.6.2/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限
chkconfig php-fpm on #设置开机启动
vi /usr/local/php5/php.ini #编辑配置文件
date.timezone = PRC #设置时区
expose_php = Off #禁止显示php版本的信息
short_open_tag = ON #支持php短标签
#还有一些其它信息这里不列出来了
:wq! #保存退出

3.修改nginx配置

  • 注意点
vi /etc/nginx/nginx.conf  #配置修改
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
  • nginx.conf配置 include /etc/nginx/vhosts/*.conf; 在vhosts下面配置server。
user  www www;
worker_processes  2;

#error_log  /usr/local/nginx/logs/error.log;
#pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

http {
        include       mime.types;
        default_type  application/octet-stream;

        #charset  gb2312;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 8m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        #limit_zone  crawler  $binary_remote_addr  10m;
        log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
        include /etc/nginx/vhosts/*.conf;
}

4.启动php-fpm,nginx服务

/etc/init.d/php-fpm start
/etc/init.d/nginx reload
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值