nginx server settings

Table of contents

1. Install nginx from source code and provide service scripts

 Write nginx service script

2. Based on domain name virtual host configuration

1. Add in the main configuration file

2. Write a virtual host configuration file

3. Write two pages

 4. The configuration hosts file under windos

 5. Test

 3. Configure nginx based on user and address access control.

Configure nginx based on user

User-Based Access Control

4. Configure LNMP and deploy php applications

1. Deploy lnmp environment

After the test is complete, delete the tested php file! ! !

Deploy application deployment discuz forum


1. Install nginx from source code and provide service scripts

1.下载源码包
[root@slave2 ~]# wget -c https://nginx.org/download/nginx-1.20.1.tar.gz
2.解压源码到/usr/local/src
[root@slave2 ~]# tar xf nginx-1.20.1.tar.gz -C /usr/local/src/
3.编译
(1)根据rpm安装的nginx查看编译选项
1.安装需要的库文件
yum install -y pcre-devel
yum install -y openssl-devel
2.编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
3.创建用户
nginx:x:304:305:Nginx web server:/var/lib/nginx:/sbin/nologin
4.关闭防火墙
systemctl status firewalld.service 
getenforce 

 Write nginx service script

编写nginx脚本
vim /usr/lib/systemd/system/nginx.service
[root@slave2 sbin]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.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 quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
启动脚本
systemctl daemon-reload 
systemctl start nginx.service

 Problem: If you can’t start it, you need to solve
1: Check whether there is an nginx process, if it exists, kill it
or not, execute the default command to shut down nginx: ./nginx -s stop
specifies the location of the configuration file where nginx is installed
/usr/local/ nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

Test: 192.168.27.122:80

2. Based on domain name virtual host configuration

1. Add in the main configuration file

[root@slave2 conf]# vim /usr/local/nginx/conf/nginx.conf
include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

2. Write a virtual host configuration file

The configuration of the virtual host must be server{} containing location{}

There are listening ports and server_name in the server

There are access directory files in location

1.vim /etc/nginx/conf.d/vhosts.conf
[root@slave2 conf.d]# cat vhosts.conf 
server {
        listen 192.168.27.122:80;
        server_name bbs.test.com;
        location / {
                root   html/bbs;
                index  index.html index.htm;
	}
}

server {
        listen 192.168.27.126:80;
        server_name blog.test.com;
        location / {
                root   html/blog;
                index  index.html index.htm;
	}
}
2.[root@slave2 conf.d]# systemctl restart nginx.service

3. Write two pages

1.创建目录
mkdir /usr/local/nginx/html/bbs
mkdir /usr/local/nginx/html/blog
2.切到bbs目录下创建index.html
[root@slave2 html]# cd bbs/
[root@slave2 bbs]# ll
total 4
-rw-r--r--. 1 root root 14 Mar 30 10:02 index.html
[root@slave2 html]# cd blog/
[root@slave2 blog]# ll
total 4
-rw-r--r--. 1 root root 15 Mar 30 09:53 index.html
3.重启
[root@slave2 blog]# systemctl restart nginx.service 

 4. The configuration hosts file under windos

 5. Test

Browser: bbs.test.com blog.test.com

 3. Configure nginx based on user and address access control.

Configure nginx based on user

A host is not allowed to access, port 192.168.27.126:80 is not allowed to access a page

1.修改虚拟主机
[root@slave2 conf.d]# vim vhosts.conf 
server {
        listen 192.168.27.122:80;
        server_name bbs.test.com;
        location / {
                root   html/bbs;
                index  index.html index.htm;
                deny 192.168.27.121;
                allow 192.168.27.0/24;
        }
}

server {
        listen 192.168.27.126:80;
        server_name blog.test.com;
        location / {
                root   html/blog;
                index  index.html index.htm;
        }
}
2.重启
[root@slave2 conf.d]# systemctl restart nginx.service 

Test Test
on the 192.168.27.121 host: curl 192.168.27.122:80
in other network segments of 192.168.27.0/24

User-Based Access Control

1.下载httpd-tools工具
yum install httpd-tools
2.创建加密用户文件
[root@slave2 ~]# htpasswd -c /usr/local/nginx/html/.file tom
New password: 
Re-type new password: 
Adding password for user tom
3.修改虚拟文件
[root@slave2 conf.d]# cat vhosts.conf 
server {
        listen 192.168.27.126:80;
        server_name blog.test.com;
        location / {
                root   html/blog;
                index  index.html index.htm;
		auth_basic "Restricted";
		auth_basic_user_file /usr/local/nginx/html/.file;
	}
}

test:

Enter blog.test.com in the browser, and then the user will be prompted to access the website or log in with a password

4. Configure LNMP and deploy php applications

1. Deploy lnmp environment

1.安装扩展源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.部署lnmp环境
[root@slave2 ~]# yum install mariadb-server php php-mysql php-gd php-fpm -y nginx
3.配置额外配置文件
[root@slave2 conf.d]# cat test.conf   /etc/nginx/conf.d/test.conf
server {
   location / {
	root html;
	index index.html index.htm;
	auth_basic "Restricted";
	auth_basic_user_file  /usr/local/nginx/html/.file;


}

location ~ \.php$ {
	root /usr/local/nginx/html;
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	include fastcgi_params;
}
}
4.启动服务
systemctl restart nginx mariadb php-fpm
5.测试nginx和php
[root@node1 conf.d]# echo "<?php phpinfo(); ?>" >/usr/local/nginx/html/phpinfo.php
[root@node1 conf.d]# systemctl restart nginx
6.配置数据库密码
mysqladmin -uroot password '123456'
7.写入mysql.php
[root@node2 conf.d]# cat /usr/slocal/nginx/html/nysql.php
<?php
$con = mysql_connect("localhost","root","12345");
if (!$con) {
die("could not connect to the db:\n" . mysql_error());
}
else { echo "success"; }
mysql_close($con);
?>

 test:

192.168.27.122/mysql.php

192.168.27.122/phpinfo.php

After the test is complete, delete the tested php file! ! !

Deploy application deployment discuz forum

1.安装Discuz压缩包
2.解压
unzip Discuz_X3.2_SC_UTF8.zip 
3.切到upload目录下
[root@slave2 upload]# ls
admin.php  archiver     cp.php           favicon.ico  home.php   member.php  portal.php  source    uc_client
api        config       crossdomain.xml  forum.php    index.php  misc.php    robots.txt  static    uc_server
api.php    connect.php  data             group.php    install    plugin.php  search.php  template  userapp.php
4.将upload移动到/usr/local/nginx/html/adc
[root@slave2 conf.d]# mv upload/ /usr/local/nginx/html/abc
5.在浏览器页面访问
http://192.168.27.122/abc/install/index.php
6.加执行权限
[root@slave2 abc]# chmod -R  a+w data/ uc_client/ config/ uc_server/
[root@slave2 abc]# systemctl restart nginx.service php-fpm.service 
进一步安装操作
http://192.168.27.122/abc/forum.php
在页面访问时输入192.168.27.122访问需要在配置文件以及子配置文件,将index.php放在index之后
http://192.168.27.122/abc

Guess you like

Origin blog.csdn.net/weixin_62173637/article/details/130233118