CentOS7.2 编译安装lnmp(nginx1.15.0,MariaDB10.3.9,PHP7.0.32)

检查更新:

[root@i****z ~]# yum check-update

安装gcc gcc-c++:

[root@i****z ~]# yum -y install gcc gcc-c++

安装依赖包:

[root@i****z ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
[root@i****z ~]# yum -y install libxml2 libxml2-devel libpng libpng-devel libjpeg libjpeg-devel
[root@i****z ~]# yum -y install libcurl libcurl-devel libedit libedit-devel libmcrypt libmcrypt-devel
[root@i****z ~]# yum -y install libxslt libxslt-devel
[root@i****z ~]# yum -y install freetype freetype-devel bison bison-devel
[root@i****z ~]# yum -y install mhash mhash-devel bzip2 bzip2-devel jemalloc jemalloc-devel
[root@i****z ~]# yum -y install readline readline-devel sqlite sqlite-devel
[root@i****z ~]# yum -y install epel-release unzip vsftpd

安装MariaDB

https://blog.csdn.net/qq_21326433/article/details/82794364

1、安装nginx

1.1 创建用来运行nginx的组及用户

[root@i****z ~]# groupadd nginx
[root@i****z ~]# useradd -g nginx -M nginx -s /sbin/nologin
# -g:为nginx用户指定一个组    -M:保证其不自动生成home目录

1.2 创建安装目录并进入此目录

[root@i****z ~]# mkdir /usr/local/nginx
[root@i****z ~]# cd /usr/local/nginx/

1.3 下载并解压nginx源码包 nginx下载官网

[root@i****z nginx]# wget http://nginx.org/download/nginx-1.15.0.tar.gz
[root@i****z nginx]# tar zxvf nginx-1.15.0.tar.gz

# 解压后进入文件夹
[root@i****z nginx]# cd nginx-1.15.0

1.4 执行配置命令

[root@i****z nginx-1.15.0]# ./configure --prefix=/usr/local/nginx \
--with-http_ssl_module

checking for OS
 + Linux 3.10.0-514.26.2.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
checking for gcc -pipe switch ... found
******************
省略中间数据
******************
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@i****z nginx-1.15.0]# 

1.5 完成编译工作:

[root@i****z nginx-1.15.0]# make && make install
make -f objs/Makefile
make[1]: Entering directory `/usr/local/nginx/nginx-1.15.0'
******************
省略中间数据
******************
make[1]: Leaving directory `/usr/local/nginx/nginx-1.15.0'
[root@i****z nginx-1.15.0]# 

1.6 配置nginx自启动脚本,输入以下内容,然后保存退出

[root@i****z nginx-1.15.0]# vim /etc/init.d/nginx
#!/bin/bash
# nginx     This shell script takes care of starting and stopping
#           nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
rm -f ${NGINX_PID} 2>/dev/null
if [ -s ${NGINX_PID} ]; then
echo "nginx already running"
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF}
if_no start $?
        else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
        if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac

1.7 给与用户操作权限

[root@i****z nginx-1.15.0]# chmod +x /etc/init.d/nginx

1.8 启动nginx

[root@i****z nginx-1.15.0]# service nginx start
Reloading systemd:                                         [  OK  ]
Starting nginx (via systemctl):                            [  OK  ]
[root@i****z nginx-1.15.0]# 

1.9 查看nginx状态

[root@i****z nginx-1.15.0]# systemctl status nginx.service
● nginx.service - LSB: start|stop|status|restart|configtest
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (running) since Mon 2018-09-17 15:47:20 CST; 2min 12s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 11258 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─11266 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─11267 nginx: worker process

Sep 17 15:47:20 i****z systemd[1]: Starting LSB: start|stop|status|restart|configtest...
Sep 17 15:47:20 i****z nginx[11258]: start nginx:[  OK  ]
Sep 17 15:47:20 i****z systemd[1]: Started LSB: start|stop|status|restart|configtest.
[root@i****z nginx-1.15.0]# 

1.10 设置开机启动

[root@i****z nginx-1.15.0]# chkconfig --add nginx
[root@i****z nginx-1.15.0]# chkconfig --level 2345 nginx on

 1.11 重启服务器检查是否设置成功

[root@i****z nginx-1.15.0]# reboot

[root@i****z ~]# systemctl status nginx.service
● nginx.service - LSB: start|stop|status|restart|configtest
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (running) since Mon 2018-09-17 15:53:33 CST; 34s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/nginx.service
           ├─874 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─881 nginx: worker process

Sep 17 15:53:33 i****z systemd[1]: Starting LSB: start|stop|status|restart|configtest...
Sep 17 15:53:33 i****z nginx[785]: start nginx:[  OK  ]
Sep 17 15:53:33 i****z systemd[1]: Started LSB: start|stop|status|restart|configtest.
[root@i****z ~]# 

1.12 nginx安装完成,通过浏览器输入服务器IP地址检测

CentOS7.2 编译安装lnmp(nginx1.15.0,MariaDB10.3.9,PHP7.0.32)_第1张图片


2、安装php

2.1 创建PHP安装目录并进入此目录

[root@i****z ~]# mkdir /usr/local/php
[root@i****z ~]# cd /usr/local/php/
[root@i****z php]# 

2.2 下载解压php源码包并进入文件目录 PHP下载官网

[root@i****z php]# wget -O php-7.0.32.tar.gz http://cn2.php.net/get/php-7.0.32.tar.gz/from/this/mirror
[root@i****z php]# tar zxvf php-7.0.32.tar.gz
[root@i****z php]# cd php-7.0.32

2.3 执行配置命令。注:‘\’ 后面不能有空格

[root@i****Z php-7.0.32]# ./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-php-config=/usr/local/php/bin/php-config \
--disable-rpath \
--disable-debug \
--disable-fileinfo \
--enable-shared \
--enable-opcache \
--enable-inline-optimization \
--enable-fpm \
--enable-mbstring \
--enable-bcmath \
--enable-soap \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-zip \
--enable-gd-native-ttf \
--with-curl \
--with-zlib \
--with-bz2 \
--with-iconv-dir \
--with-mcrypt \
--with-mhash \
--with-openssl \
--with-libxml-dir \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysql \
--with-mysqli \
--with-mysql-sock \
--with-pdo-mysql \
--with-pear \
--with-gettext \
--with-jpeg-dir \
--with-png-dir \
--with-gd \
--with-freetype-dir= \
--with-xmlrpc \
--with-readline

# 上面为要输入的内容
# 下面为配置结束部分内容
config.status: creating main/php_config.h
config.status: executing default commands
configure: WARNING: unrecognized options: --with-php-config, --with-mysql
[root@i****Z php-7.0.32]# 

2.4 完成编译工作:

[root@i****Z php-7.0.32]# make && make install
******************
省略中间数据
******************
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/php/php-7.0.32/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/
[root@i****Z php-7.0.32]# 

2.5 配置文件

[root@i****Z php-7.0.32]# cp php.ini-development /usr/local/php/etc/php.ini
[root@i****Z php-7.0.32]# vim /usr/local/php/etc/php.ini

# 修改内容根据自己需求设置
# 例如
# upload_tmp_dir = "/usr/local/php/tmp"    
# upload_max_filesize = 20M
# date.timezone = "Asia/Shanghai"
# session.save_path = "/usr/local/php/tmp"
# soap.wsdl_cache_dir="/usr/local/php/tmp"

# 创建缓存目录
[root@i****Z php-7.0.32]# mkdir /usr/local/php/tmp
[root@i****Z php-7.0.32]# chmod -R 755 /usr/local/php/tmp/

2.6 配置php-fpm服务

[root@i****Z php-7.0.32]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@i****Z php-7.0.32]# vim /usr/local/php/etc/php-fpm.conf

# 取消前面的分号
pid = run/php-fpm.pid

[root@i****Z php-7.0.32]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@i****Z php-7.0.32]# vim /usr/local/php/etc/php-fpm.d/www.conf

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

# 监听端口
listen = 127.0.0.1:9000

2.7 配置php启动脚本,输入以下内容,保存退出

[root@i****Z php-7.0.32]# vim /etc/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target

2.8 启动php-fpm服务

[root@i****Z php-7.0.32]# systemctl start php-fpm.service

# 查看进程
[root@i****Z php-7.0.32]# netstat -utnlp | grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      3629/php-fpm: pool 

2.9 配置php-fpm开机启动

[root@i****Z php-7.0.32]# vim /etc/init.d/php-fpm
# 拷贝下面一个代码框的内容

# 设置文件权限
[root@i****Z php-7.0.32]# chmod -R 755 /etc/init.d/php-fpm

# 添加到开机启动项
[root@i****Z php-7.0.32]# chkconfig --add /etc/init.d/php-fpm
[root@i****Z php-7.0.32]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /etc/systemd/system/php-fpm.service.

# 启动php-fpm
[root@i****Z php-7.0.32]# service php-fpm start
Starting php-fpm (via systemctl):                          [  OK  ]
#!/bin/sh  
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

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

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

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

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    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  
    start  
}  

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

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

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  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2  
esac

2.10 配置nginx多站点及支持php

[root@i****Z php-7.0.32]# vim /usr/local/nginx/conf/nginx.conf

将   #user  nobody;                 修改为   user  nginx nginx;
将   #error_log  logs/error.log;    修改为   error_log  logs/error.log;

# 修改Nginx运行组为nginx nginx;
# 必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
# 开启nginx错误日志,日志文件位置:/uer/local/nginx/logs/

# 包含域名配置文件(支持通配符) 
# 在#gzip  on; 后面添加下面一行代码
# 屏蔽后面所有内容(修改内容在http{}中,所以最后一个‘}’不能屏蔽掉)
include /usr/local/nginx/vhost/*.conf;

2.11 配置虚拟主机公用配置文件server.conf

[root@i****Z php-7.0.32]# vim /usr/local/nginx/conf/server.conf
# php文件访问配置
location ~ .*\.(php|php5)?$
{
    #fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include       fastcgi.conf;
    fastcgi_param HTTPS $https if_not_empty;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# 静态文件缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
    expires 30d;
    # access_log off;
}

# js,css文件缓存15个小时
location ~ .*\.(js|css)?$
{
    expires 15d;
    # access_log off;
}

2.12 配置虚拟主机文件,内容根据自己需求更改

[root@i****Z php-7.0.32]# mkdir /usr/local/nginx/vhost
[root@i****Z php-7.0.32]# vim /usr/local/nginx/vhost/test.conf
server {
    listen 80;

    # 配置域名
    server_name  www.test.com test.com;

    # 配置网站目录
    root   /home/www/test;

    # 配置域名重定向
    if ($host != "www.test.com") {
        rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }

    location / {
        # 配置rewrite url重写,框架必须
        #if (!-e $request_filename) {
        #    rewrite  ^(.*)$  /index.php?s=$1  last;
        #    break;
        #}

        # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;

        # 配置默认访问文件
        index  index.php index.html index.htm;
    }

    # 包含虚拟主机公用配置文件
    include server.conf;
}

2.13 创建网站目录,修改目录权限、所属用户和组

[root@i****Z php-7.0.32]# mkdir -p /home/www/test
[root@i****Z php-7.0.32]# chmod -R 777 /home/www/test/
[root@i****Z php-7.0.32]# chown -R nginx:nginx /home/www/test/

2.14 重启nginx

[root@i****Z php-7.0.32]# systemctl restart nginx

 2.15 添加到环境变量

[root@i****Z php-7.0.32]# vim /etc/profile

# 文件尾添加下面两行
PATH=$PATH:/usr/local/php/bin
export PATH

# 保存退出
# 执行命令使其修改生效
[root@i****z php-7.0.32]# source /etc/profile

# 检测,任意目录
[root@i****z ~]# php -v
PHP 7.0.32 (cli) (built: Sep 17 2018 18:10:31) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
[root@i****z ~]# 

 2.16 添加测试文件

[root@i****Z ~]# vim /home/www/test/index.html

# 输入内容
test html

[root@i****Z ~]# vim /home/www/test/index.php

# 输入内容

2.17 通过浏览器检测是否能够访问成功

CentOS7.2 编译安装lnmp(nginx1.15.0,MariaDB10.3.9,PHP7.0.32)_第2张图片CentOS7.2 编译安装lnmp(nginx1.15.0,MariaDB10.3.9,PHP7.0.32)_第3张图片

你可能感兴趣的:(服务器设置)