Nginx是一款轻量级的 HTTP 服务器软件,由俄罗斯的 Igor Sysoev 开发,其最知名的优点是它的稳定性和低系 统资源消耗,以及对 HTTP 并发连接的高处理能力(单台物理服务器可支持 30000~50000个并发请求)。因此大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择 Nginx 来提供 Web 服务。
[root@localhost ~]# cd /opt
将nginx的软件包拷贝到当前目录下
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# tar zvxf nginx-1.12.2.tar.gz ##解压到当前文件夹
[root@localhost opt]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel -y ##安装一些软件编译包
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##创建一个管理用户
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \ ##配置安装信息
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module ##统计模块功能
[root@localhost nginx-1.12.2]# make && make install ##编译安装
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ ##建立软链接到/usr/local/bin/ 下,便于系统识别
[root@localhost nginx-1.12.2]# nginx -t ##这时候就可以使用这条命令检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# iptables -F ##清空防防火墙规则
[root@localhost conf]# setenforce 0 ##临时关闭增强型核心防护功能
[root@localhost conf]# nginx ##开启nginx服务
[root@localhost conf]# killall -s QUIT nginx ##关闭服务的命令
[root@localhost conf]# killall -s HUP nginx ##重新加载服务
为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写基于CentOS7.6的Nginx服务控制文件使用systemctl工具来进行管理CentOS7.6系统的管理习惯。
[root@localhost conf]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/kill -s HUP $MAINPID
ExecStop=/usr/local/kill -s QUIT $MAINPID
PrivateTmp=ture
[Install]
WantedBy=multi-user.target ##多用户模式
[root@localhost conf]# chmod 754 /lib/systemd/system/nginx.service ##赋予权限,我们就可以用systemctl命令管理nginx服务了
[root@localhost conf]# cd /opt
[root@localhost opt]# ls
mysql-boost-5.7.20.tar.gz nginx-1.12.2.tar.gz
nginx-1.12.2 rh
[root@localhost opt]# tar zvxf mysql-boost-5.7.20.tar.gz ##解压到当前目录
[root@localhost mysql-5.7.20]# yum install ncurses ncurses-devel bison cmake -y
[root@localhost mysql-5.7.20]# cmake \ ##编译配置
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[root@localhost mysql-5.7.20]# make ##编译
[root@localhost mysql-5.7.20]# make install ##安装
[root@localhost mysql-5.7.20]# useradd -s /sbin/nologin mysql ##创建一个数据库管理用户
[root@localhost mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql/ ##将myaql的属主、属组改为mysql
调整配置文件
[root@localhost mysql-5.7.20]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
设置环境变量
[root@localhost mysql-5.7.20]# vim /etc/profile ##在环境变量配置文件的末尾加入以下语句,声明环境变量
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
[root@localhost mysql-5.7.20]# source /etc/profile ##加载环境变量
初始数据库
[root@localhost mysql-5.7.20]# cd /usr/local/mysql
[root@localhost mysql]# bin/mysqld \
> --initialize-insecure \
> --user=mysql \
> --basedir=/usr/local/mysql \
> --datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ ##添加到systemctl管理
[root@localhost mysql]# systemctl start mysqld.service ##开启mysqld服务
[root@localhost mysql]# mysqladmin -u root -p password "abc123" ##为mysql用户创建密码
Enter password: ##直接回车
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
登录mysql
[root@localhost mysql]# mysql -u root -p ##登录数据库
Enter password: ##输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
...
省略部分内容
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; ##查看数据文件
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye
[root@localhost mysql]# cd /opt
将软件包拷贝进来
[root@localhost opt]# ls
mysql-5.7.20 nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
nginx-1.12.2 rh
[root@localhost opt]# tar jxvf php-7.1.10.tar.bz2 ##解压到当前目录
[root@localhost opt]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel ##安装环境依赖包
[root@localhost opt]# cd php-7.1.10/
[root@localhost php-7.1.10]# ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
[root@localhost php-7.1.10]# make && make install ##编译安装
php有三个配置文件php.ini核心配置文件php-fpm.conf进程服务配置文件www.conf 扩展配置文件
修改核心配置文件
[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini
mysqli.default_socket =/usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai
验证已安装的模块
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
配置及优化FPM模块
[root@localhost php-7.1.10]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf ##创建进程服务配置文件
扩展配置文件
[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf ##生成扩展配置文件
[root@localhost php-fpm.d]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini ##启动服务
[root@localhost php-fpm.d]# netstat -ntap | grep 9000 ##服务的9000端口已开启
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 48744/php-fpm: mast
[root@localhost php-fpm.d]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
[root@localhost php-fpm.d]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mv index.html index.html.bak ##将原来的网页首页备份掉
[root@localhost html]# vim index.php ##新建一个网页首页
<?php ## 显示php页面
phpinfo();
?>
在win10虚拟机中测试
登录数据库创建管理用户
[root@localhost opt]# mysql -u root -p ##登录数据库
Enter password: ##输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
...
省略部分内容
mysql> CREATE DATABASE bbs; ##创建用户
Query OK, 1 row affected (0.00 sec)
mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123'; ##允许从任意终端连接数据库
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123'; ##允许从本地连接数据库
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; ##刷新数据库
Query OK, 0 rows affected (0.00 sec)
mysql> quit ##退出
Bye
[root@localhost etc]# vim /usr/local/nginx/html/index.php ##修改网页文件
<?php
$link=mysqli_connect('14.0.0.27','bbsuser','admin123');
if($link) echo"Success!!
";
else echo "Fail!!";
?>
安装论坛
[root@localhost html]# cd /opt
将软件包拷贝进来
[root@localhost opt]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2 php-7.1.10.tar.bz2
mysql-5.7.20 nginx-1.12.2.tar.gz rh
mysql-boost-5.7.20.tar.gz php-7.1.10
[root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip ##解压到当前目录
[root@localhost opt]# cd dir_SC_UTF8/ ##改变一些文件的属组、属主及权限
[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/bbs
[root@localhost bbs]# chown -R root:nginx ./config/
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/
[root@localhost bbs]# chmod -R 777 ./config/
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/