Linux下nginx的安装
安装环境:
# cat /proc/version
Linux version 3.10.0-123.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Mon Jun 30 12:09:22 UTC 2014
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
yum安装
设置yum仓库地址
vi /etc/yum.repos.d/nginx.repo
:[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
使用yum安装:
yum install -y nginx
安装完成后配置文件所在目录为/etc/nginx
。
- nginx的启动与停止:
- 启动nginx:
nginx
或systemctl start nginx
。 - 停止nginx:
nginx -s stop
或systemctl stop nginx
。
查看nginx是否启动成功。
ps -ef|grep nginx
root 1830 1 0 22:45 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 1831 1830 0 22:45 ? 00:00:00 nginx: worker process root 1835 1638 0 22:45 pts/0 00:00:00 grep --color=auto nginx
systemctl status nginx
● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2020-10-07 22:45:12 EDT; 22s ago Docs: http://nginx.org/en/docs/ Process: 1817 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=1/FAILURE) Process: 1829 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) CGroup: /system.slice/nginx.service ├─1830 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─1831 nginx: worker process Oct 07 22:45:12 localhost.localdomain systemd[1]: Starting nginx - high performance web server... Oct 07 22:45:12 localhost.localdomain systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory Oct 07 22:45:12 localhost.localdomain systemd[1]: Started nginx - high performance web server.
访问网站
http://localhost/
。卸载nginx。
yum list nginx
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile
- base: mirrors.ustc.edu.cn
- centos-sclo-rh: mirrors.163.com
- centos-sclo-sclo: mirrors.ustc.edu.cn
- extras: mirrors.ustc.edu.cn
- updates: mirrors.ustc.edu.cn Installed Packages nginx.x86_64 1:1.18.0-1.el7.ngx @nginx-stable
yum remove nginx.x86_64
Loaded plugins: fastestmirror ... ...
源码安装
源码安装的方式方便对nginx进行自定义的配置以及安装插件。
下载源码,下载地址:http://nginx.org/en/download.html,这里使用的版本为`nginx-1.18.0.tar.gz`。
解压
nginx-1.18.0.tar.gz
:tar xf nginx-1.18.0.tar.gz
安装可能需要的依赖:
yum -y install autoconf automake make
yum -y install gcc gcc-c++
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum install -y openssl openssl-devel
配置:
cd nginx-1.18.0
./configure
安装:
make && make install
安装完成后的二进制文件和配置文件在/usr/local/nginx
目录下。
配置环境变量:
vi /etc/profile
最后增加 export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
安装成为服务:
vi nginx.service
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target
[Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install] WantedBy=multi-user.target
systemctl daemon-reload
OpenResty的安装
OpenResty是一个基于Nginx与Lua的高性能Web平台,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。
下载OpenResty,下载地址:
https://openresty.org/download/openresty-1.17.8.2.tar.gz
。安装:
tar xf openresty-1.17.8.2.tar.gz
cd openresty-1.17.8.2
./configure
make && make install
配置环境变量:
vi /etc/profile
最后追加 export PATH=$PATH:/usr/local/openresty/bin
cd /usr/local/openresty/bin
ll | grep openresty
lrwxrwxrwx. 1 root root 37 Oct 9 14:09 openresty -> /usr/local/openresty/nginx/sbin/nginx
openresty是一个指向/usr/local/openresty/nginx/sbin/nginx
的软链接,其实openresty内置了一个nginx,使用方法与nginx一样。
# openresty -V
nginx version: openresty/1.17.8.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.17 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.8 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module