linux安装nginx
1.查看nginx是否安装,有则卸载
whereis nginx
ps -ef | grep nginx
如果有则卸载,卸载命令: yum remove nginx
#彻底删除
rm -rf /etc/nginx/
rm -rf /usr/sbin/nginx yum remove nginx
===========
安装
首先进入/usr/local目录
cd /usr/local
2.从官网下载最新版的nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
或者从其它机器拷贝 # scp -r root@192.168.0.2:/home/push/upload/* /home/push/upload/ 该命令有如下几个参数: -p 拷贝文件的时候保留源文件建立的时间。 Preserves modification times, access times, and modes from the original file. -q 执行文件拷贝时,不显示任何提示消息。 : disables the progress meter as well as warning and diagnostic messages from ssh(1). -v 拷贝文件时,显示提示信息。 Verbose mode. Causes scp and ssh(1) to print debugging messages about their progress. -r 拷贝整个目录。Recursively copy entire directories. 如果不支持scp,则需要用如下命令安装它: # yum install openssh-clients
3.解压nginx压缩包
tar -zxvf nginx-1.18.0.tar.gz
4.会产生一个nginx-1.18.0 目录,这时进入该目录
cd nginx-1.18.0
5.接下来安装,使用--prefix参数指定nginx安装的目录,并且 安装 http_ssl_module with-http_stub_status_module 模块
默认安装在/usr/local/nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
支持tcp代理:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module
--with-stream
--with-stream --with-stream_ssl_module
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
可能错误
========
checking for OS + Linux 3.10.0-1160.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
解决:
yum remove -y gcc yum install -y gcc gcc-c++
可能出现 错误:./configure: error: the HTTP rewrite module requires the PCRE library.
解决:安装pcre-devel解决问题
yum -y install pcre-devel
还有可能出现:
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解决办法:
yum -y install openssl openssl-devel
============
make
make install
6.如果没有报错,顺利完成后,最好看一下nginx的安装目录
whereis nginx
7.安装完毕后,进入nginx/sbin
./nginx -t //查看nginx.conf 配置文件是否正确
./nginx -V //查看已经安装好的模块
8.configure arguments:后面跟着的就是已经安装好的模块
已经看到ssl的模块已经安装好了,接下来启动nginx的服务,以下命令启动nginx
使用守护开机启动,不使用下面的直接启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
或者进入sbin目录下执行:./nginx
在浏览器中打开访问ip查看是否正常启动
9.后面就是配置sll 证书了。在nginx.conf 加上如下配置
server {
listen 80;
server_name www.xxxxx.com;
ssl on;
ssl_certificate /usr/local/nginx/cert/www.xxxxx.com .crt; //证书路径
ssl_certificate_key /usr/local/nginx/cert/www.xxxxx.com .key; //证书路径
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_verify_client off;
location / {
root /opt/web;
index index.html index.htm;
}
}
10 重启nginx服务
./nginx -s reload
平滑新增tcp模块:https://www.cnblogs.com/crysmile/p/9565048.html
开机启动
https://www.cnblogs.com/easonchean/p/14199109.html
vim /lib/systemd/system/nginx.service
[Unit] Description=nginx service After=network.target [Install] WantedBy=multi-user.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=false
如果nginx使用原生方式启动,需要先使用原生指令停止: ./nginx -s stop
如果停止过程遇到: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
则使用杀死端口方式停止:
ps aux | grep nginx(查看nginx进程运行状态)
killall -9 pid
启动服务:systemctl start nginx.service
关闭服务:systemctl stop nginx.service
重启服务:systemctl restart nginx.service
显示服务的状态:systemctl status nginx.service
在开机时启用服务:systemctl enable nginx.service
在开机时禁用服务:systemctl disable nginx.service
查看服务是否开机启动:systemctl is-enabled nginx.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed