如何在CentOS 7上安装带有PHP和MySQL(LEMP Stack)的Nginx
Nginx (发音为“引擎x”)是一个免费的,开放源码的高性能HTTP服务器。 Nginx以其稳定性,丰富的功能集,简单的配置和低资源消耗而闻名。 本教程将介绍如何在支持PHP(通过PHP-FPM )和MySQL(Mariadb)支持的CentOS 7服务器上安装Nginx。
1初步说明
在本教程中,我使用IP地址为192.168.1.105
的hostname 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的免费分支。 在shell上运行此命令:
yum install mariadb mariadb-server net-tools
然后,我们为MySQL创建系统启动链接(以便每当系统启动时,MySQL自动启动)并启动MySQL服务器:
systemctl enable mariadb.service
systemctl start mariadb.service
现在检查网络是否启用。 跑
netstat -tap | grep mysql
应该显示如下:
[root@example ~]# netstat -tap | grep mysql
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 10623/mysqld
跑
mysql_secure_installation
为用户root
设置密码(否则任何人都可以访问您的MySQL数据库!):
[root@example ~]# 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!
[root@example ~]#
[root @ server1〜]#mysql_secure_installation
4安装Nginx
Nginx可以从nginx.org提供,我们可以安装如下:
yum install nginx
然后我们为nginx创建系统启动链接并启动它:
systemctl enable nginx.service
systemctl start nginx.service
有机会你得到一个错误,如端口80已经在使用,错误信息将是这样的
[root@server1 ~]# 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]
[root@server1 ~]#
那么这意味着有可能在那里运行apache服务。 停止服务,并进一步开始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
shell上的结果输出如下所示:
[root@example ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@example ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@example ~]# firewall-cmd --reload
success
[root@example ~]#
在浏览器中输入您的Web服务器的IP地址或主机名(例如http://192.168.1.105
),您应该看到nginx的欢迎页面: