一、安装前的准备
1、购买阿里云 centos7.2服务器后
2、在阿里云安全组开启需要开启的端口
3、配置防火墙,首先需要关闭centos7.2默认的防火墙 7以下是iptables
systemctl status firewalld.servic 查看默认防火墙状态
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl start firewalld.service #开启防火墙
systemctl enable firewalld.service #开机开启防火墙
firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效) #开启80端口
firewall-cmd --zone= public --remove-port=80/tcp --permanent #删除80端口
firewall-cmd --list-ports #查看已开放的端口(默认不开放任何端口)
当我们修改了某些配置之后(尤其是配置文件的修改),firewall并不会立即生效。可以通过两种方式来激活最新配置
systemctl restart firewalld 和 firewall-cmd --reload 两种方式,前一种是重启firewalld服务,建议使用后一种“重载配置文件”。
- 修改selinux的配置
Vi /etc/selinux/config
注释 SELINUX=enforcing
注释 SELINUXTYPE=targeted
增加 SELINUX=disabled
修改后重启服务器 shutdown -r now
- 文件源码包的位置: /usr/local/src
- 文件编译位置:/usr/local/软件名称
- 数据存放目录:挂载盘(df -h 查看系统盘等信息)
二、安装nginx
1、更新yum 环境
yum install epel-release -y
yum update
- 依赖yum安装
yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
- 建立用户和用户组
useradd -s /sbin/nologin www #不拿来登录的用与用户组
- 下载解压nginx包
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
- 安装编译
cd nginx-1.12.2
./configure --user=www --group=www #指定用户和用户组
--prefix=/usr/local/nginx/ #安装的位置
#--with 开启哪些模块
--with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module
make && make install
- 启动nginx
cd /usr/local/nginx
cd sbin
./nginx #启动
./nginx -s reload #重启nginx
./nginx -s stop #停止nginx
./nginx -t #验证配置文件是否正确
ps -ef | grep nginx #查看是否启动
Linux下本地测试 curl localhost
浏览器测试 输入IP地址
--------------------配置nginx为systemctl-------------------------
vim /usr/lib/systemd/system/nginx.service #编辑文件
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
systemctl daemon-reload #重新加载生效
systemctl start nginx.service #启动nginx
--------------------配置nginx为systemctl-------------------------
- 安装mysql
- 安装cmake,作为mysql的安装工具
wget https://github.com/Kitware/CMake/releases/download/v3.14.3/cmake-3.14.3.tar.gz
tar -zxvf cmake-3.14.3.tar.gz
cd cmake-3.14.3
./bootstrap
gmake
gmake install
- 安装boost_1_59_0
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxvf boost_1_59_0.tar.gz
cd boost_1_59_0
./bootstrap.sh
./b2
./b2 install
- 创建用户和用户组
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
mkdir /usr/local/mysql #mysql安装目录
mkdir /usr/local/mysql/data #mysql数据目录
chown -R mysql:mysql /usr/local/mysql #修改目录所有者
- yum -y install ncurses-devel
- 编译安装mysql
wget https://downloads.mysql.com/archives/get/file/mysql-boost-5.7.14.tar.gz
tar -zxvf mysql-boost-5.7.14.tar.gz
cd mysql-5.7.14
yum -y install ncurses-devel
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost_1_59_0 -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0
(可能会出现内存不足的情况,要么加大内存要么
[root@Byrd byrd]# free -m
total used free shared buffers cached
Mem: 512 108 403 0 0 28
-/+ buffers/cache: 79 432
Swap: 0 0 0
[root@Byrd ~]# mkdir /opt/images/
[root@Byrd ~]# rm -rf /opt/images/swap
[root@Byrd ~]# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 82.7509 s, 25.3 MB/s
[root@Byrd ~]# mkswap /opt/images/swap
mkswap: /opt/images/swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=59daeabb-d0c5-46b6-bf52-465e6b05eb0b
[root@hz mnt]# swapon /opt/images/swap
[root@hz mnt]# free -m
total used free shared buffers cached
Mem: 488 481 7 0 6 417
-/+ buffers/cache: 57 431
Swap: 999 0 999
使用完毕后可以关掉swap:
[root@hz mnt]# swapoff /opt/images/swap
[root@hz mnt]# rm -f /opt/images/swap
)
make install
- 配置mysql并初始化数据库 #daIWMCwnM9)n *%z?T3q!r(aV
#Bin/mysqld --initialize --basedir --datadir
cd /usr/local/mysql/support-files
cp ./my-default.cnf /etc/my.cnf
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
./mysqld_safe --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql & #保护模式下启动mysql(不知道什么用)
/usr/local/mysql/bin/mysql -u root -p
daIWMCwnM9)n #输入密码
set password=password(‘新密码’);
alter user 'root'@'localhost' password expire never; #设置账户永不过期
flush privileges;#刷新配置
建立软连接(快捷方式)
ln /usr/local/mysql/bin/mysql /usr/bin #可以直接 mysql -u root -p
设置开机启动:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on
chkconfig --list | grep mysqld #查看是否开机启动
- PHP编译安装
cd /usr/local/src
wget https://www.php.net/distributions/php-7.2.17.tar.gz
wget http://cn2.php.net/get/php-5.6.29.tar.gz/from/this/mirror
mv mirror phpphp.tar.gz
1、依赖包更新
yum install epel-release -y
yum update
- 依赖扩展安装
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
- 创建运行用户和建立软连接
- 创建运行用户
groupadd www
useradd -g www www
-
- 建立软连接
cp -frp /usr/lib64/libldap* /usr/lib/
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
- 编译PHP(php7默认开启zend优化。PHP扩展都是有依赖的)
cd /usr/local/src
tar -zxvf php-7.2.17.tar.gz
cd php-7.2.17
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --with-libmbfl --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm
make -j 4 && make install
make ZEND_EXTRA_LIBS=’-liconv’#php7使用(Make -j 4 不行使用)
cp php.ini-development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vi /usr/local/php/etc/php.ini
修改php.ini相关参数
vi /usr/local/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2
extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20160303/ldap.so"
设置 OPcache 缓存:
[opcache]#这个我没有
zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20160303/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
设置php安全函数:
$ vim /usr/local/php/etc/php.ini
默认值:
disable_functions =
修改为:
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
或通配:
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru
配置www.conf
取消以下注释并修改优化其参数:
listen = /var/run/www/php-cgi.sock #我这里不改
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
listen.backlog = -1
pm.max_children = 180
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 180
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log
创建php-cgi.sock存放目录
$ mkdir /var/run/www/
$ chown -R www:www /var/run/www
配置php-fpm.conf
取下以下注释并填写完整路径:
pid = /usr/local/php/var/run/php-fpm.pid
- 运行php-fpm
/usr/local/php/sbin/php-fpm
- 将php-fpm加入全局
vi /root/.bash_profile#将 /usr/local/php/bin 加到后面,用:隔开
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/lcoal/php/bin#重启
source /root/.bash_profile
- 增加php-fpm启动脚本
cp /temp/php-7.1.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm#修改权限
cd /etc/init.d
chmod +x php-fpm
Chkconfig php-fpm on
Vi /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;#fastcgi_pass unix:/var/run/www/php-cgi.sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
linux的操作例子:
找到php-fpm所有的进程然后杀死(之前怎么都关闭不掉php-fpm的pool进程,master进程是没了,但是pool进程一直杀不掉,用这个方法直接杀掉了)
pgrep php-fpm | xargs kill -s 9