CentOS7安装nginx
-
安装前的准备
yum -y \ install \ libtool \ openssl-devel
-
安装PCRE
cd /root wget --no-check-certificate -c https://udomain.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz tar -xf pcre-8.45.tar.gz cd pcre-8.45 ./configure make make install
-
安装zlib
cd /root wget --no-check-certificate -c https://udomain.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz tar -xf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make make install
-
安装nginx
cd /root wget --no-check-certificate -c http://nginx.org/download/nginx-1.23.3.tar.gz tar -xf nginx-1.23.3.tar.gz cd nginx-1.23.3 ./configure \ --prefix=/usr/local/nginx/ \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_slice_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-pcre=../pcre-8.45/ \ --with-zlib=../zlib-1.2.11 make make install
-
开启firewall指定端口
systemctl unmask firewalld systemctl enable firewalld systemctl start firewalld firewall-cmd --permanent --zone=public --add-port=80/tcp firewall-cmd --permanent --zone=public --add-port=443/tcp firewall-cmd --reload
-
修改nginx配置
vim /usr/local/nginx/conf/nginx.conf user www; worker_processes auto; worker_cpu_affinity auto; pid logs/nginx.pid; events { worker_connections 65535; } http { server_tokens off; charset utf-8; proxy_hide_header Server; proxy_hide_header X-Powered-By; include mime.types; default_type application/octet-stream; client_max_body_size 210M; sendfile on; keepalive_timeout 20; client_header_timeout 20; client_body_timeout 20; send_timeout 20; gzip on; gzip_vary on; gzip_comp_level 1; gzip_types text/css application/javascript application/json image/png image/webp image/apng image/jpeg image/x-icon; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; error_log /www/z_error.log; access_log /www/z_$host.log main; server { listen 80; server_name localhost; root /www; location / { index index.php index.html index.htm; } location ~* \.php { include fastcgi_params; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } } ESC :wq
-
将nginx加入环境变量
vim /etc/profile export PATH=$PATH:/usr/local/nginx/bin export PATH=$PATH:/usr/local/nginx/sbin ESC :wq source /etc/profile
-
创建www用户
useradd www
-
启动nginx并设置开机启动
mkdir /www mkdir /www/log chown -R www:www /www chown -R www:www /usr/local/nginx vim /etc/systemd/system/nginx.service [Unit] Description=Nginx After=default.target network.target [Service] User=root Group=root Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true Restart=always RestartSec=5 StartLimitInterval=0 [Install] WantedBy=multi-user.target ESC :wq systemctl daemon-reload systemctl enable nginx systemctl start nginx
-
nginx操作
/usr/local/nginx/sbin/nginx 启动 /usr/local/nginx/sbin/nginx -t 检查当前配置错误 /usr/local/nginx/sbin/nginx -s reload 重载配置 /usr/local/nginx/sbin/nginx -s quit 正常关闭 /usr/local/nginx/sbin/nginx -s stop 强制关闭