Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

kyh코딩 공부 블로그

centOS 7 nginx설치 방법 본문

가상서버

centOS 7 nginx설치 방법

킴용현 2023. 3. 14. 11:22

nginx를 설치할때 yum에는 nginx패키지가 없기 때문에 경로를 만들어준다.

cd etc/yum.repos.d/

파일을 하나 생성해주고

 vi nginx.repo

파일에 이렇게 적어준다.

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

다른 방법으로는 yum install epel-release를 설치하면 yum으로 위에 설정 없이 바로 nginx를 설치할 수 있다.

yum install epel-release

 

nginx를 설치해준다.

 //nginx 설치
 yum install -y nginx

 

포트를 개방해준다.

//포트 추가
//https
  firewall-cmd --permanent --zone=public --add-port=443/tcp
 //http
   firewall-cmd --permanent --zone=public --add-port=80/tcp
   
   //리로드
    firewall-cmd --reload
    
    //방화벽 확인
     firewall-cmd --list-ports

 

톰캣과 연결하는 부분

vi /etc/nginx/conf.d/default.conf

이렇게 입력을 해준다

//내가 사용할 ip를 입력 ifconfig으로 확인하고 넣어주면 된다.
upstream test_tomcat{
     server 가상머신의 ip:8080;
}
server {
    listen       80;
    //서버이름은 정확히는 모르지만 내 ip랑 localhost를 입력해주었다.
    server_name  가상머신ip localhost;

    #access_log  /var/log/nginx/host.access.log  main;

      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;

    location / {
    
        proxy_hide_header Access-Control-Allow-Origin;
        add_header 'Access-Control-Allow-Origin' '*';
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_set_header Host $http_host;
        //test_tomcat부분은 위에 지정한 upstream을 넣어주면 된다.
        proxy_pass http://test_tomcat;
        proxy_redirect off;
        charset utf-8;

}

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

default 설정을 다 했으면 /etc/nginx에 있는 nginx.conf파일을 설정해준다.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

//default.conf에 설정부분을 include하는 부분이다
    include /etc/nginx/conf.d/*.conf;
}
~

설정 완료후에 nginx를 구동하면 작동은 되지만 권한문제로 에러가 난다 에러 확인은

/var/log/nginx에 error.log가 있다

tail -f error.log를 입력하면 콘솔처럼 에러가 발생하면 에러가 보여질 것이다.(빠져 나올때는 ctrl + c를 입력)

 

권한 문제 해결

vi /etc/selinux/config 여기로 들어가서 표시된 부분을 변경하면 권한 에러가 없어질 것이다.

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  //요기 부분을 disabled로 변경
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

그후 해주면 nginx를 실행시키고 ip를 입력하면 잘 나올것이다.

systemctl start nginx

systemctl enable nginx

'가상서버' 카테고리의 다른 글

tomcat 실행 경로 이동  (0) 2023.03.17
서버 tomcat에 프로젝트 올리기  (2) 2023.03.14
CentOS에서 Tomcat 설치  (1) 2023.03.14
CentOS에서 Tomcat 설치 jdk설치  (0) 2023.03.14
virtualBox centOS 7 설치 방법  (0) 2023.03.14