一、安装插件
1、安装gcc
首先查看是否已经安装过gcc了
gcc -v
如果已经安装过了,则会有如下类似输出
[root@localhost ~]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
如果没有,则用如下命令安装
yum -y install gcc
2、pcre、pcre-devel安装
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。
yum install -y pcre pcre-devel
3、zlib安装
zlib库提供了很多种压缩和解压缩方式,nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel
4、安装openssl
安装了openssl,才能使用https
yum install -y openssl openssl-devel
二、安装Nginx
1、下载Nginx安装包
wget http://nginx.org/download/nginx-1.14.1.tar.gz
下载完之后解压
tar -xf nginx-1.14.1.tar.gz
2、安装Nginx
解压之后,执行以下命令
cd nginx-1.14.1
./configure --with-http_stub_status_module --with-http_ssl_module
make
make install
有以下输出,说明安装成功
[root@localhost nginx-1.14.1]$ make install
make -f objs/Makefile install
make[1]: Entering directory `/home/django/download/nginx-1.14.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
mkdir: cannot create directory ‘/usr/local/nginx’: Permission denied
make[1]: *** [install] Error 1
make[1]: Leaving directory `/home/django/download/nginx-1.14.1'
make: *** [install] Error 2
[django@localhost nginx-1.14.1]$ sudo make install
[sudo] password for django:
make -f objs/Makefile install
make[1]: Entering directory `/home/django/download/nginx-1.14.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
|| mv '/usr/local/nginx/sbin/nginx' \
'/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
|| mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
|| cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
'/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/home/django/download/nginx-1.14.1'
3、配置Nginx
首先切换到/usr/local/nginx
[root@localhost nginx]$ pwd
/usr/local/nginx
[root@localhost nginx]$ ll
total 0
drwxr-xr-x 2 root root 333 May 12 17:18 conf
drwxr-xr-x 2 root root 40 May 12 17:18 html
drwxr-xr-x 2 root root 6 May 12 17:18 logs
drwxr-xr-x 2 root root 19 May 12 17:18 sbin
[django@localhost nginx]$
加载到conf下
[root@localhost nginx]$ cd conf/
[root@localhost conf]$ ll
total 68
-rw-r--r-- 1 root root 1077 May 12 17:18 fastcgi.conf
-rw-r--r-- 1 root root 1077 May 12 17:18 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 May 12 17:18 fastcgi_params
-rw-r--r-- 1 root root 1007 May 12 17:18 fastcgi_params.default
-rw-r--r-- 1 root root 2837 May 12 17:18 koi-utf
-rw-r--r-- 1 root root 2223 May 12 17:18 koi-win
-rw-r--r-- 1 root root 5170 May 12 17:18 mime.types
-rw-r--r-- 1 root root 5170 May 12 17:18 mime.types.default
-rw-r--r-- 1 root root 2656 May 12 17:18 nginx.conf
-rw-r--r-- 1 root root 2656 May 12 17:18 nginx.conf.default
-rw-r--r-- 1 root root 636 May 12 17:18 scgi_params
-rw-r--r-- 1 root root 636 May 12 17:18 scgi_params.default
-rw-r--r-- 1 root root 664 May 12 17:18 uwsgi_params
-rw-r--r-- 1 root root 664 May 12 17:18 uwsgi_params.default
-rw-r--r-- 1 root root 3610 May 12 17:18 win-utf
image配置nginx的配置文件nginx.conf
image首先修改用户,其次修改默认的服务端口,或者删除默认服务
在http{}的最后添加include /usr/local/nginx/conf/conf.d/*.conf; 这个的意思是会从include /usr/local/nginx/conf/conf.d中读取所有.conf的配置文件
完整配置文件如下:
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /usr/local/nginx/conf/conf.d/*.conf;
}
4、启动Nginx
切换目录到/usr/local/nginx/sbin下面,用下面这条命令启动nginx
./nginx
5、查看Nginx是否启动成功
[root@localhost sbin]# ps -ef | grep nginx
root 25241 1 0 17:50 ? 00:00:00 nginx: master process ./nginx
root 25242 25241 0 17:50 ? 00:00:00 nginx: worker process
root 25244 25209 0 17:50 pts/0 00:00:00 grep --color=auto nginx
6、编写自己的配置
切换目录到/usr/local/nginx/conf下,新建conf.d并加载到conf.d下面
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# mkdir conf.d
[root@localhost conf]# cd conf.d/
[root@localhost conf.d]#
在conf.d中编写.conf文件,即我们自己的配置
server {
listen 8080;
server_name 172.18.3.171;
charset utf-8;
location / {
root /home/project/ruicheng_project/dist;
try_files $uri $uri/ /index.html last;
index index.html;
}
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000/;
}
}
编写完后,重启nginx即可
三、将Nginx添加到service
按照以上步骤安装nginx完毕之后,每次启动nginx都得加载到/usr/local/nginx/sbin中启动./nginx,这样非常麻烦,可以进行以下步骤将nginx添加到service中,就可以用service nginx start来启动nginx
1、在/etc/init.d/路径下添加脚本文件,名称为nginx
image2、将如下代码写进nginx脚本文件
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done."
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done."
;;
test)
$nginx -t -c $conf
echo "Success."
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done."
;;
restart)
$nginx -s reload
echo "reload done."
;;
*)
echo "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
3、为nginx添加文件可执行权限
[root@localhost init.d]# chmod +x nginx
4、service启动nginx
# 启动
[root@localhost init.d]# service nginx start
Starting Nginx done.
# 重启
[root@localhost init.d]# service nginx restart
reload done.
网友评论