CentOS 7에서 PHP 및 MySQL(LEMP 스택)과 함께 Nginx를 설치하는 방법

CentOS 7에서 PHP 및 MySQL(LEMP 스택)과 함께 Nginx를 설치하는 방법

2022-10-19 last update

7 minutes reading centos nginx php linux mysql web server
Nginx("엔진 x"로 발음)은 무료 오픈 소스 고성능 HTTP 서버입니다. Nginx는 안정성, 풍부한 기능 세트, 간단한 구성 및 낮은 리소스 소비로 유명합니다. 이 가이드에서는 PHP 지원(PHP-FPM을 통해) 및 MySQL(Mariadb)을 지원하는 CentOS 7 서버에 Nginx를 설치하는 방법을 보여줍니다.

1 서문


이 자습서에서는 IP 주소가 192.168.1.105인 호스트 이름 server1.example.com을 사용합니다. 이러한 설정은 사용자에 따라 다를 수 있으므로 적절한 경우 교체해야 합니다.

2 추가 리포지토리 활성화


Nginx는 공식 CentOS 리포지토리에서 사용할 수 없으므로 Nginx 프로젝트의 리포지토리를 포함하여 설치합니다.
vi /etc/yum.repos.d/nginx.repo
 [nginx]
     name=nginx repo
     baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
     gpgcheck=0
     enabled=1

3 MySQL 설치


먼저 Mariadb를 설치합니다. Mariadb는 MySQL의 무료 포크입니다. 셸에서 다음 명령을 실행합니다.
yum install mariadb mariadb-server net-tools
그런 다음 MySQL에 대한 시스템 시작 링크를 만들고(시스템이 부팅될 때마다 MySQL이 자동으로 시작되도록) MySQL 서버를 시작합니다.
systemctl enable mariadb.service
systemctl start mariadb.service
이제 네트워킹이 활성화되어 있는지 확인하십시오. 운영
netstat -tap | grep mysql
다음과 같이 표시되어야 합니다.
[[email protected] ~]# netstat -tap | grep mysql
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 10623/mysqld 

운영
mysql_secure_installation
루트 사용자의 비밀번호를 설정하려면(그렇지 않으면 누구나 MySQL 데이터베이스에 액세스할 수 있습니다!):
[[email protected] ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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] <-- ENTER
New password: <-- yourrootsqlpassword
Re-enter new password: <-- yourrootsqlpassword
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] <-- ENTER
... 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] <-- ENTER
... 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] <-- ENTER
- 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] <-- ENTER
... 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!
[[email protected] ~]#
[[email protected] ~]# mysql_secure_installation

4 Nginx 설치


Nginx는 nginx.org에서 패키지로 제공되며 다음과 같이 설치할 수 있습니다.
yum install nginx
그런 다음 nginx용 시스템 시작 링크를 만들고 시작합니다.
systemctl enable nginx.service
systemctl start nginx.service
이미 사용 중인 포트 80과 같은 오류가 발생할 가능성이 있습니다. 오류 메시지는 다음과 같습니다.
[[email protected] ~]# service nginx start
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [FAILED]
[[email protected] ~]#
그런 다음 아파치 서비스가 실행될 가능성이 있음을 의미합니다. 서비스를 중지하고 다음과 같이 NGINX에 대한 서비스를 추가로 시작합니다.
systemctl stop httpd.service
yum remove httpd
systemctl disable httpd.service
systemctl enable nginx.service
systemctl start nginx.service
방화벽에서 http 및 https 포트를 엽니다.
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
쉘의 결과 출력은 다음과 같습니다.
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]#
웹 서버의 IP 주소 또는 호스트 이름을 브라우저에 입력하면(예: http://192.168.1.105) nginx 시작 페이지가 표시됩니다.