美文网首页
2019-07-16 安装NGINX+Mysql+PHP服务器

2019-07-16 安装NGINX+Mysql+PHP服务器

作者: GeekJaylen | 来源:发表于2021-09-24 21:22 被阅读0次

一、安装Nginx

1、切换安装源

我们先把yum源换成国内的阿里云镜像源(当然不换也可以),先备份一下原来的源镜像文件,以免出错后可以恢复:

[root@tuniao ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载新的CentOS-Base.repo 到/etc/yum.repos.d/,版本根据自己的系统版本选择下载:

CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

更改/etc/yum.repos.d/CentOS-Media.repo使其为不生效:

enabled=0

运行yum makecache生成缓存:

yum clean all
yum makecache
yum update

2、安装Nginx

由于yum源中没有我们想要的nginx,那么我们就需要创建一个“/etc/yum.repos.d/nginx.repo”的文件,其实就是新增一个yum源。

[root@tuniao yum.repos.d]# vim /etc/yum.repos.d/nginx.repo

把如下内容复制进去:

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

然后保存退出,输入 yum list nginx 查看

[root@tuniao ~]# yum list nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                | 3.6 kB  00:00:00     
epel                                                                                | 5.3 kB  00:00:00     
extras                                                                              | 3.4 kB  00:00:00     
nginx                                                                               | 2.9 kB  00:00:00     
updates                                                                             | 3.4 kB  00:00:00     
(1/3): epel/x86_64/updateinfo                                                       | 993 kB  00:00:00     
(2/3): epel/x86_64/primary_db                                                       | 6.8 MB  00:00:00     
(3/3): nginx/7/x86_64/primary_db                                                    |  46 kB  00:00:02     
Available Packages
nginx.x86_64                                    1:1.16.0-1.el7.ngx                                    nginx
[root@tuniao ~]# yum list |grep nginx
collectd-nginx.x86_64                   5.8.1-1.el7                    epel     
munin-nginx.noarch                      2.0.49-1.el7                   epel     
nextcloud-nginx.noarch                  10.0.4-2.el7                   epel     
nginx.x86_64                            1:1.16.0-1.el7.ngx             nginx    
nginx-all-modules.noarch                1:1.12.2-3.el7                 epel     
nginx-debug.x86_64                      1:1.8.0-1.el7.ngx              nginx    
nginx-debuginfo.x86_64                  1:1.16.0-1.el7.ngx             nginx    
nginx-filesystem.noarch                 1:1.12.2-3.el7                 epel     
nginx-mod-http-geoip.x86_64             1:1.12.2-3.el7                 epel     
nginx-mod-http-image-filter.x86_64      1:1.12.2-3.el7                 epel     
nginx-mod-http-perl.x86_64              1:1.12.2-3.el7                 epel     
nginx-mod-http-xslt-filter.x86_64       1:1.12.2-3.el7                 epel     
nginx-mod-mail.x86_64                   1:1.12.2-3.el7                 epel     
nginx-mod-stream.x86_64                 1:1.12.2-3.el7                 epel     
nginx-module-geoip.x86_64               1:1.16.0-1.el7.ngx             nginx    
nginx-module-geoip-debuginfo.x86_64     1:1.16.0-1.el7.ngx             nginx    
nginx-module-image-filter.x86_64        1:1.16.0-1.el7.ngx             nginx    
nginx-module-image-filter-debuginfo.x86_64
                                        1:1.16.0-1.el7.ngx             nginx    
nginx-module-njs.x86_64                 1:1.16.0.0.3.2-1.el7.ngx       nginx    
nginx-module-njs-debuginfo.x86_64       1:1.16.0.0.3.2-1.el7.ngx       nginx    
nginx-module-perl.x86_64                1:1.16.0-1.el7.ngx             nginx    
nginx-module-perl-debuginfo.x86_64      1:1.16.0-1.el7.ngx             nginx    
nginx-module-xslt.x86_64                1:1.16.0-1.el7.ngx             nginx    
nginx-module-xslt-debuginfo.x86_64      1:1.16.0-1.el7.ngx             nginx    
nginx-nr-agent.noarch                   2.0.0-12.el7.ngx               nginx    
owncloud-nginx.noarch                   9.1.5-1.el7                    epel     
pcp-pmda-nginx.x86_64                   4.1.0-5.el7_6                  updates  
python2-certbot-nginx.noarch            0.35.1-1.el7                   epel   

如果执行命令是这样的显示效果,那么我们的nginx的yum源就配置成功啦!

然后要安装我们的nginx就直接执行

yum -y install nginx

启动Nginx并且设置为开机启动,可以使用curl 127.0.0.1查看是否启动成功

[root@tuniao ~]# systemctl start nginx
[root@tuniao ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-07-16 13:13:58 CST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 5433 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 5434 (nginx)
   CGroup: /system.slice/nginx.service
           ├─5434 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─5435 nginx: worker process

Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: Starting nginx - high performance web server...
Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) a...rt.
Jul 16 13:13:58 iZwz961b3fgv6g8a3oongyZ systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@tuniao ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@iZwz961b3fgv6g8a3oongyZ ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

3、配置防火墙和阿里云的安全组

[root@tuniao ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@tuniao ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@tuniao ~]# firewall-cmd --reload 

二、安装MySQL数据库

1、安装 MySQL 源

yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2、安装 MySQL

yum install mysql-community-server

3、配置并启动 MySQL并设置为开机启动

[root@tuniao ~]# systemctl start mysqld
[root@tuniao ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-07-16 16:16:12 CST; 4s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 5985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5912 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 5989 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─5989 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 16 16:16:07 iZwz961b3fgv6g8a3oongyZ systemd[1]: Starting MySQL Server...
Jul 16 16:16:12 iZwz961b3fgv6g8a3oongyZ systemd[1]: Started MySQL Server.
[root@tuniao ~]# systemctl enable mysqld

获取密码

grep 'temporary password' /var/log/mysqld.log
得到这行 A temporary password is generated for root@localhost: Jqqskhz1Wr(:冒号后面的就是密码

进入 MySQL 对数据库的权限进行一定的修改

mysql -uroot -p
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY ********** (密码请用引号包起来 注意 MySQL 的密码必须复杂 不复杂会报错)
#开放远程访问权限
use mysql;
update user set host = '%' where user = 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
//远程连接数据库的时候需要输入用户名和密码
用户名:root
密码:123456
指点ip:%代表所有Ip,此处也可以输入Ip来指定Ip
输入后使修改生效还需要下面的语句
mysql>FLUSH PRIVILEGES;

修改数据库的默认端口3306
往vim /etc/my.cnf文件中添加port=【端口号】

port=6603
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

重启mysql服务器

[root@tuniao ~]# systemctl restart mysqld

防火墙打开3306端口

firewall-cmd --zone=public --add-port=6603/tcp --permanent 
firewall-cmd --reload

三、安装php

由于 linux 的 yum 源不存在 php7.x,所以我们要更改 yum 源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm   
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm    

yum 安装 php72w 和各种拓展,选自己需要的即可,由于apache和nginx的不一样,所以这里重点需要安装php-fpm

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

启动php-fpm并设置开机启动

systemctl start php-fpm
systemctl enable php-fpm

配置nginx的配置文件
修改/etc/nginx/nginx.conf的user为root


image.png

修改/etc/nginx/conf.d/default.conf里面的网站目录 root的值

location / {
        root   /home/vsftpd/httpuser/www/html;
        index  index.php index.html index.htm;
    }

location ~ \.php$ {
        root           /home/vsftpd/httpuser/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

重启nginx服务器即可

配置过程中遇到的问题

1、配置完成服务器后,对服务器进行访问的时候出现403 Forbidden

这个时候需要检查网站的文件夹以及对应的父级目录是否具有其他用户的可读可写权限
可以使用chmod -R命令对主目录以及其子目录和文件进行权限设置

就好像我们这里配置的网站主目录在vsftpd下的,由于vsftpd的文件对其他用户没有读写权限,所以需要使用

chmod -R 755 vsftpd/

进行权限的修改

2、如果网站下有.htaccess文件需要使用,需要对nginx的服务器配置文件添加以下内容

location ~ /\.ht {
    deny  all;
}

相关文章

网友评论

      本文标题:2019-07-16 安装NGINX+Mysql+PHP服务器

      本文链接:https://www.haomeiwen.com/subject/piozkctx.html