houjinkai 2019-06-29
[root@localhost ~]# yum -y install gcc [root@localhost ~]# yum -y install gcc++ [root@localhost ~]# yum -y install gcc-c++ [root@localhost ~]# yum -y install wget [root@localhost ~]# yum -y install make [root@localhost ~]# yum -y install libxml2 [root@localhost ~]# yum -y install libxml2-devel [root@localhost ~]# yum -y install openssl [root@localhost ~]# yum -y install openssl-devel [root@localhost ~]# yum -y install curl-devel [root@localhost ~]# yum -y install libjpeg-devel [root@localhost ~]# yum -y install libpng-devel [root@localhost ~]# yum -y install freetype-devel [root@localhost ~]# yum -y install bison [root@localhost ~]# yum -y install autoconf [root@localhost ~]# yum -y install automake [root@localhost ~]# yum -y install libtool [root@localhost ~]# yum -y install cmake [root@localhost ~]# yum -y install zlib [root@localhost ~]# yum -y install zlib-devel [root@localhost ~]# yum -y install pcre-devel
[root@localhost ~]# wget http://211.143.146.219:82/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/nginx.org/download/nginx-1.14.1.tar.gz [root@localhost ~]# tar -zxvf nginx-1.14.1.tar.gz
[root@localhost ~]# ./configure \ [root@localhost ~]# --prefix=/usr/local/nginx \ [root@localhost ~]# --with-http_stub_status_module \ [root@localhost ~]# --with-http_ssl_module \ [root@localhost ~]# --with-pcre \ [root@localhost ~]# --with-stream \ [root@localhost ~]# --with-mail=dynamic
[root@localhost ~]# make && make install
[root@localhost ~]# /usr/local/nginx/sbin/nginx [root@localhost ~]# ps -ef | grep nginx
> -r: 添加系统用户 > -g: 指定要创建的用户所属组 > -s: 新帐户的登录shell > -d: 新帐户的主目录 > -M: 不要创建用户的主目录
[root@centos7 ~]# groupadd -r nginx [root@centos7 ~]# useradd -r -g nginx -M nginx [root@centos7 ~]# passwd nginx [root@centos7 ~]# usermod -s /sbin/nologin nginx
[root@localhost ~]# echo $PATH [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
在环境变量:/usr/local/sbin/ 下创建 /usr/local/nginx/sbin/nginx 的软连接,当我们在任意目录输入 nginx 时,系统去环境变量中查找,通过软连接指向 /usr/local/nginx/sbin/nginx
检查配置文件是否正确、查看编译选项、启动、关闭、指定配置文件启动、重启、杀死进程
[root@centos7 /]# /usr/local/nginx/sbin/nginx -t [root@centos7 /]# /usr/local/nginx/sbin/nginx -V [root@centos7 /]# /usr/local/nginx/sbin/nginx [root@centos7 /]# /usr/local/nginx/sbin/nginx -s stop [root@centos7 /]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [root@centos7 /]# /usr/local/nginx/sbin/nginx -s reload [root@centos7 /]# pkill nginx
具体文法参考:Systemd 入门教程:实战篇
[root@centos7 /]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target
启动 nginx,设置 nginx 自动启动
[root@centos7 /]# systemctl start nginx [root@centos7 /]# systemctl enable nginx
当下面的端口占用错误时,可以尝试终止进程再次启动
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
[root@centos7 /]# netstat -ntpl [root@centos7 /]# kill 31448
参考文章:
基本功能介绍
配置文件详解
配置功能解释
Nginx 常用配置
Nginx配置文件主要分成如下四部分:
Nginx配置文件四个部分间的层级关系:
server { listen 80; server_name www.sea.com; root html; index index.php index.html index.htm; }
修改 linux 的 hosts 创建本地虚拟域名
[root@centos7 /]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 www.sea.com www.star.com
访问测试
[root@centos7 /]# curl www.sea.com
本系统是在 Windows 下的虚拟机,所以也得修改 Windows 的 hosts 使在 Windows 下访问目标时 IP 会指向虚拟机
192.168.108.128 www.sea.com 192.168.108.128 www.star.com
= 表示进行普通字符精确匹配 ~ 表示执行一个正则匹配,区分大小写 ~* 表示执行一个正则匹配,不区分大小写 ^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
修改目标 Server 的 根目录到项目目录下,即:/usr/local/www/sea
Nginx本身不支持PHP等语言,但是它可以通过FastCGI来将请求扔给某些语言或框架处理(例如PHP、Perl)server { listen 80; server_name www.sea.com; root /usr/local/www/sea; location / { index index.html index.htm index.php; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
PHP-FPM 是 PHP 的一个 FastCGI 管理器,配置中将 .php 结尾的请求通过 FashCGI 交给 PHP-FPM 处理
以下是对 ThinkPHP 的 URL 重写,隐藏应用的入口文件 index.php,这是对低版本 Nginx 不支持 PATHINFO 的解决方法
location / { index index.html index.htm index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } }
服务器上放置多个网站项目,它们共同使用 80 端口,根据 HTTP 协议报文表头信息的 Host 来区分不同的 Server
server { listen 80; server_name www.sea.com; root /usr/local/www/sea; location / { index index.html index.htm; } } server { listen 80; server_name www.star.com; root /usr/local/www/sea; location / { index index.html index.htm; } }
# 使用指定配置文件 nginx -c /usr/local/nginx/conf/nginx.conf # 测试配置文件是否正确 nginx -t # 重载配置 nginx -s reload # 停止 nginx -s stop
阿里云域名解析不通
ping www.sea.com # Ping 请求找不到主机 http://banjintaohua.online/。请检查该名称,然后重试。 # Windows nslookup www.sea.com # 服务器: UnKnown # Address: fe80::1 # DNS request timed out. # timeout was 2 seconds. # DNS request timed out. # timeout was 2 seconds. # *** 没有 www.sea.com 可以使用的 internal type for both IPv4 and IPv6 Addresses (A+AAAA)记录 # Linux dig www.sea.com # ; <<>> DiG 9.10.4-P2 <<>> www.sea.com # ;; global options: +cmd # ;; Got answer: # ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32759 # ;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 # ;; WARNING: recursion requested but not available # ;; QUESTION SECTION: # ;www.sea.com. IN A # ;; ANSWER SECTION: # www.sea.com. 128 IN A 42.123.125.237 # ;; Query time: 159 msec # ;; SERVER: 218.85.152.99#53(218.85.152.99) # ;; WHEN: Tue Dec 11 13:31:56 CST 2018 # ;; MSG SIZE rcvd: 61 # 刷新DNS缓存 # Windows ipconfig/flushdns # 刷新DNS缓存 # Centos7 systemctl restart nscd
提工单,原来是:解析线路的问题,我使用了[解析路线:谷歌],也就意味着只有谷歌源IP才可以解析到,更改为[解析路线:默认]就好了
转发到 www.sea.com 的 8080 端口
server { listen 80; location / { proxy_pass http://www.sea.com:8080; # 应用服务器HTTP地址 } }
哈希法:每个请求按访问 IP 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题
upstream loadbalance { ip_hash; # 哈希法 server 192.168.0.1:8080; # 应用服务器1 server 192.168.0.2:8080; # 应用服务器2 } server { listen 80; location / { proxy_pass http://loadbalance; } }
----------------------------------------------------------DONE------------------------------------------------------------
LNMP环境搭建(一):Mysql
LNMP环境搭建(二):Nginx
LNMP环境搭建(三):PHP