云服务器从购买到部署环境lnmp(三)

二、源码安装最新nginx1.16.0

2.1安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2.2安装 PCRE

注意安装前先查看有没有PCRE,有则跳过2.2这一步!

pcre-config --version

2.2.1下载PCRE

PCRE 作用是让 Nginx 支持 Rewrite 重写功能,例如隐藏URL的某路径,TP5中的index.php就需要用到Rewrite,当然这是后话。
PCER下载地址:http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
下载好使用Xftp 6复制到服务器中(为什么不直接下载呢,因为这样比较快)
在这里插入图片描述
或者可能超慢的

wget -c http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2.2.2解压

cd /Downloads(你刚才把压缩包复制到的位置)

tar -zxvf pcre-8.35.tar.gz

在这里插入图片描述

2.2.3配置

注意:指定了安装目录,我是/etc/pcre-8.35。不指定目录的安装到时卸载有可能需要手动一个个删除(微笑)
cd pcre-8.35

./configure --prefix=/etc/pcre-8.35

2.2.4编译&安装

make && make instsall

2.2.5查看版本

pcre-config --version

在这里插入图片描述

2.3安装nginx

2.3.1下载nginx

nginx下载地址:http://nginx.org/download/nginx-1.16.0.tar.gz (建议)

流程参考2.3.1

或者wget -c http://nginx.org/download/nginx-1.16.0.tar.gz

2.3.2解压

cd /Downloads

tar -zxvf nginx-1.16.0.tar.gz

2.3.3配置指定路径

cd nginx-1.16.0

(这里没指定上面安装好的pcre是因为我的系统已经下载好了,避免安装出现问题,使用时在conf指定位置即可)

./configure --prefix=/etc/nginx --with-http_ssl_module

2.3.4编译并安装

make && make install

清理编译文件

make clean 

2.3.5查看安装版本

/etc/nginx/sbin/nginx -v

在这里插入图片描述

2.3.6设置服务启动+开机启动(编码安装需要自己编写)

cd /etc/init.d/nginx 

在init.d中新建一个nginx启动文件,可能你多我一层目录,在init.d中

vi nginx

将以下代码粘贴进去,目录不是/etc/nginx的记得更改

#! /bin/bash
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “KaTeX parse error: Expected 'EOF', got '&' at position 22: …KING" = "no" ] &̲& exit 0 nginx…(basename $nginx)
NGINX_CONF_FILE=”/etc/nginx/conf/nginx.conf"
[ -f /etc/nginx/sbin/nginx ] && ./etc/nginx/sbin/nginx
lockfile=/etc/nginx/logs/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c NGINXCONFFILEretval=NGINX_CONF_FILE retval=?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc progQUITretval=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 nginxHUPRETVAL=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
– VISUAL –

在init.d目录下

chmod +x nginx
chkconfig

在这里插入图片描述

chkconfig  --add nginx

chkconfig nginx on

在这里插入图片描述

添加成功
systemctl list-dependencies nginx
systemctl daemon-reload

2.3.7nginx.conf配置文件

vi /etc/nginx/conf/nginx.conf

以下是我的nginx.conf

user www;
worker_processes auto;
error_log /etc/nginx/logs/error.log;
pid /run/nginx.pid;

\# Load dynamic modules. See /usr/share/nginx/README.dynamic.
\# 模块目录待更改
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /etc/nginx/logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/conf/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # include /etc/nginx/conf/nginx.conf.default;
    
    #服务器配置
    server {
    listen       80;
    server_name  localhost;
    root /var/www;
    index  index.html index.htm index.php;
    error_page  404              /404.html;
    location = /404.html {
        return 404 'Sorry, File not Found!';
    }
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        #还没做这个页面
        root   /usr/share/nginx/html; # windows用户替换这个目录
    }
    location /myProject/ {
        try_files $uri @rewrite;
    }
    location @rewrite {
        set $static 0;
        if  ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
            set $static 1;
        }
        if ($static = 0) {
            rewrite ^/myProject/JM-yingfu/(.*)$ /myProject/JM-yingfu/index.php?s=/$1 last;
        }
    }
    #拒绝所有.php格式的文件上传
    location ~ /Uploads/.*\.php$ {
        deny all;
    }
    location ~ \.php/ {
       if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
       fastcgi_pass 127.0.0.1:9000;
       include fastcgi_params;
       fastcgi_param SCRIPT_NAME     $1;
       fastcgi_param PATH_INFO       $2;
       fastcgi_param SCRIPT_FILENAME $document_root$1;
    }
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\.ht {
        deny  all;
    }
}

需要注意以下3点:

1、软件目录和网站目录,网站目录我是选择放在/var/www
2、我进行了二级目录也就是myProject,并且使用rewirite重写隐藏了index.php,所以直接指定了项目,具体查找myProject那部分代码。假如你不需要二级目录和隐藏项目中的index.php,只需要使用查找替换myProject/JM-yingfu/为空,前者是二级目录,后者是我的项目名。
3、复制粘贴完后注意vi进去查看粘贴的代码是否完整

4、下面是我遇到的一些问题,因为我已经在nginx.conf做了修改,所以你不会遇到!!可跳过

遇到问题了一个个解决吧0.0

在这里插入图片描述

systemctl status nginx.service

在这里插入图片描述
1、mime.types文件缺失

find / -name mime.types

在这里插入图片描述
目录不对,复制一份

 cp /etc/nginx/conf/mime.types /etc/nginx/mime.types
 
 service nginx restart

2、重启后,显示user不允许在这

在这里插入图片描述
google一番后,猜测是这里的问题,结果确实是
在这里插入图片描述
看了下只有这个配置文件,所以只引入nginx.conf.default即可
在这里插入图片描述

service nginx restart

3、重启后,和2类似的问题
在这里插入图片描述
google一番后,依然是上面include的问题,因为nginx.conf和nginx.conf.default均含有worker_processes的定义。所以

删掉nginx.conf.default中的wroker_process,只保留一个

在这里插入图片描述
service nginx restart
4、重启后,又是类似的问题(手动微笑)
在这里插入图片描述

删掉nginx.conf.default中的events方法,只保留nginx.conf一个定义
service nginx restart

5、重启后,又是上面的问题。(从yum安装的conf复制的源码安装conf就是事多)
在这里插入图片描述

到了这里我发现nginx.conf.default都删没了。。所以我直接nginx.conf中的include直接注释掉(微笑)

在这里插入图片描述
service nginx restart
6、重启后,成功!不过出现了警告
在这里插入图片描述
按照提示

systemctl daemon-reload

完成!

在这里插入图片描述

可以使用service和systemctl命令操作,一样的

状态|重启|开启|停止|重载

service nginx states|restart|start|stop|reload
systemctl states|restart|start|stop|reload nginx

2.3.8创建网站的目录

在nginx中我们指定的目录是/var/www,所以需要创建个目录作为网站根目录

mkdir /var/www

创建个index.php测试

vi /var/www/index.php

在这里插入图片描述

在你的浏览器输入你的云服务器地址或者127.0.0.1

(这里之所以不用输入index.php就能访问到index.php,是因为我们nginx.conf中对url进行了重写,自动填充了index.php)
在这里插入图片描述

下一篇安装MySQL

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章