CentOS7安装Nginx1.17.8的详细步骤和网站配置

nginx网站:http://nginx.org/
如果你没有配置过centos系统的初始环境,需要手动安装需要的软件包。
nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库:包括PCRE正则表达式,安全套接字层密码库,和一些常用软件包等等。如果之前搭建过环境,下面的请无视,直接下一步。

yum install gcc gcc-c++
yum -y install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
yum make wget

下载最新版本的Nginx1.17.8

wget http://nginx.org/download/nginx-1.17.8.tar.gz
tar -zxvf nginx-1.17.8.tar.gz
cd nginx-1.17.8

取消Debug编译模式

vim auto/cc/gcc

#CFLAGS=”$CFLAGS -g” 将这句用#号注释掉。
译安装

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make
make install

测试安装是否成功:

cd /usr/local/nginx/sbin

进入nginx目录输入./nginx -t 或者/usr/local/nginx/sbin/nginx -t
设置开机自启动Nginx:

vi /lib/systemd/system/nginx.service

创建一个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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

设置nginx开机启动,使配置生效:

systemctl enable nginx.service

编辑和配置nginx,打开Nginx的配置文件

vi /usr/local/nginx/conf/nginx.conf

将下面代码替换掉nginx.conf文件内的代码.这只是一个简单的支持http访问和PHP访问的配置文件。

user www www;
worker_processes 1;
pid logs/nginx.pid;

events {
use epoll;
worker_connections 2048;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

server {
listen 80;
server_name 网站域名;
client_max_body_size 10M;
root /var/www/;
index index.html index.htm index.php;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
try_files $uri =404;

include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}

}
}

启动Nginx

systemctl start nginx

在服务器的VAR目录下创建WWW目录,然后在WWW目录里新建一个index.php的文件,将下列内容复制粘贴到index.php后,保存!

/var/www目录是上面我们配置Nginx的网站访问文件。这个目录一定要和nginx.conf里设置的对应,可以随便设置!

phpinfo();

?>

保存好index.php后,输入服务器的IP地址,或者设置绑定的域名,域名一定要解析生效后才可以使用。

在浏览器输入IP或者域名后,就可以打开PHP的探针页面了。

在探针页面上,你可以查看到安装PHP的一些软件包信息和安装目录等内容。好了支持PHP的Nginx安装好了!接下来去安装其它软件吧!

 

下面是Nginx的控制命令:
systemctl start nginx 启动

systemctl stop nginx 停止

systemctl reload nginx 重启 可以不用停止nginx服务,使修改的配置生效

systemctl restart nginx 重启

systemctl enable nginx 设置开机启动

systemctl disable nginx 禁用开机启动

systemctl status nginx 查看服务状态

评分

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注