Nginx是一个高性能的HTTP和反向代理服务器。
Nginx的工作原理:Nginx本身只支持静态页面的处理,当客户端访问php页面的时候,nginx会将php转到php-fpm处理,php-fpm服务会把php页面解析成html文件给nginx处理,nginx返回给客户端。
安装组建包:yum groupinstall "Development Tools" "Development Libraries" -y
安装编译包:yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* pcre-devel -y
1、下载源码包:
libmcrypt-2.5.8.tar.gz
nginx-1.9.9.tar.gz
mysql-5.5.30.tar.gz
pcre-8.37.tar.bz2
php-7.1.11.tar.bz2
2、编译安装(按顺序安装)
-
-
- tar xf pcre-8.37.tar.bz2 -C /usr/local/src/ //只解压就可以,不需要安装,安装nginx时指定路径就可以
- tar xf nginx-1.9.9.tar.gz -C /usr/local/src/
-
cd /usr/local/src/
- 安装ngin
cd nginx-1.9.9/
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.37
make -j 2 && make install //安装
useradd -M -s /sbin/nologin nginx //为nginx创建一个用户
ll /usr/local/nginx/ //查看nginx的主目录
vim /usr/local/nginx/conf/nginx.conf //修改运行用户
#user nobody; 改为user nginx nginx;
//在主页面中添加PHP页面格式,加入index.php
location / {
root html;
index index.html index.php index.htm;
}
//将注释符号去掉,将路径改为绝对路径
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
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;
}
:wq
/usr/local/nginx/sbin/nginx //启动nginx,没有start、stop等命令
netstat -tlunp |grep nginx //查看nginx端口
ln -s /usr/local/nginx/sbin/nginx /bin/ //建立软连接或者添加环境变量,使用nginx执行命令
nginx安装完成
nginx的常用维护命令:
nginx -t //检查配置文件是否有语法错误
nginx -s reload //nginx平滑重启
nginx -s storp //停止
echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local //设置开机自启动,将脚本追加到开机启动的文件中
- 安装mysql
tar xf mysql-5.6.26.tar.gz -C /usr/local/src/ //解压
cd /usr/local/src/
id mysql //查看有没有mysql用户
useradd -M -s /sbin/nologin mysql //创建mysql用户
yum install cmake ncurses-devel -y //安装必备依赖包
yum remove mysql* -y
yum remove mysql -y //删除所有与mysql相关的东西,确保系统干净
//编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DSYSCONFDIR=/etc \
-DWITH_MYISAM__STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \
-DWITH_EXTRA_CHARSETS=all \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DENABLED_LOCAL_INFILE=1 \ -DMYSQL-USER=mysql
cmake 编译选项含意:
-DCMAKE_INSTALL_PREFIX=dir_name | 设置mysql安装目录 |
-DMYSQL_UNIX_ADDR=file_name | 设置监听套接字路径,这必须是一个绝对路径名。默认为/tmp/mysql.sock |
-DDEFAULT_CHARSET=charset_name | 设置服务器的字符集。 缺省情况下,MySQL使用latin1的(CP1252西欧)字符集。cmake/character_sets.cmake文件包含允许的字符集名称列表。 |
-DDEFAULT_COLLATION=collation_name | 设置服务器的排序规则。 |
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 | 存储引擎选项: MyISAM,MERGE,MEMORY,和CSV引擎是默认编译到服务器中,并不需要明确地安装。 静态编译一个存储引擎到服务器,使用-DWITH_engine_STORAGE_ENGINE= 1 可用的存储引擎值有:ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), 和PERFSCHEMA (Performance Schema) |
-DMYSQL_DATADIR=dir_name | 设置mysql数据库文件目录 |
-DMYSQL_TCP_PORT=port_num | 设置mysql服务器监听端口,默认为3306 |
-DENABLE_DOWNLOADS=bool | 是否要下载可选的文件。例如,启用此选项(设置为1),cmake将下载谷歌所使用的测试套件运行单元测试。 |
//安装
make -j 2 && make install
chown -R mysql.mysql /usr/local/mysql/ //更改组权限
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf //复制配置文件并覆盖
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //复制启动脚本并重命名为mysqld
vim /etc/init.d/mysqld //修改启动脚本配置
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
:wq
chkconfig mysqld on //加入开机自启动
//mysql初始化
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
ln -sv /usr/local/mysql/bin/* /bin/ //创建软连接,将mysql的命令放到/bin/目录下
/etc/init.d/mysqld start //启动mysql
ps aux|grep mysql //查看进程
mysql_secure_installation //安全初始化
mysql -u root -p //登录
- 安装PHP
yum install php-pear -y //安装依赖包
tar xf libmcrypt-2.5.8.tar.gz -C /usr/local/src/ //解压
cd /usr/local/src/libmcrypt-2.5.8/
./configure --prefix=/usr/local/libmcrypt && make && make install //编译安装
-y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel libzip* //安装php的依赖包
vim /etc/ld.so.conf //添加库文件 加入
/usr/local/libmcrypt/lib
/usr/local/mysql/lib
:wq
ldconfig //加载
//编辑php
tar xf php-7.1.11.tar.bz2 -C /usr/local/src/
cd /usr/local/src/php-7.1.11/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-mysqlnd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --with-mcrypt=/usr/local/libmcrypt //编译(--with-mysqli和with-pdo-mysql之前的路径是mysqld,因为报错找不到mysql_config,所以经路径改为/usr/local/mysql/bin/mysql_config)
make -j 2 && make install //安装
//配置php和php-fpm
cp /usr/local/src/php-7.1.11/php.ini-production /usr/local/php/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf //复制配置文件
vim /usr/local/php/etc/php-fpm.conf
user = nginx
group = nginx
:wq
cp /usr/local/src/php-7.1.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //复制启动脚本
chmod +x /etc/init.d/php-fpm //添加执行权限
chkconfig php-fpm on //添加开机启动
/etc/init.d/php-fpm start //启动
//遇到问题报错
WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/*.conf' from /usr/local/php/etc/php-fpm.conf at line 124.
[11-Nov-2017 03:33:51] ERROR: No pool defined. at least one pool section must be specified in config file
[11-Nov-2017 03:33:51] ERROR: failed to post process the configuration
[11-Nov-2017 03:33:51] ERROR: FPM initialization failed
//解决办法
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
//成功启动
netstat -tlunp|grep 9000 //查看端口
echo "<?php echo phpinfo(); ?>" >>/usr/local/nginx/html/index.php //编写探针文件
nginx -s reload //重启