相信技术的力量

阿里云Centos7服务器-JavaWeb正式环境搭建

服务器系统为阿里云Centos7的linux系统,用于部署JavaWeb项目,记录下搭建步骤:

  • 挂载数据盘
  • 安装MySQL
  • 安装JDK
  • 安装Tomcat
  • Nginx配置

挂载数据盘

这部分内容阿里云官方已有详尽的教程,链接直达:

Linux 格式化和挂载数据盘

MySQL

通过YUM方式安装MySQL5.7版本

1.下载YUM库

命令: wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

/usr/local/创建一个空文件夹,比如dev,命令:mkdir dev,然后cd进入该目录,执行下载YUM库的命令。控制台输出记录:

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
--2018-01-15 14:41:41--  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm [following]
--2018-01-15 14:41:41--  https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm [following]
--2018-01-15 14:41:44--  https://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.211.97.88
Connecting to repo.mysql.com (repo.mysql.com)|23.211.97.88|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8984 (8.8K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-7.noarch.rpm’

100%[====================================================================================================>] 8,984       --.-K/s   in 0s      

2018-01-15 14:41:44 (196 MB/s) - ‘mysql57-community-release-el7-7.noarch.rpm’ saved [8984/8984]

下载完成后,通过ls命令,可以查看到当前目录已经有MySQL5.7的rpm包

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# ls
mysql57-community-release-el7-7.noarch.rpm

2.安装YUM库

命令:yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# yum localinstall -y mysql57-community-release-el7-7.noarch.rpm 
Loaded plugins: fastestmirror
Examining mysql57-community-release-el7-7.noarch.rpm: mysql57-community-release-el7-7.noarch
Marking mysql57-community-release-el7-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================
 Package                                 Arch                 Version             Repository                                             Size
==============================================================================================================================================
Installing:
 mysql57-community-release               noarch               el7-7               /mysql57-community-release-el7-7.noarch               7.8 k

Transaction Summary
==============================================================================================================================================
Install  1 Package

Total size: 7.8 k
Installed size: 7.8 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-7.noarch                                                                                     1/1 
  Verifying  : mysql57-community-release-el7-7.noarch                                                                                     1/1 

Installed:
  mysql57-community-release.noarch 0:el7-7                                                                                                    

Complete!
[root@iZbp1in0wc4jzpx7hkho6sZ dev]#

3.安装MySQL数据库

命令:yum install -y mysql-community-server

控制台输出:

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# yum install -y mysql-community-server
Loaded plugins: fastestmirror
base                                                                                                                   | 3.6 kB  00:00:00     
epel                                                                                                                   | 4.7 kB  00:00:00     
extras                                                                                                                 | 3.4 kB  00:00:00     
mysql-connectors-community                                                                                             | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                  | 2.5 kB  00:00:00     
mysql57-community                                                                                                      | 2.5 kB  00:00:00     
updates                                                                                                                | 3.4 kB  00:00:00     
(1/10): base/7/x86_64/group_gz                                                                                         | 156 kB  00:00:00     
(2/10): epel/x86_64/group_gz                                                                                           | 266 kB  00:00:00     
(3/10): epel/x86_64/updateinfo                                                                                         | 869 kB  00:00:00     
(4/10): extras/7/x86_64/primary_db                                                                                     | 145 kB  00:00:00     
(5/10): mysql-connectors-community/x86_64/primary_db                                                                   |  16 kB  00:00:00     
(6/10): epel/x86_64/primary_db                                                                                         | 6.2 MB  00:00:00     
(7/10): mysql-tools-community/x86_64/primary_db                                                                        |  37 kB  00:00:00     
(8/10): mysql57-community/x86_64/primary_db                                                                            | 124 kB  00:00:00     
(9/10): updates/7/x86_64/primary_db                                                                                    | 5.2 MB  00:00:00     
(10/10): base/7/x86_64/primary_db                                                                                      | 5.7 MB  00:00:00     
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.20-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.20-1.el7 for package: mysql-community-server-5.7.20-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.20-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: mysql-community-server-5.7.20-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: mysql-community-server-5.7.20-1.el7.x86_64
--> Processing Dependency: libaio.so.1()(64bit) for package: mysql-community-server-5.7.20-1.el7.x86_64
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
---> Package mysql-community-client.x86_64 0:5.7.20-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.20-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.20-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.20-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.20-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================
 Package                                     Arch                   Version                           Repository                         Size
==============================================================================================================================================
Installing:
 mysql-community-libs                        x86_64                 5.7.20-1.el7                      mysql57-community                 2.1 M
     replacing  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-libs-compat                 x86_64                 5.7.20-1.el7                      mysql57-community                 2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-server                      x86_64                 5.7.20-1.el7                      mysql57-community                 164 M
Installing for dependencies:
 libaio                                      x86_64                 0.3.109-13.el7                    base                               24 k
 mysql-community-client                      x86_64                 5.7.20-1.el7                      mysql57-community                  24 M
 mysql-community-common                      x86_64                 5.7.20-1.el7                      mysql57-community                 272 k

Transaction Summary
==============================================================================================================================================
Install  3 Packages (+3 Dependent packages)

Total download size: 192 M
Downloading packages:
(1/6): libaio-0.3.109-13.el7.x86_64.rpm                                                                                |  24 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.7.20-1.el7.x86_64.rpm is not installed
(2/6): mysql-community-common-5.7.20-1.el7.x86_64.rpm                                                                  | 272 kB  00:00:00     
(3/6): mysql-community-libs-5.7.20-1.el7.x86_64.rpm                                                                    | 2.1 MB  00:00:00     
(4/6): mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm                                                             | 2.0 MB  00:00:00     
(5/6): mysql-community-client-5.7.20-1.el7.x86_64.rpm                                                                  |  24 MB  00:00:02     
(6/6): mysql-community-server-5.7.20-1.el7.x86_64.rpm                                                                  | 164 MB  00:00:23     
----------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                         7.9 MB/s | 192 MB  00:00:24     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-7.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.20-1.el7.x86_64                                                                                 1/7 
  Installing : mysql-community-libs-5.7.20-1.el7.x86_64                                                                                   2/7 
  Installing : mysql-community-client-5.7.20-1.el7.x86_64                                                                                 3/7 
  Installing : libaio-0.3.109-13.el7.x86_64                                                                                               4/7 
  Installing : mysql-community-server-5.7.20-1.el7.x86_64                                                                                 5/7 
  Installing : mysql-community-libs-compat-5.7.20-1.el7.x86_64                                                                            6/7 
  Erasing    : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                         7/7 
  Verifying  : mysql-community-libs-5.7.20-1.el7.x86_64                                                                                   1/7 
  Verifying  : mysql-community-server-5.7.20-1.el7.x86_64                                                                                 2/7 
  Verifying  : mysql-community-common-5.7.20-1.el7.x86_64                                                                                 3/7 
  Verifying  : mysql-community-client-5.7.20-1.el7.x86_64                                                                                 4/7 
  Verifying  : mysql-community-libs-compat-5.7.20-1.el7.x86_64                                                                            5/7 
  Verifying  : libaio-0.3.109-13.el7.x86_64                                                                                               6/7 
  Verifying  : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                         7/7 

Installed:
  mysql-community-libs.x86_64 0:5.7.20-1.el7  mysql-community-libs-compat.x86_64 0:5.7.20-1.el7  mysql-community-server.x86_64 0:5.7.20-1.el7 

Dependency Installed:
  libaio.x86_64 0:0.3.109-13.el7       mysql-community-client.x86_64 0:5.7.20-1.el7       mysql-community-common.x86_64 0:5.7.20-1.el7      

Replaced:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                                                                                          

Complete!
[root@iZbp1in0wc4jzpx7hkho6sZ dev]# 

查看MySQL是否安装成功

命令:yum repolist enabled | grep "mysql.*-community.*"

控制台输出:

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                  42
mysql-tools-community/x86_64      MySQL Tools Community                       55
mysql57-community/x86_64          MySQL 5.7 Community Server                 227

4.启动MySQL服务

启动命令:systemctl start mysqld.service

启动完毕后,通过命令systemctl status mysqld查看MySQL的启动状态,控制台输出

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-01-15 14:45:39 CST; 1min 47s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 25374 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 25300 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 25378 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─25378 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jan 15 14:45:32 iZbp1in0wc4jzpx7hkho6sZ systemd[1]: Starting MySQL Server...
Jan 15 14:45:39 iZbp1in0wc4jzpx7hkho6sZ systemd[1]: Started MySQL Server.

5.修改MySQL默认密码

MySQL安装完毕后,会默认提供一个临时密码,查看MySQL的临时密码:

grep 'temporary password' /var/log/mysqld.log

控制台输出;

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# grep 'temporary password' /var/log/mysqld.log
2018-01-15T06:45:35.070084Z 1 [Note] A temporary password is generated for root@localhost: lyZmjDrcj0(q

这个lyZmjDrcj0(q就是MySQL的临时密码,用这个密码进行数据库登录,并修改密码。

> mysql -uroot -p
> Enterpassword:lyZmjDrcj0(q

登录成功后修改密码:

mysql > set password=password('Longke&Crocutax2018');

注意:

密码格式规则:大写字母+小写字母+数字+特殊字符

也可以修改密码规则,让MySQL不再检查密码强度,有很多同学上来就想设置个root或者123456,不推荐。

此外,不推荐使用root账号管理数据库,应该从访问地址,数据库访问权限,操作权限等角度缩小单个用户的权限范围。

添加数据库管理员

这里记录下给 【单个用户】从【任何地址】对【某个数据库】进行【所有操作】的权限。

mysql> grant all privileges on factory.* to 'crocutax'@'%' identified by 'Longke&180101';
Query OK, 0 rows affected, 1 warning (0.00 sec)

其他情况的调整,修改里面的参数即可。

另外,上面这句命令,同时也创建了一个用户。用户名为crocutax,密码为Longke&180101,可以用这个数据库账号从【本机】及【远程】访问factory数据库

修改MySQL默认端口号

MySQL数据库默认的访问端口号是3306,如果在阿里云后台安全规则组中,没有将此端口号添加为入网规则的话,那么从外网是无法访问的。不排除需要修改MySQL默认端口号的情况,这里记录下。

修改默认端口号需要编辑 /etc/my.conf配置文件,因为该文件是只读(read only)文件,所以必须以如下方式打开

sudo /etc/my.conf

在[mysqld]下方,添加自定义的端口号即可,比如

[mysqld]
port=8180
...

当然,还要确保该端口已经在阿里云后台入网安全规则中添加.

之后重启mysql服务即可

service mysqld restart

至此,MySQL部署完毕。

参考链接,感谢前辈:

http://www.linuxidc.com/Linux/2016-08/134790.htm

http://blog.csdn.net/csdn2193714269/article/details/72897815

JDK

JDK的安装非常简单,三步骤:

  • 下载安装包
  • 解压
  • 配置环境变量

下载和解压就不说了,记录下环境变量配置:

1.编辑配置文件,命令:vim /etc/profile

在配置文件底部添加

JAVA_HOME=/usr/local/dev/jdk1.8.0_152
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export JRE_HOME
export PATH
export CLASSPATH

ESC+:wq保存

2.刷新配置文件,命令:source /etc/profile

注意:非常关键,必须刷新,修改的配置才能生效。

3.查看是否配置成功,命令:java -version

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

参考链接:
极客学院-JDK安装

Tomcat

必须先安装JDK,再安装Tomcat,否则即使安装了Tomcat,由于缺少Java环境,也无法正常启动

三步骤:

  • 下载安装包
  • 解压
  • 启动tomcat

tomcat的启动和关闭

//启动
/usr/local/dev/tomcat8_erp/bin/startup.sh 
//关闭
/usr/local/dev/tomcat8_erp/bin/shutdown.sh 

启动成功

[root@iZbp1in0wc4jzpx7hkho6sZ dev]# /usr/local/dev/tomcat8_erp/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/dev/tomcat8_erp
Using CATALINA_HOME:   /usr/local/dev/tomcat8_erp
Using CATALINA_TMPDIR: /usr/local/dev/tomcat8_erp/temp
Using JRE_HOME:        /usr/local/dev/jdk1.8.0_152/jre
Using CLASSPATH:       /usr/local/dev/tomcat8_erp/bin/bootstrap.jar:/usr/local/dev/tomcat8_erp/bin/tomcat-juli.jar
Tomcat started.

之后就可以通过【公网IP+端口号】来访问tomcat首页了,比如:http://1.2.3.4.:8080

如果发现tomcat已启动,但是外网无法访问tomcat,可参考

linux下启动tomcat后-浏览器无法访问

参考链接:
http://www.cnblogs.com/hanyinglong/p/5024643.html

Nginx

这里记录的是yum源安装,编译安装可参考:

https://www.linuxidc.com/Linux/2016-09/134907.htm

配置YUM源,下载rpm包

wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
rpm -ivh epel-release-latest-7.noarch.rpm

查看下载结果:

[root@izbp109iqt20o2h63tpcuvz java]# ls
epel-release-latest-7.noarch.rpm

安装Nginx

yum install nginx -y

安装过程中的log输出:

[root@izbp109iqt20o2h63tpcuvz java]# yum install nginx -y
Loaded plugins: fastestmirror
base                                                                                                                   | 3.6 kB  00:00:00     
epel                                                                                                                   | 4.7 kB  00:00:00     
extras                                                                                                                 | 3.4 kB  00:00:00     
mysql-connectors-community                                                                                             | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                  | 2.5 kB  00:00:00     
mysql57-community                                                                                                      | 2.5 kB  00:00:00     
updates                                                                                                                | 3.4 kB  00:00:00     
(1/7): epel/x86_64/group_gz                                                                                            | 266 kB  00:00:00     
(2/7): epel/x86_64/updateinfo                                                                                          | 874 kB  00:00:00     
(3/7): extras/7/x86_64/primary_db                                                                                      | 145 kB  00:00:00     
(4/7): epel/x86_64/primary_db                                                                                          | 6.2 MB  00:00:00     
(5/7): updates/7/x86_64/primary_db                                                                                     | 5.2 MB  00:00:00     
(6/7): mysql-tools-community/x86_64/primary_db                                                                         |  37 kB  00:00:00     
(7/7): mysql57-community/x86_64/primary_db                                                                             | 134 kB  00:00:01     
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.12.2-1.el7 will be installed
--> Processing Dependency: nginx-all-modules = 1:1.12.2-1.el7 for package: 1:nginx-1.12.2-1.el7.x86_64
--> Processing Dependency: nginx-filesystem = 1:1.12.2-1.el7 for package: 1:nginx-1.12.2-1.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-1.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-1.el7.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.4-8.el7 will be installed
--> Processing Dependency: libunwind.so.8()(64bit) for package: gperftools-libs-2.4-8.el7.x86_64
---> Package nginx-all-modules.noarch 1:1.12.2-1.el7 will be installed
--> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
--> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
--> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
--> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
--> Processing Dependency: nginx-mod-mail = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
--> Processing Dependency: nginx-mod-stream = 1:1.12.2-1.el7 for package: 1:nginx-all-modules-1.12.2-1.el7.noarch
---> Package nginx-filesystem.noarch 1:1.12.2-1.el7 will be installed
--> Running transaction check
---> Package libunwind.x86_64 2:1.2-2.el7 will be installed
---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-1.el7 will be installed
---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-1.el7 will be installed
--> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-1.el7.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-1.el7.x86_64
---> Package nginx-mod-http-perl.x86_64 1:1.12.2-1.el7 will be installed
---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-1.el7 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64
---> Package nginx-mod-mail.x86_64 1:1.12.2-1.el7 will be installed
---> Package nginx-mod-stream.x86_64 1:1.12.2-1.el7 will be installed
--> Running transaction check
---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libpng15.so.15()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
--> Running transaction check
---> Package fontconfig.x86_64 0:2.10.95-11.el7 will be installed
--> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.10.95-11.el7.x86_64
--> Processing Dependency: font(:lang=en) for package: fontconfig-2.10.95-11.el7.x86_64
---> Package libX11.x86_64 0:1.6.5-1.el7 will be installed
--> Processing Dependency: libX11-common >= 1.6.5-1.el7 for package: libX11-1.6.5-1.el7.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-1.el7.x86_64
---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.90-5.el7 will be installed
---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed
--> Running transaction check
---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed
---> Package libX11-common.noarch 0:1.6.5-1.el7 will be installed
---> Package libxcb.x86_64 0:1.12-1.el7 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.12-1.el7.x86_64
---> Package lyx-fonts.noarch 0:2.2.3-1.el7 will be installed
--> Running transaction check
---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================
 Package                                        Arch                      Version                               Repository               Size
==============================================================================================================================================
Installing:
 nginx                                          x86_64                    1:1.12.2-1.el7                        epel                    529 k
Installing for dependencies:
 fontconfig                                     x86_64                    2.10.95-11.el7                        base                    229 k
 fontpackages-filesystem                        noarch                    1.44-8.el7                            base                    9.9 k
 gd                                             x86_64                    2.0.35-26.el7                         base                    146 k
 gperftools-libs                                x86_64                    2.4-8.el7                             base                    272 k
 libX11                                         x86_64                    1.6.5-1.el7                           base                    606 k
 libX11-common                                  noarch                    1.6.5-1.el7                           base                    164 k
 libXau                                         x86_64                    1.0.8-2.1.el7                         base                     29 k
 libXpm                                         x86_64                    3.5.12-1.el7                          base                     55 k
 libjpeg-turbo                                  x86_64                    1.2.90-5.el7                          base                    134 k
 libpng                                         x86_64                    2:1.5.13-7.el7_2                      base                    213 k
 libunwind                                      x86_64                    2:1.2-2.el7                           base                     57 k
 libxcb                                         x86_64                    1.12-1.el7                            base                    211 k
 libxslt                                        x86_64                    1.1.28-5.el7                          base                    242 k
 lyx-fonts                                      noarch                    2.2.3-1.el7                           epel                    159 k
 nginx-all-modules                              noarch                    1:1.12.2-1.el7                        epel                     16 k
 nginx-filesystem                               noarch                    1:1.12.2-1.el7                        epel                     17 k
 nginx-mod-http-geoip                           x86_64                    1:1.12.2-1.el7                        epel                     23 k
 nginx-mod-http-image-filter                    x86_64                    1:1.12.2-1.el7                        epel                     26 k
 nginx-mod-http-perl                            x86_64                    1:1.12.2-1.el7                        epel                     35 k
 nginx-mod-http-xslt-filter                     x86_64                    1:1.12.2-1.el7                        epel                     25 k
 nginx-mod-mail                                 x86_64                    1:1.12.2-1.el7                        epel                     53 k
 nginx-mod-stream                               x86_64                    1:1.12.2-1.el7                        epel                     76 k

Transaction Summary
==============================================================================================================================================
Install  1 Package (+22 Dependent packages)

Total download size: 3.3 M
Installed size: 9.8 M
Downloading packages:
(1/23): fontpackages-filesystem-1.44-8.el7.noarch.rpm                                                                  | 9.9 kB  00:00:00     
(2/23): fontconfig-2.10.95-11.el7.x86_64.rpm                                                                           | 229 kB  00:00:00     
(3/23): gd-2.0.35-26.el7.x86_64.rpm                                                                                    | 146 kB  00:00:00     
(4/23): gperftools-libs-2.4-8.el7.x86_64.rpm                                                                           | 272 kB  00:00:00     
(5/23): libX11-1.6.5-1.el7.x86_64.rpm                                                                                  | 606 kB  00:00:00     
(6/23): libX11-common-1.6.5-1.el7.noarch.rpm                                                                           | 164 kB  00:00:00     
(7/23): libXau-1.0.8-2.1.el7.x86_64.rpm                                                                                |  29 kB  00:00:00     
(8/23): libXpm-3.5.12-1.el7.x86_64.rpm                                                                                 |  55 kB  00:00:00     
(9/23): libjpeg-turbo-1.2.90-5.el7.x86_64.rpm                                                                          | 134 kB  00:00:00     
(10/23): libpng-1.5.13-7.el7_2.x86_64.rpm                                                                              | 213 kB  00:00:00     
(11/23): libunwind-1.2-2.el7.x86_64.rpm                                                                                |  57 kB  00:00:00     
(12/23): libxcb-1.12-1.el7.x86_64.rpm                                                                                  | 211 kB  00:00:00     
(13/23): libxslt-1.1.28-5.el7.x86_64.rpm                                                                               | 242 kB  00:00:00     
(14/23): lyx-fonts-2.2.3-1.el7.noarch.rpm                                                                              | 159 kB  00:00:00     
(15/23): nginx-all-modules-1.12.2-1.el7.noarch.rpm                                                                     |  16 kB  00:00:00     
(16/23): nginx-filesystem-1.12.2-1.el7.noarch.rpm                                                                      |  17 kB  00:00:00     
(17/23): nginx-mod-http-geoip-1.12.2-1.el7.x86_64.rpm                                                                  |  23 kB  00:00:00     
(18/23): nginx-1.12.2-1.el7.x86_64.rpm                                                                                 | 529 kB  00:00:00     
(19/23): nginx-mod-http-image-filter-1.12.2-1.el7.x86_64.rpm                                                           |  26 kB  00:00:00     
(20/23): nginx-mod-http-perl-1.12.2-1.el7.x86_64.rpm                                                                   |  35 kB  00:00:00     
(21/23): nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64.rpm                                                            |  25 kB  00:00:00     
(22/23): nginx-mod-stream-1.12.2-1.el7.x86_64.rpm                                                                      |  76 kB  00:00:00     
(23/23): nginx-mod-mail-1.12.2-1.el7.x86_64.rpm                                                                        |  53 kB  00:00:00     
----------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                         9.2 MB/s | 3.3 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : fontpackages-filesystem-1.44-8.el7.noarch                                                                                 1/23 
  Installing : lyx-fonts-2.2.3-1.el7.noarch                                                                                              2/23 
  Installing : fontconfig-2.10.95-11.el7.x86_64                                                                                          3/23 
  Installing : libXau-1.0.8-2.1.el7.x86_64                                                                                               4/23 
  Installing : libxcb-1.12-1.el7.x86_64                                                                                                  5/23 
  Installing : libX11-common-1.6.5-1.el7.noarch                                                                                          6/23 
  Installing : libX11-1.6.5-1.el7.x86_64                                                                                                 7/23 
  Installing : libXpm-3.5.12-1.el7.x86_64                                                                                                8/23 
  Installing : 2:libunwind-1.2-2.el7.x86_64                                                                                              9/23 
  Installing : gperftools-libs-2.4-8.el7.x86_64                                                                                         10/23 
  Installing : libxslt-1.1.28-5.el7.x86_64                                                                                              11/23 
  Installing : 2:libpng-1.5.13-7.el7_2.x86_64                                                                                           12/23 
  Installing : 1:nginx-filesystem-1.12.2-1.el7.noarch                                                                                   13/23 
  Installing : libjpeg-turbo-1.2.90-5.el7.x86_64                                                                                        14/23 
  Installing : gd-2.0.35-26.el7.x86_64                                                                                                  15/23 
  Installing : 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64                                                                         16/23 
  Installing : 1:nginx-mod-stream-1.12.2-1.el7.x86_64                                                                                   17/23 
  Installing : 1:nginx-mod-http-geoip-1.12.2-1.el7.x86_64                                                                               18/23 
  Installing : 1:nginx-mod-http-perl-1.12.2-1.el7.x86_64                                                                                19/23 
  Installing : 1:nginx-mod-mail-1.12.2-1.el7.x86_64                                                                                     20/23 
  Installing : 1:nginx-1.12.2-1.el7.x86_64                                                                                              21/23 
  Installing : 1:nginx-mod-http-image-filter-1.12.2-1.el7.x86_64                                                                        22/23 
  Installing : 1:nginx-all-modules-1.12.2-1.el7.noarch                                                                                  23/23 
  Verifying  : libX11-1.6.5-1.el7.x86_64                                                                                                 1/23 
  Verifying  : lyx-fonts-2.2.3-1.el7.noarch                                                                                              2/23 
  Verifying  : libjpeg-turbo-1.2.90-5.el7.x86_64                                                                                         3/23 
  Verifying  : 1:nginx-filesystem-1.12.2-1.el7.noarch                                                                                    4/23 
  Verifying  : 2:libpng-1.5.13-7.el7_2.x86_64                                                                                            5/23 
  Verifying  : 1:nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64                                                                          6/23 
  Verifying  : 1:nginx-all-modules-1.12.2-1.el7.noarch                                                                                   7/23 
  Verifying  : fontpackages-filesystem-1.44-8.el7.noarch                                                                                 8/23 
  Verifying  : 1:nginx-1.12.2-1.el7.x86_64                                                                                               9/23 
  Verifying  : 1:nginx-mod-stream-1.12.2-1.el7.x86_64                                                                                   10/23 
  Verifying  : 1:nginx-mod-http-image-filter-1.12.2-1.el7.x86_64                                                                        11/23 
  Verifying  : libXpm-3.5.12-1.el7.x86_64                                                                                               12/23 
  Verifying  : 1:nginx-mod-http-geoip-1.12.2-1.el7.x86_64                                                                               13/23 
  Verifying  : libxcb-1.12-1.el7.x86_64                                                                                                 14/23 
  Verifying  : gperftools-libs-2.4-8.el7.x86_64                                                                                         15/23 
  Verifying  : libxslt-1.1.28-5.el7.x86_64                                                                                              16/23 
  Verifying  : gd-2.0.35-26.el7.x86_64                                                                                                  17/23 
  Verifying  : 2:libunwind-1.2-2.el7.x86_64                                                                                             18/23 
  Verifying  : 1:nginx-mod-http-perl-1.12.2-1.el7.x86_64                                                                                19/23 
  Verifying  : libX11-common-1.6.5-1.el7.noarch                                                                                         20/23 
  Verifying  : libXau-1.0.8-2.1.el7.x86_64                                                                                              21/23 
  Verifying  : fontconfig-2.10.95-11.el7.x86_64                                                                                         22/23 
  Verifying  : 1:nginx-mod-mail-1.12.2-1.el7.x86_64                                                                                     23/23 

Installed:
  nginx.x86_64 1:1.12.2-1.el7                                                                                                                 

Dependency Installed:
  fontconfig.x86_64 0:2.10.95-11.el7                                 fontpackages-filesystem.noarch 0:1.44-8.el7                              
  gd.x86_64 0:2.0.35-26.el7                                          gperftools-libs.x86_64 0:2.4-8.el7                                       
  libX11.x86_64 0:1.6.5-1.el7                                        libX11-common.noarch 0:1.6.5-1.el7                                       
  libXau.x86_64 0:1.0.8-2.1.el7                                      libXpm.x86_64 0:3.5.12-1.el7                                             
  libjpeg-turbo.x86_64 0:1.2.90-5.el7                                libpng.x86_64 2:1.5.13-7.el7_2                                           
  libunwind.x86_64 2:1.2-2.el7                                       libxcb.x86_64 0:1.12-1.el7                                               
  libxslt.x86_64 0:1.1.28-5.el7                                      lyx-fonts.noarch 0:2.2.3-1.el7                                           
  nginx-all-modules.noarch 1:1.12.2-1.el7                            nginx-filesystem.noarch 1:1.12.2-1.el7                                   
  nginx-mod-http-geoip.x86_64 1:1.12.2-1.el7                         nginx-mod-http-image-filter.x86_64 1:1.12.2-1.el7                        
  nginx-mod-http-perl.x86_64 1:1.12.2-1.el7                          nginx-mod-http-xslt-filter.x86_64 1:1.12.2-1.el7                         
  nginx-mod-mail.x86_64 1:1.12.2-1.el7                               nginx-mod-stream.x86_64 1:1.12.2-1.el7                                   

Complete!
[root@izbp109iqt20o2h63tpcuvz java]# 

查看安装结果

rpm -qa | grep nginx

控制台输出:

[root@izbp109iqt20o2h63tpcuvz java]# rpm -qa | grep nginx
nginx-mod-http-perl-1.12.2-1.el7.x86_64
nginx-all-modules-1.12.2-1.el7.noarch
nginx-mod-http-xslt-filter-1.12.2-1.el7.x86_64
nginx-mod-mail-1.12.2-1.el7.x86_64
nginx-filesystem-1.12.2-1.el7.noarch
nginx-mod-stream-1.12.2-1.el7.x86_64
nginx-1.12.2-1.el7.x86_64
nginx-mod-http-geoip-1.12.2-1.el7.x86_64
nginx-mod-http-image-filter-1.12.2-1.el7.x86_64

配置

启动Nginx

[root@izbp109iqt20o2h63tpcuvz java]# systemctl start nginx

查看启动结果

注意绿色的 active (running),表明启动成功

查看启动状态
[root@izbp109iqt20o2h63tpcuvz java]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-01-23 19:36:18 CST; 29s ago
 Main PID: 24724 (nginx)
   CGroup: /system.slice/nginx.service
           ├─24724 nginx: master process /usr/sbin/nginx
           ├─24725 nginx: worker process
           ├─24726 nginx: worker process
           ├─24727 nginx: worker process
           └─24728 nginx: worker process

Jan 23 19:36:18 izbp109iqt20o2h63tpcuvz systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 23 19:36:18 izbp109iqt20o2h63tpcuvz nginx[24719]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 23 19:36:18 izbp109iqt20o2h63tpcuvz nginx[24719]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 23 19:36:18 izbp109iqt20o2h63tpcuvz systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@izbp109iqt20o2h63tpcuvz java]# 

设置Nginx开机启动

[root@izbp109iqt20o2h63tpcuvz java]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

测试Nginx

Nginx默认端口号为80,一般情况下服务器的80端口也是开放的,因此在直接输入主机公网IP地址即可看见Nginx的主界面

Welcome to nginx on Fedora!

表明配置成功。

默认页面位于/usr/share/nginx/html/index.html,而/usr/share/nginx/html是Nginx默认的根目录。如果觉得不方便,可以在配置文件中修改根目录位置,同时注意由于目录权限问题导致的403Forbidden

配置文件说明

配置文件路径 /etc/nginx/nginx.conf

#运行用户
user nginx;
#启动进程,通常设置成和 cpu 的数量相等
worker_processes 1;

#全局错误日志及 PID 文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;

#工作模式及连接数上限
events {
#epoll 是多路复用 IO(I/O Multiplexing)中的一种方式,
#仅用于 linux2.6 以上内核,可以大大提高 nginx 的性能
use epoll;
#单个后台 worker process 进程的最大并发链接数
worker_connections 1024;
# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下, max_clients = worker_processes * worker_connections / 4

为什么
# 为什么上面反向代理要除以 4,应该说是一个经验值
# 根据以上条件,正常情况下的 Nginx Server 可以应付的最大连接数为: 4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受 IO 约束, max_clients 的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般 1GB 内存的机器上可以打开的文件数大约是
10 万左右
# 我们来看看 360M 内存的 VPS 可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受
的范围之内
# 所以, worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文
件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理 CPU 和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535
}
http {
    #设定 mime 类型,类型由 mime.type 文件定义
    include 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 logs/access.log main;
    #sendfile 指令指定 nginx 是否调用 sendfile 函数( zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,
    #以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
    sendfile on;
    #tcp_nopush on;
    #连接超时时间
    #keepalive_timeout 0;
    keepalive_timeout 65;
    tcp_nodelay on;

    #开启 gzip 压缩
    gzip on;
    gzip_disable "MSIE [1-6].";
    #nginx 传输文件大小,默认为1M

    client_max_body_size 20m;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;

    #设定请求缓冲
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;

    #设定虚拟主机配置
    server {
        #侦听 80 端口
        listen 80;
        #定义使用 www.linuxidc.com 访问
        server_name www.linuxidc.com;
        #定义服务器的默认网站根目录位置
        root html;
        #设定本虚拟主机的访问日志
        access_log logs/nginx.access.log main;
        #默认请求
        location / {
        #定义首页索引文件的名称
            index index.php index.html index.htm;
        }
        # 定义错误提示页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
        } 
        # 静态文件, nginx 自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            #过期 30 天,静态文件不怎么更新,过期可以设大一点,
            #如果频繁更新,则可以设置得小一点。
            expires 30d;
        }
        #PHP 脚本请求全部转发到 FastCGI 处理. 使用 FastCGI 默认配置.

        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        } 
        # 禁止访问 .htxxx 文件
        location ~ /.ht {
            deny all;
        }
    }
}

参考链接,感谢前辈:

http://www.linuxidc.com/Linux/2017-04/142986.htm

(完)

⬆️