[web-server] Centos7 LEMP install

Centos7 LEMP install

Nginx php mariadb install

 

 

nginx 설치전 system update 를 진행 합니다.

[root@centos74 ~]# yum update -y

 

 

nginx repo file 생성

[root@centos74 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

참고페이지: http://nginx.org/en/linux_packages.html#stable

 

yum list 확인

[root@centos74 ~]# yum clean all && yum list
[root@centos74 ~]# yum search nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.neowiz.com
 * extras: ftp.neowiz.com
 * updates: ftp.neowiz.com
============================================================================================================================= N/S matched: nginx ==============================================================================================================================
nginx-debug.x86_64 : debug version of nginx
nginx-debuginfo.x86_64 : Debug information for package nginx
nginx-module-geoip.x86_64 : nginx GeoIP dynamic modules
nginx-module-geoip-debuginfo.x86_64 : Debug information for package nginx-module-geoip
nginx-module-image-filter.x86_64 : nginx image filter dynamic module
nginx-module-image-filter-debuginfo.x86_64 : Debug information for package nginx-module-image-filter
nginx-module-njs.x86_64 : nginx nginScript dynamic modules
nginx-module-njs-debuginfo.x86_64 : Debug information for package nginx-module-njs
nginx-module-perl.x86_64 : nginx Perl dynamic module
nginx-module-perl-debuginfo.x86_64 : Debug information for package nginx-module-perl
nginx-module-xslt.x86_64 : nginx xslt dynamic module
nginx-module-xslt-debuginfo.x86_64 : Debug information for package nginx-module-xslt
nginx-nr-agent.noarch : New Relic agent for NGINX and NGINX Plus
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
nginx.x86_64 : High performance web server

  Name and summary matches only, use "search all" for everything.
[root@centos74 ~]#

 

nginx 

nginx install

[root@centos74 ~]# yum install -y nginx

 

 php71 

php71 install

[root@centos74 ~]# yum install -y epel-release
[root@centos74 ~]# rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@centos74 ~]# yum clean all && yum list
[root@centos74 ~]# yum update -y
[root@centos74 ~]# init 6
[root@centos74 ~]# yum-config-manager --enable remi-php71
[root@centos74 ~]# yum install -y php php-opcache php-mysql php-fpm php-gd \
php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel

 

mariadb

 

mariadb repo 확인 https://downloads.mariadb.org/mariadb/repositories/#mirror=kaist&distro=CentOS&distro_release=centos7-amd64–centos7&version=10.1

mariadb.repo 생성

[root@centos74 ~]# vi /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
[root@centos74 ~]# yum clean all && yum list

 

mariadb 10.1 설치

[root@centos74 ~]# yum install -y mariadb mariadb-server

 

mariadb 10.1 start & enable 

[root@centos74 ~]# systemctl start mariadb.service
[root@centos74 ~]# systemctl enable mariadb.service

 

mariadb secure_installation 실행

[root@centos74 ~]# /usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@centos74 ~]#

 

nginx 

 

nginx enable & start

[root@centos74 ~]# systemctl enable nginx
[root@centos74 ~]# systemctl start nginx

 

nginx 동작 확인

[root@centos74 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2017-12-24 13:57:28 KST; 2min 16s ago
     Docs: http://nginx.org/en/docs/
 Main PID: 27786 (nginx)
   CGroup: /system.slice/nginx.service
           ├─27786 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─27787 nginx: worker process

Dec 24 13:57:28 centos74 systemd[1]: Starting nginx - high performance web server...
Dec 24 13:57:28 centos74 nginx[27782]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 24 13:57:28 centos74 nginx[27782]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 24 13:57:28 centos74 systemd[1]: Started nginx - high performance web server.
[root@centos74 ~]#

 

접속 확인

 

nginx 설정

nginx.conf 파일 

위치: /etc/nginx/nginx.conf (nginx main 설정 파일)

[root@centos74 nginx]# cat nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;

    include /etc/nginx/conf.d/*.conf;
}
[root@centos74 nginx]#

 

 

 

default.conf 파일

위치: /etc/nginx/conf.d/default.conf (nginx Default Server 설정 파일)

추가 도메인이 있을경우 /etc/nginx/conf.d/ 디렉토리에 $file_name.conf 로 파일을 생성하시면 됩니다.

[root@centos74 ~]# cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }


    #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;
    #}
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # 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;
    #}
}


[root@centos74 ~]#

sock 방식을 사용할 경우 해당 라인의 주석을 제거합니다. fastcgi_pass unix:/run/php-fpm/php-fpm.sock;

www.conf 파일을  listen = /run/php-fpm/php-fpm.sock; 로 listen = 127.0.0.1:9000 라인을 수정 해야 합니다.

 

php-fpm 설정

[root@centos74 html]# vi /etc/php-fpm.d/www.conf
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx


listen.owner = nginx
listen.group = nginx
listen.mode = 0666
[root@centos74 ~]# systemctl enable php-fpm.service
[root@centos74 ~]# systemctl start php-fpm.service
[root@centos74 ~]# systemctl restart nginx

 

phpinfo() 확인

[root@centos74 ~]# vi /usr/share/nginx/html/info.php
<?php phpinfo(); ?>

 

 

VirtualHost 

default 파일 수정

[root@centos74 ~]# vi /etc/nginx/conf.d/default.conf
server {
    listen       80 default_server;
    server_name  localhost;

 

/etc/nginx/sites-enabled 디렉토리 생성 없이 /etc/nginx/conf.d 디렉토리에 $domain.conf 파일로 생성 합니다.

test2.com 파일을 생성 합니다.

[root@centos74 ~]# vi /etc/nginx/conf.d/test2.conf
server {
    listen       80;
    server_name  www.test2.com test2.com;
    root   /var/www/test2.com;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        autoindex on;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/test2.com;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

 

test2.com 의 root 디렉토리는 /home/test/public_html 입니다.

phpinfo 페이지 파일을 생성 합니다.

[root@centos74 ~]# vi /var/www/test2.com/test.php
<?php phpinfo(); ?>

 

nginx 재시작후 확인 합니다.

[root@centos74 ~]# systemctl restart nginx

 

 

 

 

댓글 남기기