Home  >  Article  >  Operation and Maintenance  >  How to install nginx in a custom directory in centos8

How to install nginx in a custom directory in centos8

王林
王林forward
2023-05-18 18:16:061087browse

1. Install tools and libraries

# pcre is a perl library, including a perl-compatible regular expression library. The http module of nginx uses pcre to parse regular expressions

# The zlib library provides many compression and decompression methods. nginx uses zlib to gzip the contents of the http package

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

2. Directory structure

Source code directory:/home/werben/pkgsrc/nginx
Installation directory:/home/werben/application/nginx

3. Download Unzip the source code

# Official website address:
wget -c

4. Create user groups and users

groupadd www
useradd -g www www

5. Compile source code

./configure --user=www --group=www --prefix=/home/werben/application/nginx --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre

make && make install

6. Map global commands

ln -s /home/werben/application/nginx/sbin/nginx /usr/local/bin/nginx

7. Start, stop, restart

nginx -s stop
nginx -s quit
ngins -s reload

8. Check the correctness of the configuration file nginx.conf

##nginx -t

9.Auto-start at boot

vim /lib/systemd/system/nginx.service
[unit]
description=nginx
after=network.target

[service]
type=forking
execstart=nginx
execreload=nginx reload
execstop=nginx quit
privatetmp=true

[install]
wantedby=multi-user.target
#重新加载守护进程
systemctl daemon-reload

#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#设置开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service
#查看服务当前状态
systemctl status nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service

10. Problems and solutions

#如果`systemctl start nginx.service`提示如下报错

job for nginx.service failed because the control process exited with error code.
see "systemctl status nginx.service" and "journalctl -xe" for details.

#执行
systemctl status nginx.service
#如果出现如下错误
  process: 35783 execstart=...nginx/sbin/nginx(code=exitedstatus=203/exec)
  nginx.service: control process exited, code=exited status=203
  systemd[1]: nginx.service: failed with result 'exit-code'.
  localhost.localdomain systemd[1]: failed to start nginx.


journalctl -xe

#如果看到如下信息     
  if you believe that systemd should be allowed execute access on the>
  then you should report this as a bug.
  you can generate a local policy module to allow this access.
  do allow this access for now by executing:
  # ausearch -c '(nginx)' --raw | audit2allow -m my-nginx
  # semodule -x 300 -i my-nginx.pp


#解决方法
setenforce 0
vim /etc/selinux/config
selinux=disabled

ps: Structural description of nginx configuration file

All nginx configuration files are located in the /etc/nginx/ directory.


The main configuration file of nginx is /etc/nginx/nginx.conf.


Creating a separate configuration file for each domain makes the server easier to maintain.


nginx server blocking files must end with .conf and are stored in the /etc/nginx/conf.d directory. You can have as many server blocks as you need.


It is a good practice to follow standard naming conventions. For example, if the domain name is mydomain.com then the configuration file should be named mydomain.com.conf


If you are using repeatable configuration segments within a domain server block, it is best to refactor these segments into fragments .


nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have different access and error log files for each server module.


You can set the root of your domain document to any location you want. The most common locations for webroot include:


/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>
/usr/share/nginx/html

The above is the detailed content of How to install nginx in a custom directory in centos8. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete