目录
一、yum安装
可参考:How To Install Nginx on CentOS 7 | DigitalOcean
1、添加Epel软件库:
sudo yum install epel-release
2、安装nginx:
sudo yum install nginx
3、启动nginx:
sudo systemctl start nginx
4、验证nginx是否成功启动:
sudo systemctl sratus nginx
5、访问nginx:http://ip:80 (可访问成功看到欢迎页面即可,若无法访问可查看端口是否开启)
默认配置文件路径:/etc/nginx/nginx.conf
默认网站根目录:/usr/share/nginx/html
二、安装包安装
安装包下载路径:Index of /download/
1、安装相关依赖包:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、进入安装目录:cd /usr/local
3、解压文件:tar -zxvf nginx-1.8.0.tar.gz
4、进入解压后的目录:cd nginx-1.8.0
5、编译安装:
./configure
make && make install
6、执行 /usr/local/nginx/sbin/nginx 启动nginx服务
(关闭可执行:/usr/local/nginx/sbin/nginx -s stop)
启动前可验证配置文件是否正确执行:/usr/local/nginx/sbin/nginx -t
7、将nginx加入服务项
vi /usr/lib/systemd/system/nginx.service
写入以下代码:
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8、测试启动服务:systemctl start nginx
三、nginx+php配置
1、安装php(CentOS 7自带的源中仅提供PHP 5.x版本)
yum install -y php-fpm
systemctl start php-fpm
2、查看php版本:php -v
3、更改nginx配置文件后,重新启动nginx
4、验证:
在网站根目录下新建一个php文件,如vi index.php,写入以下内容
<?php
echo phpinfo();
?>
再通过http://ip:80/index.php访问,可访问成功出现以下画面则完成