一、参考如下资料html
https://www.cnblogs.com/reboot777/p/7226365.html https://www.cnblogs.com/EasonJim/p/7806879.html 20190125 再次实践后,改用最简单的方法! sudo apt install nginx
二、因为 ubuntu 下已经存在了 apache2,因此,建议先删除 apache2nginx
直接参照 https://zerlong.com/587.html sudo service apache2 stop update-rc.d -f apache2 remove sudo apt-get remove apache2
三、如下内容彻底抄写自 http://www.javashuo.com/article/p-zlzjrbjw-ep.html (除了最新版本号改变到 1.14.0)apache
1.安装gcc g++的依赖库 sudo apt-get install build-essential sudo apt-get install libtool 2.安装pcre依赖库 sudo apt-get install libpcre3 libpcre3-dev 3.安装zlib依赖库 sudo apt-get install zlib1g-dev 4.安装ssl依赖库 sudo apt-get install openssl 5.安装nginx - 下载最新稳定版本: ** 先打开 http://nginx.org/download/ 去找找最新版本,替换下面的版本号 sudo wget http://nginx.org/download/nginx-1.14.0.tar.gz - 解压: tar -zxvf nginx-1.14.0.tar.gz - 进入解压目录: cd nginx-1.14.0 - 配置: ./configure --prefix=/usr/local/nginx - 编译 nginx: make ** 注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory 须要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev - 安装nginx: sudo make install - 启动nginx: sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ** 注意:-c 指定配置文件的路径, 不加的话,nginx会自动加载默认路径的配置文件 能够经过 -h查看帮助命令。 - 查看nginx进程: ps -ef|grep nginx - 加入 nginx.service cd /lib/systemd/system sudo touch nginx.service sudo vim nginx.service 输入如下内容 [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
四、确认 nginxubuntu
方式1、直接 curl 127.0.0.1,能够看到 Welcome to nginx! ...... ** 我没有删除 apache2 就直接安装的,结果看到的仍是 Apache2 Ubuntu Default Page ...... ** 此时回头删除 apache2 ,还来得及 方式二:http://221.122.102.167:7193/ (我映射了 7193端口,实际应该是 http://IP地址)
五、设置 nginx 服务,让 nginx 自动启动vim
我是学习了如下 2 个之后,才算完成的,建议初学者也同时打开以上两位的blog https://blog.csdn.net/Java__han/article/details/77648475 https://pdf-lib.org/Home/Details/4864 ** 网上大量方法,还都是之前的 init.d 设置方法,我就没有试错了! 再次感谢以上两位大神! ** 直接抄写 https://segmentfault.com/q/1010000005987075 帖子一段话做为个人学习成果! 自从 Ubuntu 15.04 以后,就已经开始默认使用 systemd 了,你的配制方法是传统的 Upstart/Sysinitv,因此不起做用。应该使用 systemd 对应的 systemctl 命令。 启动: systemctl start nginx 查看状态:systemctl status nginx 设置为系统默认启动: systemctl enable nginx 并且不知道你的 Nginx 是如何安装的,在 Ubuntu/Debian 中,若是是使用标准的 apt-get install nginx 的方式,这些都已经配置好了,默认状况下,就是开机自启动。 ** 我操做过程当中,老是看到错误 Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. *** 我回忆起 systemctl list-unit-files | grep nginx 的时候,看到的是 nginx.service disabled *** 因此,我干脆,直接 reboot 了,从新近来再看,发现一切 ok了! ** 因此,下面 2 个命令很重要 # 这个是列举全部已经存在配置文件对应的服务状态列表 systemctl list-unit-files | grep nginx # 列举出具备加载状态的服务列表(或者理解为最近被使用的服务) systemctl --all | grep nginx
20190123 从新按照我本身的博文操做后,出现了如下 2 次错误 sudo systemctl start nginx [sudo] password for dhbm: Failed to start nginx.service: Unit nginx.service not found. systemctl status nginx ● nginx.service Loaded: not-found (Reason: No such file or directory) Active: inactive (dead) 由于以上是下载源码编译、安装的,因此,须要本身生成服务文件 nginx.service
最简单的方法! sudo apt install nginx 仍是 感谢 https://pdf-lib.org/Home/Details/4864 做者!