lnmp环境搭建

linux 2018-03-27 浏览(3478 评论(0

1、关闭防火墙

service iptables stop

2、关闭selinux

setenforce 0

3、安装路径

1.软件源代码包存放位置:/lnmp/src
2.源码包编译安装位置:/usr/local/软件名
3.数据库数据文件存储路径:/data/mysql

4、安装编译工具及库文件,使用centos yum命令一键安装

yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ cmake gtk+-devel zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* cpp glibc libgomp libstdc++-devel keyutils-libs-devel libarchive libsepol-devel libselinux-devel krb5-devel libXpm* freetype freetype-devel freetype* fontconfig fontconfig-devel libjpeg* libpng* php-common php-gd gettext gettext-devel ncurses* libtool* libxml2 libxml2-devel patch policycoreutils bison

5、安装nginx

useradd www -s /sbin/nologin #创建nginx运行账户www,不允许直接登录系统

cd /lnmp/src
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

make & make install #编译和安装

/usr/local/nginx/sbin/nginx #启动nginx

设置nginx开启启动
vi /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容
==========================start=============================
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac
===========================end============================

:wq! #保存退出

chmod 775 /etc/rc.d/init.d/nginx        #赋予文件执行权限
chkconfig nginx on                      #设置开机启动
/etc/rc.d/init.d/nginx restart #重新启动Nginx
service nginx start                     #启动nginx

6、安装Mysql

mkdir -p /data/www                      #创建数据库文件保存目录
chown -R www:www /data/www/             #设置目录所有者
chmod -R 700 /data/www                  #设置目录权限

useradd mysql -s /sbin/nologin          #创建用户mysql,不允许直接登录系统
mkdir -p /var/myslq/data                #创建Mysql数据库存放目录
chown -R mysql:mysql /var/mysql/data    #设置Mysql数据库目录权限

cd /lnmp/src
tar -zxvf mysql-5.7.21
cd mysql-5.7.21

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/var/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306


如有报错,在预编译时添加相应的选项:-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost重新编译

重新编译钱,先删除 rm CMakeCache.txt,然后再cmake

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/var/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost


make && make install #编译和安装

cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf   #拷贝配置文件(注意:/etc目录下面默认有一个my.cnf,直接覆盖即可)

vim /etc/my.cnf                                          #编辑配置文件,在[mysqld]部分增加
datadir = /var/mysql/data                                #添加Mysql数据库路径

./scripts/mysql_install_db --user=mysql                  #生成mysql系统数据库

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld  #把Mysql加入系统启动



vi /etc/rc.d/init.d/mysqld                               #编辑,添加下面两个目录

    1.basedir=/usr/local/mysql                                 #MySQL程序安装路径
    2.datadir=/var/mysql/data                                  #MySQl数据库存放目录


chmod 755 /etc/init.d/mysqld                             #增加执行权限

chkconfig mysqld on                                      #加入开机启动

chown -R mysql:mysql /usr/local/mysql                    #注意将mysql用户权限加入至/usr/local/mysql

service mysqld start                                     #启动mysqld


netstat -nalp|grep "3306"                    #验证配置的3306端口是否被监听



vi /etc/profile                                          #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile                                     #使配置立即生效

mkdir /var/lib/mysql                                    #创建目录
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock         #添加软链接
mysql_secure_installation                               #设置Mysql密码,根据提示按Y 回车输入2次密码
功能同上:/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #或者直接修改密码
到此,mysql安装完成!


7、安装php

cd /lnmp/src
tar -zxvf php-5.6.34.tar.gz
cd php-5.6.34
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mysqlnd --with-mysql-sock=/usr/local/mysql/mysql.sock --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir --with-pdo-mysql=/usr/local/mysql/

make #编译
make install #安装

cd /lnmp/src/php-5.6.34
cp php.ini-production /usr/local/php/etc/php.ini #复制php配置文件到安装目录
rm -rf /etc/php.ini #删除系统自带配置文件
ln -s /usr/local/php/etc/php.ini /etc/php.ini #添加软链接

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  #拷贝模板文件为php-fpm配置文件

修改:/usr/local/php/etc/php-ftm.conf
    pid = run/php-fpm.pid #取消前面的分号
    
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

vi /usr/local/php/etc/php-fpm.d/www.conf  #编辑

user = www                                #设置php-fpm运行账号为www
group = www                               #设置php-fpm运行组为www


设置php-fpm开机启动
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm    #拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm                 #添加执行权限
chkconfig php-fpm on                              #设置开机启动
service php-fpm start                             #启动php-fpm


配置nginx支持php

vi /usr/local/nginx/conf/nginx.conf
修改/usr/local/nginx/conf/nginx.conf 配置文件,需做如下修改

user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;
root /www;
index index.php;

location ~ [^/]\.php$ {
    root         /home/www;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  #修改项
    include fastcgi_params;
}


在/home/www/中创建一个index.php文件,如果打不开,可能需要修改防火墙规则
vim /etc/sysconfig/iptables   #编辑
增加下面内容:
    -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 3306 -j DROP
    -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    
service iptables restart  #重启防火墙
service iptables save     #保存

服务器相关操作命令

service nginx restart 或者/usr/local/ngin/sbin/nginx -s reload #重启nginx
service mysqld restart #重启mysql
/usr/local/php/sbin/php-fpm #启动php-fpm
/etc/rc.d/init.d/php-fpm restart #重启php-fpm
/etc/rc.d/init.d/php-fpm stop #停止php-fpm
/etc/rc.d/init.d/php-fpm start #启动php-fpm

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^

黄信强博客