Linux的Nginx安装配置教程
一、下载安装包
下载地址:Nginx安装包下载地址
二、安装Nginx
1.安装编译工具以及库文件
执行下列命令
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.安装 PCRE
下载地址:PCRE安装包下载地址
上传安装包并在上传目录执行一下语句
tar xzvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre
make && make install
#查看是否安装成功
pcre-config --version
如下图:
3.安装Nginx
上传安装包并在上传目录执行一下语句
tar xzvf nginx-1.21.1.tar.gz
cd nginx-1.21.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre --add-module=/usr/local/src/incubator-pagespeed-ngx
make && make install
#查看当前Nginx版本
/usr/local/nginx/sbin/nginx -v
如下图:
三、Nginx配置
1.创建 Nginx 运行使用的用户 www
执行命令
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
2.配置nginx.conf
将/usr/local/nginx/conf/nginx.conf替换为以下内容
user www www;
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 {
use epoll;
worker_connections 65535;
}
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;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$ {
expires 15d;
# access_log off;
}
access_log off;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4.设置Nginx开机自启动
建立服务文件
vim /usr/lib/systemd/system/nginx.service
#新建文件,把下面文件内容放到文件中
[Unit]
Description=nginx
After=network.target
[Service]
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
[Install]
WantedBy=multi-user.target
保存退出后,执行以下命令来设置开机自启动
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
5.Nginx相关命令
#加入开机自启动
systemctl enable nginx
#取消开机自启动
systemctl disable nginx
#启动nginx服务
systemctl start nginx
#停止nginx服务
systemctl stop nginx
#重启nginx服务
systemctl restart nginx
#查看nginx服务当前状态
systemctl status nginx
#查看开机已启动是否设置成功
systemctl list-units --type=service |grep nginx
评论区